Hey guys so I’m building an extension but I have a function that is heavy when running and it’s blocking the load of the page and I was wondering if it was possible to only run it after the page is fully loaded and interactive either in the script or in manifest.json
?
I currently have it inside a window.onload
but still blocks the interactiveness of the page.
The script:
JavaScript
x
19
19
1
async function getEAN() {
2
var EANIndex;
3
var body = document.body.innerText;
4
5
if ((EANIndex = body.indexOf('EAN')) !== -1) {
6
body = body.slice(EANIndex, EANIndex + 100);
7
const regexExpression = RegExp(/([^EAN]*$)*d{3}d{4,6}d{3,5}d/gm);
8
return body.match(regexExpression)[0]
9
}
10
return false
11
}
12
13
window.onload = function() {
14
if (window.location.pathname &&
15
location.hostname.indexOf(".google.com") !== -1) {
16
console.log(getEAN());
17
}
18
}
19
Would this be possible if yes how can I achieve it?
Advertisement
Answer
Since the content script itself is trivial, the problem is caused by catastrophic backtracking inside the regular expression, specifically ([^EAN]*$)*
which can match 0 characters at any place.
The solution is to use a look-behind condition:
JavaScript
1
2
1
/(?<=EANs*)d{11,15}/gm
2