Question

i am creating my first chrome application (browser action). i believe that background pages should be executing all the times in the background. For example if i write something like

window.setTimeout(getFeed, 30000);

This method should automatically be called every 30 seconds and refresh the feed data. But it seems that the method is only executed if i open the popup page of extension by clicking on extension's icon in the browser. Is it the desired behavior or am i missing something here.

Était-ce utile?

La solution

First, you want setInterval for periodic events, not setTimeout, which only fires once.

That said, the getFeed function in your background page might not have access to the elements you want it to update. A background page cannot access elements or variables in other contexts, e.g., tabs, popups, or content scripts. It might be necessary to use message passing between your background page and the page with the feed you wish to update.

On the extension page with the feed, set up a message listener using the chrome.extension.onRequest.addListener function, and in your background page, push out messages containing feed data using chrome.extension.sendRequest. You just need to write a function that updates the feed using the data caught by the message listener.

Without seeing more of your code I can't give you more concrete advice, but message passing is almost certainly one way to solve your problem.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top