diff --git a/manifest.json b/manifest.json index 81797bd..46a9f54 100644 --- a/manifest.json +++ b/manifest.json @@ -17,7 +17,7 @@ "storage", "webRequest", "webRequestBlocking", - "offscreen" + "alarms" ], "content_scripts": [ { diff --git a/popup/popup.js b/popup/popup.js index f3224e0..bf32158 100644 --- a/popup/popup.js +++ b/popup/popup.js @@ -382,15 +382,10 @@ var browser = (browserType === 'firefox') ? browser : (browserType === 'chrome') ui = new UI(); ui.switchPanel( 'loaded' ); } - - let state = _backgroundPage.state; - function waitWhileLoading() { - if (state === 'ready' ) { + chrome.runtime.onMessage.addListener((message, sender) => { + console.log(sender); + if (message.state === "ready") { ui = new UI(); - } else { - setTimeout( waitWhileLoading, 100 ); } - } - waitWhileLoading() - + }); })(); diff --git a/sw.js b/sw.js index 335bc3a..8260491 100644 --- a/sw.js +++ b/sw.js @@ -769,11 +769,28 @@ async function setState(value) { // Run it main(); +const WAIT_ALARM = 'waitWhileLoading'; +async function createAlarm() { + await chrome.alarms.create(WAIT_ALARM, { + periodInMinutes: 1 / 60 // by specifying it as fraction we can run the ⏰ in every 1 second + },()=>console.log('⏰ running inside createAlarm()',new Date().getSeconds())); +} + chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { if (message.command === "getBackground") { - // persisted the state inside chrome.storage.local but will get to that later - console.log(state); const extensionData = {state,params,uiLanguages,records,signLanguages}; sendResponse(extensionData); + createAlarm(); } -}); \ No newline at end of file +}); + +chrome.alarms.onAlarm.addListener((alarm)=>{ + if (alarm.name === WAIT_ALARM) { + if (state === 'ready') { + chrome.runtime.sendMessage({state:'ready'}); + chrome.alarms.clear(WAIT_ALARM,()=>{ + console.log('⏰ stopped at ',new Date().getSeconds()); + }); + } + } +}) \ No newline at end of file