Jump to content

User:Ilmari Karonen/longeditsummary.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// LIMIT EDIT SUMMARIES TO EXACTLY 250 UTF-8 BYTES
// see EditPage::importFormData() in MediaWiki source for the source of the limit
// new, simpler and improved version, thanks to [[User:Remember the dot]] for the idea

$(function () {
    var wpSummary = document.getElementById("wpSummary")
    if (wpSummary)
    {
        var oldValue = wpSummary.value
        var adjustMaxLength = function ()
        {
            // subtract the number of UTF-8 continuation bytes (0x80-0xBF) from the maxlength
            var maxLength = 250 - encodeURI(wpSummary.value).split(/%[89AB]/i).length + 1

            // the last character or group might've pushed us over; if so, revert it
            if (wpSummary.value.length > maxLength && wpSummary.value != oldValue)
            {
               wpSummary.value = oldValue
            }
            else
            {
                oldValue = wpSummary.value
                wpSummary.setAttribute("maxlength", maxLength)
            }
        }
        var eventTypes = ["keyup", "keydown", "keypress", "mouseup", "mousedown", "click", "focus", "blur", "change"]
        for (var i = 0; i < eventTypes.length; i++)
        {
            addHandler(wpSummary, eventTypes[i], adjustMaxLength)
        }
        adjustMaxLength()
    }
})