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 next to page headers which copies a permalink to that section
    using [[Special:Permalink]].
*/

$(function() {
    var oldid = mw.config.get("wgRevisionId");
    if (!oldid || mw.config.get("wgAction") !== "view") {
        return;
    }
    if (mw.config.get("wgPageName") == "Main_Page") {
    	return;
    }

    mw.loader.using( ['mediawiki.util'], function() {
        mw.util.addCSS(
            ".permalink-wrapper {"                                                +
                "margin-left: 1em;"                                               +
                "visibility: hidden;"                                             +
            "}"                                                                   +
            ".permalink-wrapper > a:not(:first-child) {"                          +
                "margin-left: 0.5em;"                                             +
            "}"                                                                   +
            ".permalink-wrapper > a:hover {"                                      +
                "text-decoration: none;"                                          +
            "}"                                                                   +
            ".permalink-wrapper > a:active {"                                     +
                "color: white;"                                                   +
                "background-color: #002bb8;"                                      +
            "}"                                                                   +
            ".permalink-heading:hover > .permalink-wrapper {"                     +
                "visibility: visible;"                                            +
            "}"
        );
        $(".mw-headline").each(function(i, title) {
            var tid = $(title).prop("id");
            var link = "Special:Permalink/" + oldid + "#" + tid.replace(/_/g, " ");
            var head = $(title).parent().addClass("permalink-heading");
            var wrapper = $("<span/>", {
            	"class": "permalink-wrapper",
            }).appendTo(head);
            $("<a/>", {
                title: "Link to this section",
                href: "#" + tid,
                html: "&#xa7;",
            }).appendTo(wrapper);
            $("<a/>", {
                title: "Copy a permalink to this section to the clipboard",
                href: "#" + tid,
                html: "&#x2702;",
                click: function() {
                    var temp = $("<textarea/>", {
                        val: "[[" + link + "]]"
                    }).appendTo("body");
                    temp.select();
                    document.execCommand("copy");
                    temp.remove();
                    return false;
                }
            }).appendTo(wrapper);
        });
    });
});