Wikipedia Anchors

Add an anchored link to titles on Wikipedia articles

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Wikipedia Anchors
// @version      0.1
// @namespace    https://github.com/OoDeLally/wikipedia-anchors
// @description  Add an anchored link to titles on Wikipedia articles
// @author       Pascal Heitz
// @license      MIT
// @include        http*://*.wikipedia.org/*
// @include        http*://wikipedia.org/*
// @grant        none
// ==/UserScript==

function createDivider() {
    var divider = document.createElement('span');
    divider.innerHTML = ' | ';
    divider.classList.add('mw-editsection-divider');
    divider.style.display = 'inline';
    return divider;
}

function createLink(href) {
    var link = document.createElement('a');
    link.innerHTML = 'link';
    link.style.display = 'inline';
    link.href = href;
    return link;
}

function createBracket(innerHTML) {
    var bracket = document.createElement('span');
    bracket.innerHTML = innerHTML;
    bracket.classList.add('mw-editsection-bracket');
    bracket.style.display = 'inline';
    return bracket;
}

function createEditSection() {
    var editSectionSpan = document.createElement('span');
    editSectionSpan.classList.add('mw-editsection');
    editSectionSpan.appendChild(createBracket('['));
    editSectionSpan.appendChild(createBracket(']'));
    return editSectionSpan;
}

function insertBeforeLastIfPossible(containerNode, childNode) {
    if (containerNode.childNodes.length < 1) {
        return;
    }
    containerNode.insertBefore(childNode, containerNode.lastElementChild);
}


(function() {
    'use strict';

    var headers = document.querySelectorAll('h1, h2, h3, h4, h5, h6');
    for (var header of headers) {
        var headlineSpan = header.querySelectorAll('span.mw-headline')[0];
        if (!headlineSpan) {
            continue;
        }
        var headlineId = headlineSpan.id;
        if (!headlineId) {
            continue;
        }


        var anchoredLink = createLink(document.location.href.match(/(^[^#]*)/)[0] + "#" + headlineId);


        var editsectionSpan = header.querySelectorAll('span.mw-editsection')[0];
        if (editsectionSpan) {
            insertBeforeLastIfPossible(editsectionSpan, createDivider());
            insertBeforeLastIfPossible(editsectionSpan, anchoredLink);
        } else {
            // The editsection doesnt exist, we need to create it
            editsectionSpan = createEditSection();
            header.appendChild(editsectionSpan);
            insertBeforeLastIfPossible(editsectionSpan, anchoredLink);
        }

    }

})();