Struggling with syntax for adding listener for visiting a URL

683 views
Skip to first unread message

Tyler Crews

unread,
Aug 4, 2021, 5:15:35 AM8/4/21
to Chromium Extensions
Hi all, sorry if this is a novice question, but I'm fairly new at this and couldn't find a similarly answered question on SO or previous threads--

basically I'm trying to add a listener for when a specific url is visited.
Specifically I'm trying to get a listener initiate a function that will make a modification to the 'enabled' property of a ruleset in declarative_net_requests in my manifest, but I'm working through things step by step.

after looking through the documentation for events and runtime and whatnot I found one that looks like it might work for me. 
in webNavigation there's an onCompleted event that seems to be what I want. After the url has successfully been accessed it should trigger - 

in the listener function properties it says you can add details to specify the url, but I can't quite understand the syntax of how to do that correctly.
My current code is below, but even when I omit the details object ({url:...} in the code below) I'm getting an error when trying to upload my extension because registering my service worker is failing. 

chrome.webNavigation.onCompleted.addListener(
    function(){
        console.log('beep boop');
        alert('beep boop');
    },
    { url: '*://*.google.com/*' });

so the url is a placeholder for now, but just to test how the listeners work I was trying to get an alert popup to occur whenever I visit the named url.

Am I on the right track here? 
Thanks for your help
Message has been deleted

Tyler Crews

unread,
Aug 4, 2021, 2:01:47 PM8/4/21
to Chromium Extensions, Tyler Crews
in my example I guess I was inappropriately applying some filtered events syntax that I saw here - https://developer.chrome.com/docs/extensions/reference/events/#filtered

chrome.webNavigation.onCompleted.addListener(function (details) {
    if (details.url === '*://*.google.com/*') {
        alert('okay did this happen');
        console.log('testing 1 2');
    }
});

now i'm trying to pass the details into the function now but the service worker still won't register on upload... do I need to be using these filtered event methods like hasHostSuffix instead?

wOxxOm

unread,
Aug 4, 2021, 3:03:23 PM8/4/21
to Chromium Extensions, tycr...@gmail.com
webNavigation uses a different syntax of the filter. You can see it in the link you posted, slightly further down the page:
https://developer.chrome.com/docs/extensions/reference/events/#type-UrlFilter

Your example would look like this:

chrome.webNavigation.onCompleted.addListener(details => {
  console.log('gotcha');
}, {
  url: [
    {hostSuffix: '.google.com'},
  ]
});

Tyler Crews

unread,
Aug 4, 2021, 4:32:25 PM8/4/21
to Chromium Extensions, wOxxOm, Tyler Crews
alright!!
Thanks for your help, woxx, yeah using those keywords does the trick. 

ended up using another one - 
chrome.webNavigation.onCompleted.addListener(details => {
    console.log('gotcha');
    alert('hey there');
}, {
    url: [{ hostContains: '.google' },]
});

also for posterity if anyone looking for similar issues reads this, make sure you remember to declare webNavigation in your manifest permissions.
Reply all
Reply to author
Forward
0 new messages