MediaWiki:HeaderLinks/code.js

/** __NOWYSIWYG__ * * HeaderLinks * * This adds an icon to header tags which alters the url * to target that header which can be copied and pasted * into chat or discussions for easier linking without * having to dig it out of the ToC. * * @author Cåm * * For documentation see http://dev.wikia.com/wiki/Header_Links */

/*jshint browser:true, camelcase:true, curly:true, eqeqeq:true, forin:true, immed:true, indent:4, jquery:true, latedef:true, onevar:true, plusplus:true, quotmark:single, undef:true, unused:true, strict:true, trailing:true */

/*global mediaWiki:true */

(function ($, mw) {

'use strict'; function headerLinks { var headers, ids = [], padlockSpan, padlockLink, padlockImage, i;

headers = document.getElementsByClassName('mw-headline');

for (i = 0; i < headers.length; i += 1) { ids.push(headers[i].id); }

for (i = 0; i < ids.length; i += 1) {

padlockSpan = document.createElement('span'); padlockLink = document.createElement('a'); padlockImage = document.createElement('img');

// wrapping span tag padlockSpan.className = 'mw-header-link'; padlockSpan.style.cssFloat = 'right';

// anchor tag padlockLink.title = 'Link to this header'; padlockLink.href = '/wiki/' + mw.config.get('wgPageName') + '#' + ids[i];

// image tag padlockImage.width = '20'; padlockImage.alt = 'Ambox padlock gray'; padlockImage.src = '//upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Ambox_padlock_gray.svg/20px-Ambox_padlock_gray.svg.png';

padlockLink.appendChild(padlockImage); padlockSpan.appendChild(padlockLink);

headers[i].parentNode.insertBefore(padlockSpan, headers[i].nextSibling);

}

}

$(function {        headerLinks;    });

}(jQuery, mediaWiki));

/* */