ChromeTimeout extension not working properly

My first post here =].

I am creating a chrome extension and I am using setTimeout recursively. I noticed that if I set it to 13 seconds, it will work, but if I set it to 14sec +, it will not work.

This is an example that is on my background.js

function start() { var timeout = setTimeout( function() { start(); }, 1000*15); alert('test'); } chrome.webNavigation.onCompleted.addListener(function(o) { start(); }, { url: [ {urlContains: 'http://www.example.com/in.php'}, {urlContains: 'http://www.example.com/out.php'} ] } ); 

If you reduce this timeout to 1000 * 13, it will work.

This is my manifest.json

 { "name": "Extension", "version": "0.0.7", "manifest_version": 2, "description": "Keeps proxy session alive", "homepage_url": "http://www.example.com", "icons": { "16": "icons/icon16-on.png", "48": "icons/icon48-on.png", "128": "icons/icon128-on.png" }, "default_locale": "en", "background": { "scripts": [ "src/bg/background.js" ], "persistent": false }, "browser_action": { "default_icon": "icons/icon19.png", "default_title": "Example - Off", "default_popup": "" }, "permissions": [ "webNavigation", "*://*/*", "https://*/*" ] } 

Any idea what might cause this weirdness? I am testing this in developer mode, BTW.

Thanks in advance!

EDIT

Fixed code:

manifest.json

I added alarms to permissions

background.js

Added this event to listen alarms.create:

 chrome.alarms.onAlarm.addListener(function(alarm){ start(); }); 

Replaced the setTimeout function with the following line

 chrome.alarms.create("Start", {periodInMinutes:1}); 

Hope this helps!

+6
source share
1 answer

I suspect that a problem may occur when the event pages automatically pause after a period of inactivity. On my machine, onSuspend seems to be called in ~ 10 seconds.

https://developer.chrome.com/extensions/event_pages#lifetime notes that

As soon as the event page is idle for a short time (several seconds), the runtime.onSuspend event is executed. There is a few seconds on the events page to process this event before it is forcibly unloaded.

So, it may take about 13 seconds before the page actually gets unloaded (giving you cleanup time in onSuspend, I reckon). Then your page will be unloaded, and the code initiated from it will no longer be launched.

https://developer.chrome.com/extensions/event_pages#transition says to use alarms api for event pages instead of setTimeout .

+12
source

Source: https://habr.com/ru/post/977164/


All Articles