Jump to content

User:FlightTime/CustomTBLinks.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.
/* My slightly changed version of {{User:Chicocvenancio/HelpDeskTBLinks.js}}

NOTE:This script only works in "Vector"
   
If you want to use this script, simply add the following line to your [[Special:Mypage/vector.js]] or [[Special:Mypage/common.js]]
 
importScript('User:FlightTime/CustomTBLinks.js'); // [[User:FlightTime/CustomTBLinks.js]]                                             
 
*  Please keep the backlink so I can see how many people use this. 
*/

if ((mw.config.get('wgPageName') === "Wikipedia:Help_desk") || (mw.config.get('wgPageName') === "Wikipedia:Files_for_upload") ||  (mw.config.get('wgPageName') === "Wikipedia:Requests_for_permissions/Account_creator") || (mw.config.get('wgPageName') === "User_talk:FlightTime") ||(mw.config.get('wgPageName') === "User_talk:FlightTime/Alternate_talk_page") || (mw.config.get('wgPageName') === "User_talk:FlightTime_Phone") || (mw.config.get('wgPageName') === "User_talk:FlightTime_Public") && mw.config.get('wgAction') === 'view') {
    mw.loader.using(['jquery.tipsy', 'jquery.ui', 'jquery'], function () {
    $(mw.TBLinks()); });
};
( function ( mw, $ ) {
    var TBLinks;
    TBLinks = function () {
        "use strict";
        if ($("#mw-content-text").length > 0) {
            var $nodeList = $('a[title^="User talk:"]'), pdne = ' (page does not exist)', i, $link, $newLink;
            if ($nodeList !== null) {
                for (i = 0; i < $nodeList.length; i += 1) {
                    $link = $($nodeList[i]);
                    $newLink = $('<a href="#' + $link[0].title.replace(pdne, "") + '" style="font-size:x-small; display:inline-block;" title="Send a talkback!" noPopup=1 id="TBsubmit' + i + '">TB</a>');
                    $($link).after($newLink);
                }
            }
            $('a[title="Send a talkback!"]').tipsy({html: true}).before("|").after("|").click(function (e) {
                TBLinks.submit($(this).attr('href').substr(1), this.id);
            });
        };
    };
    TBLinks.submit = function  (page, id) {
        'use strict';
        TBLinks.edit = function (section) {
            var sectionTitle = "{{Talkback|" + mw.config.get('wgPageName').replace(/_/g, ' ') + "|", data;
            if (section === "") {
                sectionTitle += "ts=~~" + "~~}}";
            } else {
                sectionTitle += section + "|ts=~~" + "~~}}";
            }
            data = {
                format : 'json',
                action : 'edit',
                minor : false,
                title : page,
                text : sectionTitle,
                section : 'new',
                summary : "Talkback: you've got messages!",
                token : mw.user.tokens.get('csrfToken')
            };
            $.ajax({
                url : mw.util.wikiScript('api'),
                type : 'POST',
                dataType : 'json',
                data : data,
                success : function (data) {
                    if (data && data.edit && data.edit.result && data.edit.result === 'Success') {
                        TBLinks.useDialog('Success', 'Success in sending your talkback (<a href="' +
                            mw.util.getUrl( page ) + '">Open</a>) (<a href="' +
                            mw.util.getUrl( page ) + '?diff=0">diff</a>)', 'nprompt-dialog');
                    } else {
                        TBLinks.useDialog('Error!!', 'There was an error requesting the edit. Code: ' + data.error.code + '": ' + data.error.info, 'nprompt-dialog');
                    }
                },
                error : function () {
                    TBLinks.useDialog('Error!!', 'There was an error in using AJAX to edit the page.', 'nprompt-dialog');
                }
            });
        }
        TBLinks.nicePrompt("Please enter the title of the question you're replying to (or just leave it blank):", "Section:", TBLinks.edit);
    };
    TBLinks.nicePrompt = function (title, label, callback) {
        'use strict';
        $('<div id="nprompt-dialog" class="ui-widget">' + (label ? '<label for="nprompt-input">' + label + '</label>' : '') + '<input type="text" id="nprompt-input"> </input></div>').dialog({
            title: title,
            open: function () {
                $(".ui-dialog-titlebar-close").hide();
                $('#nprompt-input').autocomplete({source: $('.toctext').map(function() { return $(this).text(); }).get()})
            },
            close: function () {
            $('#nprompt-dialog').dialog('destroy')
            $('#nprompt-dialog').remove()
            },
            buttons: {
                "OK": function () {
                    callback($('#nprompt-input').val());
                },
                Cancel: function() {
                    TBLinks.useDialog('Oh!', 'You have canceled!', 'nprompt-dialog');
                }
            }         
        });
    };
    TBLinks.useDialog = function (title, message, id) {
        'use strict';
        $('#' + id).empty();
        $('#' + id).dialog("option", "title", title);
        $('#' + id).append($('<div>' + message + '</div>'));
        $('#' + id).dialog("option", "buttons", { "Close": function() {
            $(this).dialog("close");
        } } );
    };
    mw.TBLinks = TBLinks;
}( mediaWiki, jQuery ) );