User:The Earwig/permalink.js
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. User:The Earwig/permalink. |
/*
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: "§",
}).appendTo(wrapper);
$("<a/>", {
title: "Copy a permalink to this section to the clipboard",
href: "#" + tid,
html: "✂",
click: function() {
var temp = $("<textarea/>", {
val: "[[" + link + "]]"
}).appendTo("body");
temp.select();
document.execCommand("copy");
temp.remove();
return false;
}
}).appendTo(wrapper);
});
});
});