Content deleted Content added
fetch more contributions when searching for talk pages if the page is reloaded |
improve user talk page fetching by using Special:PrefixIndex |
||
(One intermediate revision by the same user not shown) | |||
Line 57:
}
return uppercase ? ipString.toUpperCase() : ipString;
}
getRange() {
const size = this.version === 4 ? 32 : 128;
const effectiveMask = this.effectiveMask;
const hostBits = BigInt(size - effectiveMask);
const start = this.ip & (~0n << hostBits);
const end = start | ((1n << hostBits) - 1n);
return {
start: new IPAddress({ ip: start, mask: null, version: this.version }),
end: new IPAddress({ ip: end, mask: null, version: this.version })
};
}
inRange(other) {
if (!(other instanceof IPAddress)) return false;
if (this.version !== other.version) return false;
const { start, end } = this.getRange();
return other.ip >= start.ip && other.ip <= end.ip;
}
Line 295 ⟶ 314:
// display talk pages for IP range
async function displayRangeTalk(ip) {
async function getUserTalkPages(ip, maxPages = 32) {
const url = `/wiki/Special:Contributions/${ip}?limit=${reloaded ? 1000 : 250}`;▼
const userTalk = new Set();
const
const prefix = commonPrefix(start.toString(true), end.toString(true));
try {▼
const validPrefix = /^\w+[.:]\w+[.:]/.test(prefix);
const html = await fetch(url, { signal: abortController.signal }).then(res => res.text());▼
let pagesFetched = 0;
const doc = parser.parseFromString(html, 'text/html');▼
let errors = false;
const talkLinks = doc.querySelectorAll('.mw-contributions-list a.mw-usertoollinks-talk:not(.new)');▼
url = `/wiki/Special:PrefixIndex?prefix=${encodeURIComponent(prefix)}&namespace=3`;
const title = link.title;▼
}
if (title) userTalk.add(title);▼
while (url && pagesFetched < maxPages && !errors) {
▲ try {
const parser = new DOMParser();
const links = fetched.querySelectorAll('ul.mw-prefixindex-list > li > a');
for (const link of links) {
const ipText = link.textContent;
const pageIp = IPAddress.from(ipText);
if (pageIp && ip.inRange(pageIp)) {
userTalk.add(`User talk:${ipText}`);
}
}
const nextLink = fetched.querySelector('.mw-prefixindex-nav a');
if (nextLink && nextLink.textContent.includes('Next page') && nextLink.href) {
url = nextLink.href;
} else {
url = null;
}
} catch (error) {▼
console.error('Error fetching usertalk pages:', error);▼
errors = true;
break;
}
pagesFetched++;
▲ } catch (error) {
▲ console.error('Error fetching usertalk pages:', error);
}
if (!validPrefix || errors || pagesFetched === maxPages) {
try {
const html = await fetch(url).then(res => res.text());
const parser = new DOMParser();
const fetched = parser.parseFromString(html, 'text/html');
▲ const talkLinks =
for (const link of talkLinks) {
▲ const title = link.title;
▲ if (title) userTalk.add(title);
}
} catch (error) {
console.error('Error fetching usertalk pages:', error);
}
}
return Array.from(userTalk);
}
Line 695 ⟶ 749:
const index = ipList.findIndex(i => i.equals(ip));
if (index !== -1) ipList.splice(index, 1);
}
function commonPrefix(a, b) {
let i = 0;
while (i < a.length && i < b.length && a[i] === b[i]) {
i++;
}
return a.slice(0, i);
}
});
|