User:Cobaltcigs/SectionUrlIcons.js

function SectionUrlIcons(css, icons) { // if can't find permalink, quit because most likely wgNamespace == "Special:" or wgAction != "view": var pl = document.getElementById("t-permalink"); if(!pl) return;

// quietly provide sensible defaults for missing/invalid parameter input: if(typeof(css) != "string") css = "font-size: 14px; font-weight: normal;"; if(!Array.isArray(icons) || icons.length < 2) icons = ['\u2693', '\u231a']; // get current and oldid base urls var u1 = location.href.split("#")[0], u2 = pl.getElementsByTagName("a")[0].href;

// helper func to create anchor elements: var ca = function(a, b) { var e = document.createElement("a"); // href will be intercepted by onclick, but allows copying url from right-click menu as some will prefer. e.href = a; 		e.setAttribute("onclick", `prompt("Link:", "${a}");`); e.innerHTML = b;		return e.outerHTML; };	// inject the css: document.head.innerHTML += `.mw-section-url-icons { ${css} } `;

// iterate through every h1-h6 element: for(var hn of [...Array(6).keys].map(n => `h${n+1}`)) for(var e of document.getElementsByTagName(hn)) { var mw = e.getElementsByClassName("mw-headline")[0]; if(!mw) continue; var a = "#" + mw.getAttribute("id"); var html = ` [${ca(u1+a,icons[0])}|${ca(u2+a,icons[1])}] `; mw.insertAdjacentHTML("afterend", html); }	}