User:Rublov/diff-link.js: Difference between revisions
Appearance
Content deleted Content added
←Created page with '/** * Show "copy" links on history pages that copy "Special:Diff/XYZ" to your * clipboard when clicked. * * Inspired by User:BrandonXLF/ShowRevisionID.js...' |
(No difference)
|
Revision as of 02:14, 2 June 2021
/**
* Show "copy" links on history pages that copy "Special:Diff/XYZ" to your
* clipboard when clicked.
*
* Inspired by [[User:BrandonXLF/ShowRevisionID.js]] and
* [[User:Enterprisey/diff-permalink.js]]
*
* Written by [[User:Rublov]], June 2021
*/
const INPUT_ID = "user-rublov-diff-link";
$(function () {
if (
location.search.includes("action=history") ||
location.href.includes("Special:Watchlist") ||
location.href.includes("Special:Contributions")
) {
document
.querySelector(".mw-history-compareselectedversions")
.after(makeCopyBox());
const items = document.querySelectorAll("li[data-mw-revid]");
for (let item of items) {
const diffLink = "Special:Diff/" + item.getAttribute("data-mw-revid");
const nav = item.querySelector("span.mw-history-histlinks");
nav.appendChild(makeCopyLink(diffLink));
}
}
});
function makeCopyLink(diffLink) {
const e = document.createElement("a");
e.textContent = "copy";
e.addEventListener("click", (event) => {
event.preventDefault();
const input = document.getElementById(INPUT_ID);
input.value = diffLink;
input.select()
document.execCommand("copy");
});
const span = document.createElement("span");
span.appendChild(e);
return span;
}
function makeCopyBox() {
const input = document.createElement("input");
input.setAttribute("id", INPUT_ID);
const div = document.createElement("div");
div.append(document.createTextNode("Diff link: "));
div.appendChild(input);
return div;
}