User:Þjarkur/Permalink to section.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.
/*
  Adds a link to copy a permalink pointing to a section right after the "[ edit | edit source ]" buttons
*/
(function () {
  if(mw.config.get('wgNamespaceNumber') === 0) return; // Ignore in article space
  var revision = mw.config.get('wgRevisionId')
  $('.mw-headline').each(function(){
    var anchor = $(this).attr('id')
    var URL = 'Special:Permalink/'+revision+'#'+anchor
    var link = '<a href="/wiki/'+URL+'">permalink</a>'
    if($(this).next().hasClass('mw-editsection')){
      $(this).next().children().last().before(' <span class="mw-editsection-divider" style="display:inline-block!important"> | </span> '+link)
    } else {
      $(this).parent().append('<span class="mw-editsection"><span class="mw-editsection-bracket">[</span>'+link+'<span class="mw-editsection-bracket">]</span></span>')
    }
  })
})()