משתמש:Eldad/new-64.js

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

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

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אינטרנט אקספלורר / אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
  • אופרה: ללחוץ על Ctrl-F5.
/* Available warnings */
var deletionWarnings = [
        {name:"הבל", template: "הבל", description:"הוספת אזהרת הבל", noLinkTitle: true},
        {name:"נמחק", template: "נמחק", description:"הוספת תבנית מחיקה", noLinkTitle: true},
        {name:"ניסוי נמחק", template: "נמחק ניסויים", description:"הוספת תבנית מחיקה של ניסוי", noLinkTitle: true},
        {name:"פרסום", template: "פרסומת", description:"הוספת תבנית פרסומת", noLinkTitle: true},
        {name:"הועתק", template: "העתקה", description:"הוספת אזהרת העתקה"},
        {name:"תרגום מכונה", template: "תרגום מכונה", description:"הוספת אזהרת תרגום מכונה", noLinkTitle: true},
    ];

function getBlockWarnings(userName){
    var anon = isAnonUser(userName),
        warnings = [
            {
                name:"נחסמת", template: "נחסמת",
                description:"הודעה למשתמש שנחסם",
                askExtra: {prompt: 'סיבת החסימה', param: '|'},
                noPage: true,
                title: "נחסמת"
            }];
    if (anon) warnings.push(
        {
            name: "הוספת שטויות לדפים",
            template: "ס:הוספת שטויות לדפים",
            description: "הודעה לIP שנחסם לטווח ארוך",
            noPage: true, noTitle: true,
        },
        {
            name: "כתובת IP חסומה",
            template: "ס:משתמש:Bharel/תבניות/כתובת IP חסומה 2",
            description: "הודעה לIP שנחסם לטווח ארוך",
            noPage: true, prepend:true, noTitle: true,
        });
    return warnings;
}
function getRollbackWarnings(userName){
    /* Gets the rollback warnings according to the anonymity of the user */
    var anon = isAnonUser(userName);
    return [
        anon ?
            {name:"אזהרה", template: "אזהרה",
             description:"הוספת תבנית אזהרה", watch: false} :
            {name:"אזהרה", template: "אזהרה לרשום", 
             description:"הוספת תבנית אזהרה", watch: false},
        {name:"ניסוי", template: "ניסויים",
         description:"הוספת תבנית ניסויים", watch: false},
        {name:"בוטל",  template: "בוטל",
         description:"הוספת תבנית בוטל",
         askExtra: {prompt: 'סיבת הביטול', param: '|סיבה='}, watch: true},
        {name:"חזרה", template: "חזרה על עריכה",
         description:"הוספת תבנית חזרה על עריכה"},
        {name:"פרסום", template: "פרסומת",
         description:"הוספת תבנית פרסומת", watch: false},
        anon ?
            {name:"העתקה", template: "העתקה",
             description:"הוספת תבנית העתקה",
             askExtra: {prompt: 'מקור', param: '|'}, watch: false} :
            {name:"העתקה", template: "העתקה לרשום",
             description:"הוספת תבנית העתקה",
             askExtra: {prompt: 'מקור', param: '|'}, watch: false},
        {name:"הסבר", template: "הסבר",
         description:"הוספת תבנית הסבר",
         askExtra: {confirm: 'בוטל ? (ביטול=לא)', param: '|בוטל=כן'}, watch: true},
        {name:"אומנם", template: "ס:אומנם",
         description:"הודעה למשתמש שנחסם שיוכל להירשם", noPage: true,
         noTitle: true},
        ];
}

function isAnonUser(userName){
    /* Checks if this is an anonymous user */
    return (mw.util.isIPv4Address(userName) ||
            mw.util.isIPv6Address(userName));
}

