User:Chlod/Scripts/Deputy.js: Difference between revisions

Content deleted Content added
(bot/CD)
(bot/CD)
Line 351:
 
var version = "0.8.0";
var gitAbbrevHash = "208f52b5523baa";
var gitBranch = "main";
var gitDate = "Mon, 8 Jul 2024 0312:0436:1245 +0800";
var gitVersion = "0.8.0+g208f52bg5523baa";
 
/**
Line 6,528:
children.forEach(child => container.appendChild(child.cloneNode(true)));
return container;
}
 
/**
* What it says on the tin. Attempt to parse out a `title`, `diff`,
* or `oldid` from a URL. This is useful for converting diff URLs into actual
* diff information, and especially useful for {{copied}} templates.
*
* If diff parameters were not found (no `diff` or `oldid`), they will be `null`.
*
* @param url The URL to parse
* @return Parsed info: `diff` or `oldid` revision IDs, and/or the page title.
*/
function parseDiffUrl(url) {
if (typeof url === 'string') {
url = new URL(url);
}
// Attempt to get values from URL parameters (when using `/w/index.php?action=diff`)
let oldid = url.searchParams.get('oldid');
let diff = url.searchParams.get('diff');
let title = url.searchParams.get('title');
// Attempt to get information from this URL.
tryConvert: {
if (title && oldid && diff) {
// Skip if there's nothing else we need to get.
break tryConvert;
}
// Attempt to get values from Special:Diff short-link
const diffSpecialPageCheck =
// eslint-disable-next-line security/detect-unsafe-regex
/\/wiki\/Special:Diff\/(prev|next|\d+)(?:\/(prev|next|\d+))?/i.exec(url.pathname);
if (diffSpecialPageCheck != null) {
if (diffSpecialPageCheck[1] != null &&
diffSpecialPageCheck[2] == null) {
// Special:Diff/diff
diff = diffSpecialPageCheck[1];
}
else if (diffSpecialPageCheck[1] != null &&
diffSpecialPageCheck[2] != null) {
// Special:Diff/oldid/diff
oldid = diffSpecialPageCheck[1];
diff = diffSpecialPageCheck[2];
}
break tryConvert;
}
// Attempt to get values from Special:PermanentLink short-link
const permanentLinkCheck = /\/wiki\/Special:Perma(nent)?link\/(\d+)/i.exec(url.pathname);
if (permanentLinkCheck != null) {
oldid = permanentLinkCheck[2];
break tryConvert;
}
// Attempt to get values from article path with ?oldid or ?diff
// eslint-disable-next-line security/detect-non-literal-regexp
const articlePathRegex = new RegExp(mw.util.getUrl('(.*)'))
.exec(url.pathname);
if (articlePathRegex != null) {
title = decodeURIComponent(articlePathRegex[1]);
break tryConvert;
}
}
// Convert numbers to numbers
if (oldid != null && !isNaN(+oldid)) {
oldid = +oldid;
}
if (diff != null && !isNaN(+diff)) {
diff = +diff;
}
// Try to convert a page title
try {
title = new mw.Title(title).getPrefixedText();
}
catch (e) {
warn('Failed to normalize page title during diff URL conversion.');
}
return {
diff: diff,
oldid: oldid,
title: title
};
}
 
Line 6,781 ⟶ 6,859:
// Avoid enlisting if the anchor can't be found (invalid row).
if (anchor) {
rowElements[const anchorLinkTarget = parseDiffUrl(new mw.TitleURL(anchor.innerText).getPrefixedTextgetAttribute('href')], =window.___location.href)).title;
if (!anchorLinkTarget) li;{
warn('Could not parse target of anchor', anchor);
}
else {
rowElements[new mw.Title(anchorLinkTarget).getPrefixedText()] =
li;
}
}
}
Line 6,804 ⟶ 6,888:
else {
// Element somehow not in list. Just keep line as-is.
warn(`Could not find row element for "${csr.title.getPrefixedText()}"`);
rowElement = line;
}
Line 8,978 ⟶ 9,063:
}
return new InternalPageLatestRevisionGetButton(config);
}
 
/**
* What it says on the tin. Attempt to parse out a `title`, `diff`,
* or `oldid` from a URL. This is useful for converting diff URLs into actual
* diff information, and especially useful for {{copied}} templates.
*
* @param url The URL to parse
* @return Parsed info: `diff` or `oldid` revision IDs, and/or the page title.
*/
function parseDiffUrl(url) {
if (typeof url === 'string') {
url = new URL(url);
}
// Attempt to get values from URL parameters (when using `/w/index.php?action=diff`)
let oldid = url.searchParams.get('oldid');
let diff = url.searchParams.get('diff');
let title = url.searchParams.get('title');
// Attempt to get information from this URL.
tryConvert: {
if (title && oldid && diff) {
// Skip if there's nothing else we need to get.
break tryConvert;
}
// Attempt to get values from Special:Diff short-link
const diffSpecialPageCheck =
// eslint-disable-next-line security/detect-unsafe-regex
/\/wiki\/Special:Diff\/(prev|next|\d+)(?:\/(prev|next|\d+))?/i.exec(url.pathname);
if (diffSpecialPageCheck != null) {
if (diffSpecialPageCheck[1] != null &&
diffSpecialPageCheck[2] == null) {
// Special:Diff/diff
diff = diffSpecialPageCheck[1];
}
else if (diffSpecialPageCheck[1] != null &&
diffSpecialPageCheck[2] != null) {
// Special:Diff/oldid/diff
oldid = diffSpecialPageCheck[1];
diff = diffSpecialPageCheck[2];
}
break tryConvert;
}
// Attempt to get values from Special:PermanentLink short-link
const permanentLinkCheck = /\/wiki\/Special:Perma(nent)?link\/(\d+)/i.exec(url.pathname);
if (permanentLinkCheck != null) {
oldid = permanentLinkCheck[2];
break tryConvert;
}
// Attempt to get values from article path with ?oldid or ?diff
// eslint-disable-next-line security/detect-non-literal-regexp
const articlePathRegex = new RegExp(mw.util.getUrl('(.*)'))
.exec(url.pathname);
if (articlePathRegex != null) {
title = articlePathRegex[1];
break tryConvert;
}
}
// Convert numbers to numbers
if (oldid != null && !isNaN(+oldid)) {
oldid = +oldid;
}
if (diff != null && !isNaN(+diff)) {
diff = +diff;
}
// Try to convert a page title
try {
title = new mw.Title(title).getPrefixedText();
}
catch (e) {
warn('Failed to normalize page title during diff URL conversion.');
}
return {
diff: diff,
oldid: oldid,
title: title
};
}