Home > Mobile >  Contents.js blocking page load chrome extension
Contents.js blocking page load chrome extension

Time:09-18

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:

async function getEAN() {
    var EANIndex;
    var body = document.body.innerText;

    if ((EANIndex = body.indexOf('EAN')) !== -1) {
        body = body.slice(EANIndex, EANIndex   100);
        const regexExpression = RegExp(/([^EAN]*$)*\d{3}\d{4,6}\d{3,5}\d/gm);
        return body.match(regexExpression)[0]
    }
    return false
}

window.onload = function() {
    if (window.location.pathname &&
        location.hostname.indexOf(".google.com") !== -1) {
        console.log(getEAN());
    }
}

Would this be possible if yes how can I achieve it?

CodePudding user response:

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:

/(?<=EAN\s*)\d{11,15}/gm
  • Related