2

I want to run the chrome extension script for a URL that matches a specific Regex pattern, for example /https:\/\/(?!en).*\.wikipedia\.org\/wiki/ which should match all wikipedia URLs that are not in English. However using the regex in my filter results in an error when loading the extension:

Service worker registration failed

Uncaught TypeError: Could not add listener

Here is the code I am using:

const filter = { url: [ { urlMatches: "https://(?!en).*\.wikipedia\.org/wiki" } ] } chrome.webNavigation.onBeforeNavigate.addListener((details) => { console.info("test"); }, filter); 

Manifest.json:

{ "name": "Test", "description": "", "version": "1.0", "manifest_version": 3, "background": { "service_worker": "script.js" }, "permissions": [ "webNavigation" ] } 

I think there is something wrong with my regex (I do not get any error when providing a full URL), but I am not sure what.

How do I get this to work?

2

1 Answer 1

3

There are several problems: the regexp should start with ^, otherwise it'll match a maliciously crafted page with non-escaped section of a URL, .* should be [^/]* for the same reason, \. should be \\., and most importantly RE2 syntax used by webNavigation API doesn't support look-ahead/behind e.g. ?!, so you can't use them.

You can check the URL inside the listener:

chrome.webNavigation.onBeforeNavigate.addListener(details => { if (details.url.startsWith('https://en.wikipedia.org/')) { return; } console.info('test'); }, { url: [{ hostSuffix: '.wikipedia.org', pathPrefix: '/wiki' }], }); 

Alternatively, list all the languages explicitly in urlMatches like "^https://(www|fr|be|ja)\\.wikipedia\\.org/wiki/"

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.