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

من أرابيكا، الموسوعة الحرة
اذهب إلى التنقل اذهب إلى البحث

ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات.

//developer: user:Ebraminio
/*jslint regexp: true */
/*browser: true*/
/*global jQuery: false, mw: false, window: false, console: false */
(function ($) {
    'use strict';
    // Library!
    function addInfoToNode(node, info, color) {
        $(node).after('<span class="infoAddedcontent">[<span style="color: ' + color + ';">' + info + '</span>]</span>');
    }
    function getSelectedTextLinks() {
        // borrowed from: http://stackoverflow.com/questions/4220478 :)
        var selection,
            selectionAncestor,
            range,
            allWithinRangeParent,
            allSelected,
            i,
            el;

        // if `window.getSelection` is not defined (on IE) return nothing.
        if (window.getSelection === undefined) {
            return [];
        }
        selection = window.getSelection();

        // if nothing is selected, return empty array
        if (selection.isCollapsed) {
            return [];
        }

        range = selection.getRangeAt(0);
        selectionAncestor = range.commonAncestorContainer;
        if (selectionAncestor.getElementsByTagName === undefined) { // if it is not a formal HTML selection
            return [];
        }
        allWithinRangeParent = selectionAncestor.getElementsByTagName('a');

        allSelected = [];
        for (i = 0; (el = allWithinRangeParent[i]) !== undefined; i += 1) {
            // The second parameter says to include the element 
            // even if it's not fully selected
            if (selection.containsNode(el, true)) {
                allSelected.push(el);
            }
        }
        return allSelected;
    }
    function getArticleLinks() {
        var allSelected = getSelectedTextLinks();
        if (allSelected.length === 0) {
            return $('#bodyContent a');
        }
        return allSelected;
    }
    function parseUrl(url) {
        if (url === undefined) {
            return undefined;
        }
        var match = url.match(/\/wiki\/([^#]*)/);
        if (match === null) {
            match = url.match(/\/w\/index\.php\?title=([^&#]*).*redlink=1/);
        }

        if (match !== null) {
            return decodeURI(match[1]); // returns () matched text
        }
        return undefined;
    }
    function getLinkTitle(link) {
        return parseUrl(link.href);
    }
    function firstUserAndTime(data) {
        var res;
        $.each(data.query.pages, function (key, value) { // for retrieving first object index
            if (value.revisions !== undefined) {
                res = [value.revisions[0].user, value.revisions[0].timestamp];
            }
        });
        return res;
    }
    function detectHasTemplate(data) {
        var hasTemplate = false;
        $.each(data.query.pages, function (key, value) { // for retrieving first object index
            hasTemplate = value.templates === undefined ? false : true;
        });
        return hasTemplate;
    }
    //

    function infoAdder(infoType) {
        var enableDisambigDetection = $('#enableDisambigDetection').is(':checked'),
            enableFirstUserName = $('#enableFirstUserName').is(':checked'),
            enableFirstUserTime = $('#enableFirstUserTime').is(':checked'),
            enableRedirectDetection = $('#enableRedirectDetection').is(':checked'),
            enableRedLinkDetection = $('#enableRedLinkDetection').is(':checked'),
            api = new mw.Api(),
            links = getArticleLinks();
        if (enableRedLinkDetection) {
            $(links).filter('a.new').each(function (key, value) {
                // value.innerHTML = value.innerHTML + ' @';
                addInfoToNode(value, 'غيرموجود', 'red');
            });
        }
        $(links).each(function (key, value) {
            var jValue = $(value),
                href = jValue.attr('href'),
                name;
            if (href === undefined || href.indexOf('/wiki/') !== 0) {
                return;
            }
            name = getLinkTitle(value);
            if (name === undefined) {
                return;
            }
            // value.innerHTML = value.innerHTML + ' @';
            if (enableRedirectDetection && jValue.is('a.mw-redirect')) {
                addInfoToNode(value, 'تحويل', 'green');
            }
            if (enableDisambigDetection) {
                api.get({
                    action: 'query',
                    prop: 'templates',
                    titles: name,
                    tltemplates: 'قالب:توضيح\u200cتوضيح'
                }).done(function (data) {
                    if (detectHasTemplate(data)) {
                        addInfoToNode(value, 'توضيح\u200cتوضيح', 'aqua');
                    }
                }).fail(function (error) {
                    console.log('API failed :(', error);
                });
            }
            if (enableFirstUserName || enableFirstUserTime) {
                api.get({
                    action: 'query',
                    prop: 'revisions',
                    titles: name,
                    rvlimit: '1',
                    rvprop: 'user|timestamp',
                    rvdir: 'newer'
                }).done(function (data) {
                    var fut = firstUserAndTime(data);
                    if (fut !== undefined) {
                        if (enableFirstUserName) {
                            addInfoToNode(value, 'م:' + fut[0], 'gray');
                        }
                        if (enableFirstUserTime) {
                            addInfoToNode(value, 'في ' + new Date(fut[1]).toDateString(), 'orange');
                        }
                    }
                }).fail(function (error) {
                    console.log('API failed :(', error);
                });
            }
        });
    }

    $(function () {
        $('#articlesLinksInfo').remove();
        var html = '<span style="font-size: 40%;display: block;line-height: 1px;padding: 0px 0px 5px;color: grey;margin-top: -5px" id="articlesLinksInfo">';
        html = html + '<a id="allInfo-button" href="#">عرض معلومات الروابط</a>';
        html = html + ' <input checked type="checkbox" name="enableDisambigDetection" id="enableDisambigDetection"><label for="enableDisambigDetection">توضيح؟</label>';
        html = html + ' <input checked type="checkbox" name="enableFirstUserName" id="enableFirstUserName"><label for="enableFirstUserName">أول مستخدم؟</label>';
        html = html + ' <input type="checkbox" name="enableFirstUserTime" id="enableFirstUserTime"><label for="enableFirstUserTime">تاريخ أول تعديل؟</label>';
        html = html + ' <input checked type="checkbox" name="enableRedirectDetection" id="enableRedirectDetection"><label for="enableRedirectDetection">تحويل؟</label>';
        html = html + ' <input type="checkbox" name="enableRedLinkDetection" id="enableRedLinkDetection"><label for="enableRedLinkDetection">غيرموجود؟</label>';
        html = html + '</span>';

        $('h1:first').append(html);

        $('#allInfo-button').click(function (event) {
            event.preventDefault();
            infoAdder('all');
        });
    });
}(jQuery));
mw.util.addCSS(".infoAddedcontent{font-weight:normal!important;margin-right:2px;font-family:tahoma;font-size:12px;position:relative;top:-2px;}");