function createTemplatesSpan(userName, templates){
    /* Creates a span with the given templates.
    Each template is an object with the following attributes:
        name: template name to show as the link text
        template: template's page name (to be included in user talk)
        description: hover description
    The following attributes are optional:
        watch: To watch or unwatch user talk page
        noPage: Don't include the page in the template
        noTitle: Don't create a new section when adding the template
        noLinkTitle: Section title should not be a link to the page
        askExtra:
            Object with one of the following:
                confirm: confirm dialog.
                    Accepting will add param to template
                prompt: prompt dialog.
                    Submitting will ass param together with prompt */
    // Container span
    var container = document.createElement("span"),
        seperator = document.createTextNode(" | ");
    container.insertAdjacentText("beforeend", "(");
    function appendTemplateElement(template){
        /* Appends a template link together with it's event handler */
        var element = document.createElement("a");
        element.insertAdjacentText("beforeend", template.name);
        element.addEventListener(
            "click",
            function(){
                activateWarning.call(this, userName, template);
            });
        container.appendChild(element);
    }
    /* Add elements to container,
    Does not add seperator before first element */
    appendTemplateElement(templates[0]);
    for (var i = 1; i < templates.length; i++) {
        container.appendChild(seperator.cloneNode());
        appendTemplateElement(templates[i]);
    }
    container.insertAdjacentText("beforeend", ")");
    return container;
}

function activateWarning(userName, template){
    /* Called upon clicking a template */
    var templateString = template.template,
        pageName = mw.config.get('wgPageName').replace(/_/g, " "),
        // Make sure to only link and not include categories & files.
        linkPageName = (
            ([6, 14].indexOf(mw.config.get('wgNamespaceNumber')) !== -1 ?
             ":" : "") + pageName);
    if (!template.noPage)
        templateString += "|" + linkPageName;
    if (template.askExtra) {
        if (template.askExtra.prompt) {
            let userInput = prompt(template.askExtra.prompt);
            // Add parameter if prompt was accepted.
            if (userInput !== null){
                templateString += (
                    template.askExtra.param + userInput.trim());
            }
        } else if (template.askExtra.confirm &&
                   confirm(template.askExtra.confirm)) {
            // Add parameter if confirmed.
            templateString += template.askExtra.param;
        }
    }
    var fulltext = "{{" + templateString + "}} ~~" + "~~\n";
    if (template.title)
        fulltext = "\n==" + template.title + "==\n" + fulltext;
    else if (!template.noTitle) {
        fulltext = "\n==" + (
            template.noLinkTitle ? pageName :
            "[[" + linkPageName + "]]") + "==\n" + fulltext;
    }
    // Add the resulting string to the user talk page.
    addToPage(
        "שיחת משתמש:" + userName, template.template,
        fulltext, template.watch,
        () => mw.notify(
            ' תבנית "' + template.template +
            '" נרשמה בשיחת משתמש:' + userName),
        template.prepend);
}

function addToPage(pageTitle, summary, content,
                   toWatch, successCallback, prepend){
    /* Appends content to the given pageTitle */
    var params = {
            action: "edit",
            title: pageTitle,
            summary: summary,
            format: "json",
            token: mw.user.tokens.get("csrfToken"),
            
        };
    if (prepend)
        params.prependtext = content;
    else
        params.appendtext = content;
    if (toWatch !== undefined)
        params.watchlist = toWatch ? "watch" : "unwatch";
    $.post(
        mw.util.wikiScript('api'),
        params,
        function (data) {
            if (!data) return;
            if (data.error)
                alert('Error saving: ' + data.error.info);
            else if (data.edit &&
                     data.edit.result == 'Success' &&
                     successCallback !== undefined)
                successCallback();
        });
}

// Wait for page to load.
if (["loaded", "complete",
     "interactive"].indexOf(
        document.readyState) === -1)
    document.addEventListener("DOMContentLoaded", mainWarningScript);
// Activate if page was already loaded.
else mainWarningScript();

