User:Lectrician1/Discuss.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// IMPORTING THIS SCRIPT ON OTHER WIKIS WILL NOT WORK
// That is because the script preloads a page (I'm preloading User:Lectrician1/new_revision_discussion) and preloading pages from other Wikis does not work. 
// I filed a Phabricator task (https://phabricator.wikimedia.org/T322699) asking to support them if you would like to subscribe to it.
// If you want to use the script on another wiki, you're going to need to copy the script and the User:Lectrician1/new_revision_discussion page to that wiki and change the preloadPage variable to be the page name of the one you copied.

jQuery(function () {
    var urlParams = new URLSearchParams(window.location.search);
    if (urlParams.has('diff')) {
        var preloadPage = 'User:Lectrician1/new_revision_discussion';
        var talkPagePath = $('#ca-talk > a').attr('href');
        var revisionUsername = $('#mw-diff-ntitle2 > a > bdi').html();
        var possibleRevisionDescription = $('#mw-diff-ntitle3 > span').text();
        // if possibleRevisionDescription starts with '(' then slice the ends. otherwise return ''
        var revisionDescription = possibleRevisionDescription.startsWith('(') ? possibleRevisionDescription.slice(1, -1) + ' ' : '';
        var preloadParams = [
            location.host,
            urlParams.get('diff'),
            urlParams.get('oldid'),
            urlParams.get('title'),
            revisionUsername
        ];
        var preloadTitle = encodeURIComponent(revisionDescription ? "Revision \"".concat(revisionDescription, "\" by ").concat(revisionUsername) : "Revision by ".concat(revisionUsername));
        var preloadParamsText = preloadParams.map(function (param) { return "&preloadparams[]=".concat(encodeURIComponent(param)); }).join('');
        var newTopicURL = "".concat(location.origin).concat(talkPagePath, "?action=edit&section=new&preloadtitle=").concat(preloadTitle, "&preload=").concat(preloadPage).concat(preloadParamsText);
        $('#mw-diff-ntitle1 > strong').append("\n\t\t\t<span>(<a href=\"".concat(newTopicURL, "\">discuss</a>)</span>\n\t\t"));
    }
});