Well firstly I would adjust that while loop so that it doesn’t do variable assignment in the condition area.
let node = walker.nextNode();
while (node) {
// CODE FOR COPYING SPECIAL PAGE NAMES TO CLIPBOARD COMES HERE
...
node = walker.nextNode();
}
}
With the regex, JavaScript doesn’t support using \K. Instead we use capture groups.
const regex = /Special:([a-zA-Z0-9]*)/
Then I would use that loop for populating an array of special lines.
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
const specialLines = [];
const regex = /Special:([a-zA-Z0-9]*)/
let node = walker.nextNode();
while (node) {
if (regex.test(node.textContent)) {
specialLines.push(node.textContent);
}
node = walker.nextNode();
}
Beyond there, we use map to get the capture group of the regex.
const specialTerms = specialLines.map(function getTerm(line) {
return line.match(regex)[1];
});
I could have done that in the while loop, by by doing it outside of the loop we reduce the complexity of the code and make it easier to understand.
Then it’s just a matter of copying that specialTerms array to the clipboard, with a similar output to console.log in case writing to the clipboard doesn’t work.
if (navigator && navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(specialTerms.join(" "));
}
console.log(specialTerms.join(" "));
Here’s the full code.
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
const specialLines = [];
const regex = /Special:([a-zA-Z0-9]*)/
let node = walker.nextNode();
while (node) {
if (regex.test(node.textContent)) {
specialLines.push(node.textContent);
}
node = walker.nextNode();
}
const specialTerms = specialLines.map(function getTerm(line) {
return line.match(regex)[1];
});
if (navigator && navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(specialTerms.join(", "));
}
console.log(specialTerms.join(" "));
All of that could be done inside of the while loop, but doing it the way I’ve done above helps to reduce complexity of what’s going on.