I have a userscript (Tampermonkey in Chrome) which runs either when the page is opened by the user, or which may open the page in a separate window (where the concerned script then runs). When the page is opened by the user, everything works as expected.
However, when the page is opened in another window, this function sometimes does not process the text as expected.
Code:
function processTableRows() {
var regex = new RegExp(/(.*?)(\d{7})/);
var texts = document.querySelectorAll(".table__row");
var out = [];
texts.forEach(text=> {
var fixMonth = text.innerText.replace(/(\d*)月/, function(p1) {
const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
return monthNames[(parseInt(p1) - 1)];
}).replace(regex,
"example.com/page.html?id=$2\n$1");
out.push(fixMonth);
});
return out;
}
Example page text (before script):
05 6月 2019 8989898
Some text
Some more text
When the user opens the page, both the userscript and the code in the console give the expected output:
example.com/page.html?id=8989898
05 Jun 2019
Some text
Some more text
However, when it is launched in a separate Window, it returns (Both TM script and console run):
05 Jun 2019
example.com/page.html?id=8989898
Some text
Some more text
I can't imagine for the life of me why the second replace()
seems to be functioning differently in a separate window. I thought it may be related to some of JavaScript's regex funkiness, e.g. moving an index, affecting the chained call, but MDN indicates it only returns a string.
EDIT: Further information obtained after the comments that were below:
When the page is launched in a new (popup) window, it is small and the original text renders like:
05 6月 2019
8989898
Some text
Some more text
However, when the window is larger (as when the user visits it), it looks like:
05 6月 2019 8989898
Some text
Some more text