Jump to content

User:Qwerfjkl/scripts/talkback.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.
// Forked from [[User:Writ Keeper/Scripts/TeahouseTalkbackLink.js]]
function talkbackLink() {
    if ($("#mw-content-text").length > 0) {
        var $nodeList = $('a[title^="User talk:"]').filter("a:not([title*='\/'])"),
            pdne = ' (page does not exist)';
        if ($nodeList !== null) {
            var i;
            for (i = 0; i < $nodeList.length; i++) {
                var $link = $($nodeList[i]);
                var newLinkString = "|";
                var redlink = false;
                var newLinkHref = $link[0].title;
                if (newLinkHref.includes(pdne)) {
                    redlink = true;
                    newLinkHref = newLinkHref.replace(pdne, "");
                }
                var newLinkId = "TBsubmit" + i;
                newLinkString += '<a href=' + (($link[0].href).replace("User_talk:", "Special:Contributions/")) + ' style="font-size:x-small; display:inline-block;">C</a>|';
                newLinkString += '<a style="font-size:x-small; display:inline-block;" title="Send a talkback!" noPopup=1 id="' + newLinkId + '" href= "'+($link[0].href)+'">TB</a>'; // add href so there is a link to the userpage just before the timestamp
                $($link).after(newLinkString);
                $("#" + newLinkId).click({
                    "page": newLinkHref,
                    "redlink": redlink
                }, function (event) {
                  event.preventDefault(); // prevent href being followed
                  talkbackSubmit(event)
                });
            }
        }
    }
}

function talkbackSubmit(event) {
    var page = event.data.page;
    var redlink = event.data.redlink;
    var questionTitle = prompt("Please enter the title of the section you're replying to (or just leave it blank):", "").trim().replace(/^(Wikipedia|User|Template|Draft|Help|Module|MediaWiki).+?#/, ''),
        sectionTitle = "{{Talkback|" + mw.config.get('wgPageName').replace(/_/g, ' ') + "|";

    if (questionTitle === null) {
        return;
    } else if (questionTitle === "") {
        sectionTitle += "ts=~~" + "~~" + "~}}\n~~" + "~";
    } else {
        sectionTitle += questionTitle + "|ts=~~" + "~~" + "~}}\n~~" + "~";
    }
    if (redlink) {
        sectionTitle = "{{Talk header}}\n== Talkback: you've got messages! ==\n" + sectionTitle;
        var requestData = {
            summary: "Talkback: you've got messages! (via [[User:Qwerfjkl/scripts/talkback|script]])"
        };
        new mw.Api().create(page, requestData, sectionTitle).done(function(data) {

            if (data && data.result && data.result === 'Success') {
                mw.notify("Talkback posted!");
            } else {
                alert('There was an error requesting the page create. Code: ' + data);
            }
        }).fail(function(data) {
            alert('There was an error using AJAX to create the page. Error: ' + data);
        });
    } else {
        var requestData2 = {
            minor: false,
            appendtext: "\n== Talkback: you've got messages! ==\n" + sectionTitle,
            //section: 'new',
            summary: "Talkback: you've got messages! (via [[User:Qwerfjkl/scripts/talkback|script]])"
        };
        new mw.Api().edit(page, function() {
            return requestData2;
        }).done(function(data) {

            if (data && data.result && data.result === 'Success') {
                mw.notify("Talkback posted!");
            } else {
                alert('There was an error requesting the page edit. Code: ' + data);
            }
        }).fail(function(data) {
            alert('There was an error using AJAX to edit the page. Error: ' + data);
        });
    }
}

mw.loader.using("mediawiki.api", function() {
    talkbackLink();
});

mw.loader.load('/w/index.php?title=User:Qwerfjkl/scripts/sectionLink.js&action=raw&ctype=text/javascript'); // Backlink: [[User:Qwerfjkl/scripts/sectionLink.js]]