function mainWarningScript(){
    "use strict";
    var mainElement = document.getElementById("mw-content-text"),
        userName, userlinkNode;
    function insertSibling(insert, node){
        return node.parentNode.insertBefore(insert, node.nextSibling);
    }
    
    // If we blocked a user
    var blockPage = mw.config.get('wgCanonicalSpecialPageName') === 'Block';
    if (blockPage) {
        userlinkNode = mainElement.getElementsByTagName("a")[0];
        userName = userlinkNode.firstChild.nodeValue;
        let spaceNode = document.createTextNode(" ");
        insertSibling(
            spaceNode,
            userlinkNode.parentNode);
        insertSibling(
            createTemplatesSpan(
                userName, getBlockWarnings(userName)),
            spaceNode);
        return;
    }

    // Check if it's a diff
    var diffElement = document.getElementById("mw-diff-ntitle2");

    if (diffElement !== null){
        userName = diffElement.getElementsByTagName(
            "bdi")[0].firstChild.nodeValue;
        var templateSpan = createTemplatesSpan(
                userName, getRollbackWarnings(userName)),
            templateParagraph = document.createElement("p");
        // Transfer to paragraph
        while (templateSpan.childNodes.length > 0){
            templateParagraph.appendChild(templateSpan.firstChild);
        }
        diffElement.appendChild(templateParagraph);
        return;
    }

    var action = mw.config.get('wgAction');
    
    // Check for rollback
    if (action == "rollback"){
        var userElement = mainElement.getElementsByTagName(
            "bdi")[0],
            userToolLinksNode = userElement.parentNode.nextElementSibling;
        userName = userElement.firstChild.nodeValue;
        console.assert(
            userToolLinksNode.className ==
            "mw-usertoollinks", userToolLinksNode.className);
        let spaceNode = document.createTextNode(" ");
        insertSibling(
            spaceNode,
            userToolLinksNode);
        insertSibling(
            createTemplatesSpan(userName,
                getRollbackWarnings(userName).concat(
                    getBlockWarnings(userName))),
            spaceNode);
        return;
    }
    
    // Might be a deletion
    if (action === "delete" &&
        // undefined > -1 = NaN meaning it's true only if sysop.
        mw.config.get('wgUserGroups').indexOf("sysop") > -1 &&
        // Make sure deletion was successful
        !document.getElementById("deleteconfirm")){
        // Query API for last revisions
        $.getJSON(
            mw.util.wikiScript('api'),
            {action: 'query', list: 'deletedrevs', drlimit: 20, titles: mw.config.get('wgPageName'), drprop: 'user', format: 'json'},
            function(data) {
                var users = new Set();
                if (data && data.query && data.query.deletedrevs)
                    // Add all users who made a change in the last 20 revisions.
                    data.query.deletedrevs.forEach(
                        (deletedrev) => deletedrev.revisions.forEach(
                            (revision) => users.add(revision.user)));
                else return;
                var container = document.createElement("p");
                container.insertAdjacentText(
                    "beforeend",
                    'את הדף ' +
                    mw.config.get('wgPageName') +
                    ' ערכו העורכים הבאים:');

                function createUserLinks(userName){
                    /* Creates basic links for a user. Returns a
                    DOM fragment. */
                    function createLink(url, text){
                        var element = document.createElement("a");
                        element.setAttribute("href", url);
                        element.textContent = text;
                        return element;
                    }
                    var fragment = document.createDocumentFragment();
                    fragment.appendChild(createLink(
                        mw.util.getUrl("User:" + userName),
                        userName));
                    fragment.appendChild(document.createTextNode(" ("));
                    fragment.appendChild(createLink(
                        mw.util.getUrl("User Talk:" + userName),
                        "שיחה"));
                    fragment.appendChild(document.createTextNode(" | "));
                    fragment.appendChild(createLink(
                        mw.util.getUrl("Special:Contributions/" + userName),
                        "תרומות"));
                    fragment.appendChild(document.createTextNode(" | "));
                    fragment.appendChild(createLink(
                        mw.util.getUrl("Special:Block/" + userName),
                        "חסימה"));
                    fragment.appendChild(document.createTextNode(' ) '));
                    return fragment;

                }
                var userContainer;
                // For each user, adds his links and warnings.
                users.forEach((user) => {
                    userContainer = document.createElement("p");
                    userContainer.appendChild(createUserLinks(user));
                    userContainer.appendChild(
                        createTemplatesSpan(user, deletionWarnings));
                    container.appendChild(userContainer);
                });
                // Append the whole container
                mainElement.appendChild(container);
            });
        return;
    }
    
}