מדיה ויקי:EditSummaryLengthLimit.js

מתוך ויקיפדיה, האנציקלופדיה החופשית

הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אינטרנט אקספלורר / אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
  • אופרה: ללחוץ על Ctrl-F5.
// make sure edit summary does not exceed byte limit

// copied from [http://bug-attachment.wikimedia.org/attachment.cgi?id=7350]

if ( editSummaryLengthLimitLoaded == undefined ) var editSummaryLengthLimitLoaded = false;

function editSummaryLengthLimit()
{
    if ( editSummaryLengthLimitLoaded ) return;
    editSummaryLengthLimitLoaded = true;

    var summary = document.getElementById("wpSummary");
    if ( !summary ) return;
    summary.maxLength = 250;

    checkSummary = function(e)
        {
            if ( !e ) e = window.event;    // IE

            // first check to see if this is actually a character key being pressed.
            // Based on key-event info from http://unixpapa.com/js/key.html
            // note === sign, if undefined, still could be a real key
            if ( e.which === 0 || e.charCode === 0 || e.ctrlKey || e.altKey || e.metaKey )
                return true;                         //a special key (backspace, etc) so don't interefere.

            // This basically figures out how many bytes a utf-16 string (which is what JS sees) will take in utf-8
            // by replacing a 2 byte character with 2 *'s, etc, and counting that.
            // Note, surogate (\uD800-\uDFFF) characters are counted as 2 bytes, since theres two of them
            // and the actual character takes 4 bytes in utf-8 (2*2=4). might not work perfectly in edge cases
            // such as illegal sequences, but that should never happen.
            len = summary.value.replace(/[\u0080-\u07FF\uD800-\uDFFF]/g,"**").replace(/[\u0800-\uD7FF\uE000-\uFFFF]/g,"***").length;

            if ( len > 248 )
            {
                if ( e.preventDefault ) e.preventDefault();
                e.returnValue = false;                      // IE
            }
        };

    addHandler ( summary, "keypress", checkSummary );
}

if ( mw.config.get('wgAction') == "edit" || mw.config.get('wgAction') == "submit" ) $( editSummaryLengthLimit );