User:REAL MOUSE IRL/pings-tool.js
Appearance
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. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:REAL MOUSE IRL/pings-tool. |
$(document).ready(function () {
const makeNode = html => {
let t = document.createElement("template");
t.innerHTML = html;
return t.content.firstElementChild;
};
const linkStyle = `
font-size: 0.6em;
font-family: sans-serif;
font-weight: bold;
margin-left: 0.75rem;
color: red;
`;
//Talk, Wikipedia, and Wikipedia_talk: namespaces
if ([1, 3, 4].includes(mw.config.get("wgNamespaceNumber"))) {
const pageId = mw.config.get("wgArticleId");
// We only need heading2s, but section number is based on all headings
let headings = document.getElementsByClassName("mw-heading");
//Filter to h2s, get section indices
let h2s = Object.values(headings).flatMap((h, i) =>
h.className.includes("mw-heading2") ? [{ index: i + 1, heading: h }] : []
);
h2s.forEach(h => {
let e = makeNode(`<a style="${linkStyle}">[@]</a>`);
e.onclick = e => {
console.log(`Pings tool: Grabbing wikitext for page ${pageId}, section ${h.index}`);
new mw.Api()
.get({
action: "query",
pageids: pageId,
rvsection: h.index,
prop: ["revisions", "info"],
rvprop: "content",
indexpageids: 1,
rawcontinue: ""
})
.then(a => {
// Get only paragraphs that end with a timestamp
let sigParas = a.query.pages[pageId].revisions[0]["*"]
.split("\n")
.filter(l => l.match(/\d{1,2}:\d{2}, \d{1,2} [a-zA-Z]+? \d{4} \(UTC\)/));
console.log(`Pings tool: Found ${sigParas.length} paragraphs with signatures`);
// Get last User: link in paragraph
let users = sigParas
.map(l =>
l
.match(/User:(.+?)\|/g)
?.at(-1)
?.replace("|", "")
)
.filter((u, i, a) => !!u && a.indexOf(u) === i) // dedupe
.sort((a, b) => a.localeCompare(b)) // sort alphabetically
.map(u => `@[[${u}|]]`) // map to ping list, using the pipe trick!
.join(", ");
console.log("Pings tool: Copied pings to clipboard:\n" + users);
navigator.clipboard.writeText(users).then(() => (e.target.style.color = "blue")); // write to clipboard
});
};
h.heading.appendChild(e);
});
}
});