object in place of webnavigation listener url filter won't work?

215 views
Skip to first unread message

Tyler Crews

unread,
Aug 10, 2021, 2:22:49 PM8/10/21
to Chromium Extensions
I'm having a hard time understanding why this won't work:

I'm using a listener on oncompleted to trigger when one of two urls is visited. 

chrome.webNavigation.onCompleted.addListener(details => {
    console.log('this function is doing things once you visit the website');
},
    { url: [{ hostContains: 'reddit.com' }, { hostContains: 'twitch.tv' }] }
);

this is functional code that will trigger correctly.

But the thing is is that I want these urls to be able to be changed by the user instead of being static.
When I replace the whole array of objects with a variable it works fine.
i.e.
if I use this code:
let urlFilterList = [{ hostContains: 'reddit.com' }, { hostContains: 'twitch.tv' }];
{url: urlFilterList}
then this is functional code that triggers correctly.

But I want to build is array of objects from a user-inputted list of strings.
Mapping and ForEach are giving me errors, so I built a quick function to do it myself - 

let urlInput= ['reddit.com', 'twitch.tv'];
let urlFilterFromScratch = [ ];
function mapObjectsToArray(arrIn, arrOut) {
    for (let i = 0; i < arrIn.length; i++) {
        let temp = { hostContains: arrIn[i] };
        arrOut.push(temp);
    }
};
mapObjectsToArray(urlInput, urlFilterFromScratch);

I now have a built array of length two that looks exactly like the web navigation array I was using before.
But when I apply it to the url filter section
{url: urlFilterFromScratch}
the code doesn't give an error, but instead acts like there's no url filter at all. Which means the onCompleted trigger triggers on any website that the user navigates to, instead of just the ones listed.

I've also tried to build an object containing the url: property filled with an array of the different properties, but when I put that in the place of the {url:} object the code threw an error and wouldn't register the service worker on upload.

Why is this? What can I do about it?
Is there something I'm doing wrong?
Or is this the wrong direction and I need to do something totally different?

Thanks for your time, 
Tyler

Simeon Vincent

unread,
Aug 10, 2021, 2:36:28 PM8/10/21
to Tyler Crews, Chromium Extensions
When I run the snippet you provided the function returns undefined; looks like you're missing a return statement. The below snippet should work as expected.

let urlInput= ['reddit.com', 'twitch.tv'];
let urlFilterFromScratch = [ ];
function mapObjectsToArray(arrIn, arrOut) {
for (let i = 0; i < arrIn.length; i++) {
let temp = { hostContains: arrIn[i] };
arrOut.push(temp);
}
return arrOut;
};
mapObjectsToArray(urlInput, urlFilterFromScratch);

Simeon - @dotproto
Chrome Extensions DevRel


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/27c99636-de86-4e2d-84b1-f0e3ebc4685an%40chromium.org.

Tyler Crews

unread,
Aug 10, 2021, 3:42:23 PM8/10/21
to Chromium Extensions, Simeon Vincent, Chromium Extensions, Tyler Crews
On further testing, I tried to set a variable equal to the results of calling the function with the return statement as suggested. 

Although the code runs without errors still (no issues in visual studio code), and creates an array with the desired objects (as shown in screencap),
with this method when you try to set the listener's urlfilter to the variable you get a service worker registration failure when you try to upload.

So unfortunately still not solved.
noredditfromscratch.png
Message has been deleted

wOxxOm

unread,
Aug 12, 2021, 11:25:07 AM8/12/21
to Chromium Extensions, tycr...@gmail.com, Simeon Vincent, Chromium Extensions
There was no need to add "return" because your function was changing the array's contents (in JavaScript the objects are passed by reference).

Without seeing your complete code I guess there may be two problems: a) you somehow registered a listener with an empty filter array so it acts like there's no filter , and b) if you use a separate function for the listener, it should be unregistered first, then registered with the new filter, because Chrome ignores the changed filter if addListener is called with the same function reference.
  1. Use a separate global function or just make sure the function's reference doesn't change if you know what it is.
  2. Call removeListener first
  3. Call addListener
function updateFilter(hosts) {
  chrome.webNavigation.onCompleted.removeListener(onCompleted);
  chrome.webNavigation.onCompleted.addListener(onCompleted, {
    // using hostSuffic with a leading '.' to match host.com & www.host.com,
    // but not ghost.com or host.com.whatever.org
    url: hosts.map(h => ({ hostSuffix: h.startsWith('.') ? h : '.' + h })),
  });
}

function onCompleted(details) {
  console.log('this function is doing things once you visit the website');
}
Reply all
Reply to author
Forward
0 new messages