Skip to content Skip to sidebar Skip to footer

Running An Extension In Background On Page Load/refresh

I'm new to writing extensions (and javascript). I'd like an extension to run on a timer whenever a certain URL is opened/refreshed. Code is below. This is all I have so far from wh

Solution 1:

Move your logic to the Event Page.

Content Script doesn't have access to chrome.tabs API.

And according to best practices when using event pages,

Use event filters to restrict your event notifications to the cases you care about. For example, if you listen to the tabs.onUpdated event, try using the webNavigation.onCompleted event with filters instead (the tabs API does not support filters). That way, your event page will only be loaded for events that interest you.

Code snippets would look like

manifest.json

{"manifest_version":2,"name":"Test Plugin","description":"This extension is a test.","version":"1.0","background":{"scripts":["main.js"],"persistent":false},"permissions":["webNavigation"]}

main.js

chrome.webNavigation.onCompleted.addListener(function(details){
    console.log("Certain url with hostSuffix google.com has been loaded");
}, {url: [{hostSuffix: 'google.com'}]});

Post a Comment for "Running An Extension In Background On Page Load/refresh"