ميدياويكي:Gadget-decodesummary.js

من كوبتيكبيديا
اذهب إلى التنقلاذهب الى البحث
لم تعد النسخة القابلة للطباعة مدعومة وقد تحتوي على أخطاء في العرض. يرجى تحديث علامات متصفحك المرجعية واستخدام وظيفة الطباعة الافتراضية في متصفحك بدلا منها.

ملاحظة: بعد النشر، أنت قد تحتاج إلى إفراغ الكاش الخاص بمتصفحك لرؤية التغييرات.

  • فايرفوكس / سافاري: أمسك Shift أثناء ضغط Reload، أو اضغط على إما Ctrl-F5 أو Ctrl-R (⌘-R على ماك)
  • جوجل كروم: اضغط Ctrl-Shift-R (⌘-Shift-R على ماك)
  • إنترنت إكسبلورر/إيدج: أمسك Ctrl أثناء ضغط Refresh، أو اضغط Ctrl-F5
  • أوبرا: اضغط Ctrl-F5.
//[[fa:mediawiki:Gadget-decodesummary.js]]
/*jslint browser: true*/
/*global $, jQuery, mw*/
(function () {
    'use strict';
    function deChar(ss) {
        try {
            ss = decodeURIComponent(ss.replace(/\.([0-9A-F][0-9A-F])/g, '%$1'));
        } catch (ignore) {}
        return ss;
    }
    function decodeAnchor(link) { //simplify internal link: replace %20 and _ then decode anchor
        link = link.replace(/(_|%20)/g, ' ').replace(/^\ +|\ +$/g, '');
        var parts = link.split('#'),
            anchor,
            hidIdx = -1,
            hidden = [];
        if (parts.length !== 2) {
            return link; //no anchor
        }
        anchor = parts[1];

        //decode 4, 3 and 2-byte: http://en.wikipedia.org/wiki/UTF-8
        anchor = anchor.replace(/\.F[0-4]\.[89AB][\dA-F]\.[89AB][\dA-F]\.[89AB][\dA-F]/g, deChar);
        anchor = anchor.replace(/\.E[\dA-F]\.[89AB][\dA-F]\.[89AB][\dA-F]/g, deChar);
        anchor = anchor.replace(/\.[CD][\dA-F]\.[89AB][\dA-F]/g, deChar);
        anchor = anchor.replace( //hide IPs
            /(?:^|[^0-9A-F\.])(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/,
            function (s) {
                hidIdx += 1;
                hidden.push(s);
                return '\x01' + hidIdx + '\x02';
            }
        );
        //decode 1-byte chars: all symbols except  -.:_  and []{} prohibited in links
        anchor = anchor.replace(/\.[2-7][0-9A-F]/g, function (hhh) {
            var ch = deChar(hhh);
            if ('!"#$%&\'()*+,/;<=>?@\\^`~'.indexOf(ch) >= 0) {
                return ch;
            }
            return hhh;
        });
        //unhide IPs and return
        hidden.forEach(function (currentValue, index) {
            anchor = anchor.replace('\x01' + index + '\x02', currentValue);
        });
        if (anchor.indexOf("''") !== -1) {
            return link; //cannot have double '' in link
        }
        return parts[0] + '#' + anchor;
    }
    var fields = '#wpSummary, #wpReason, [name=wpReason], #mwProtect-reason, #mw-input-wpReason-other, #wpComment, .mw-ui-input.summary, #mw-input-reason > .oo-ui-inputWidget-input';
    $(fields).one('paste keyup change', function () {
    	if ( $(fields).byteLimit ) {
	        $(fields).byteLimit(Infinity);
    	}
    });
    $(fields).on('paste keyup change', function (e) {
        var target, val, bytes;
        target = $(e.currentTarget);
        val = target.val();
        // from https://stackoverflow.com/questions/2219526/
        bytes = encodeURI(val).split(/%(?:u[A-F0-9]{2})?[A-F0-9]{2}|./).length - 1;
        target.css('background-color', bytes > 255
            ? '#FFEBEB'
            : 'white');
        val = val.replace( // Put diff before oldid.
            /(oldid=(?:\d+|next|prev|cur)?)&(diff=(?:\d+|next|prev|cur)?)/gi,
            '$2&$1'
        ).replace( // Special:Diff
            /(?:(^|[\ \\\^`#<>\[\]\"\t\n{\|}])|https?:\/\/([a-z\-]+)\.(?:wikipedia|wikimedia)\.org\/[^\ \\\^`#<>\[\]\"\t\n{\|}]*)&?diff=(\d+|next|prev|cur)(?:&oldid=(\d+|next|prev|cur)?)?(#[^\ \\\^`#<>\[\]\"\t\n{\|}]*)?(?=$|[\ \\\^`#<>\[\]\"\t\n{\|}])/gi,
            '$1[[$2:Special:Diff/$4/$3$5]]'
        ).replace( // Special:Permalink
            /(?:(^|[\ \\\^`#<>\[\]\"\t\n{\|}])|https?:\/\/([a-z\-]+)\.(?:wikipedia|wikimedia)\.org\/[^\ \\\^`#<>\[\]\"\t\n{\|}]*)&?oldid=(\d+|next|prev|cur)(#[^\ \\\^`#<>\[\]\"\t\n{\|}]*)?(?=$|[\ \\\^`#<>\[\]\"\t\n{\|}])/gi,
            '$1[[$2:Special:Permalink/$3$4]]'
        ).replace( // [[Special:Diff/12345678/prev]] is equal to [[Special:Diff/12345678]]
            /\[\[([a-z\-]+:)Special:Diff\/(\d+|next|prev|cur)\/prev\]\]/gi,
            '[[$1Special:Diff/$2]]'
        ).replace( // no oldid
            'Special:Diff//',
            'Special:Diff/'
        ).replace(
            '[[fa:',
            '[['
        ).replace(
            '[[:',
            '[['
        );
        try {
            val = decodeURI(val.replace(/%20/g, '\u200D\u200D\u200D')).replace(/\u200D\u200D\u200D/g, '%20'); // three ZWJs
            val = val.replace(/\[\[.*?\]\]/g, decodeAnchor);
        } catch (ignore) {}
        if (val !== target.val()) {
            target.val(val);
        }
    });
}());