User:Bencmq/script/sectionPermalink.js

From Meta, a Wikimedia project coordination wiki

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.
/** Creates a popup besides each heading. Provides a shortened permanent link to that section of the current version.
      Links only appear on subpages of [[Steward requests]] 
      Links are protocol-relative URLs.
       It also modifies the permanent link in Toolbox section to remove the title parameter, which is usually encoded to be ultra long and unreadable anyway. 
*/
$(function() {
    if (document.title.slice(0,16) === 'Steward requests') {
       // [[MediaWiki:Gadget-ShortLink.js]]
        var pLink = document.getElementById('t-permalink')
        if (pLink) {
            var permalink = pLink.firstChild.href.replace(/title=[^&]*&/,'');
            var relativePermalink = permalink.replace(/https?:/,'');
            pLink.firstChild.href = permalink;
        };
        
        var lis = document.getElementsByClassName('mw-headline');
        if (!lis) {return false};
        
        for (var i=0; i < lis.length; i++){
            li = lis[i];
            section_name = li.getAttribute('id');
            copy_button = document.createElement('a');
            copy_button.href = 'https:' + relativePermalink + '#' + section_name;
            copy_button.appendChild(document.createTextNode('Permalink'));
            copy_button.setAttribute('onClick', 'linkPopUp("https:' + relativePermalink + '#' + section_name + '"); return false;');
            copy_button.style.fontSize = "10pt";
            copy_button.style.fontWeight = "normal";
            copy_button.style.styleFloat = "none"; 
            copy_button.style.cssFloat = "none";
            copy_button.style.marginLeft = "4px";
            li.parentNode.appendChild(copy_button);
            
        }
    }
});
function linkPopUp(link) {
            prompt("Copy the following link to clipboard ", link);
};