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..ab2a96e 100644 --- a/popup/popup.js +++ b/popup/popup.js @@ -382,15 +382,21 @@ var browser = (browserType === 'firefox') ? browser : (browserType === 'chrome') ui = new UI(); ui.switchPanel( 'loaded' ); } - - let state = _backgroundPage.state; - function waitWhileLoading() { - if (state === 'ready' ) { - ui = new UI(); - } else { - setTimeout( waitWhileLoading, 100 ); - } - } - waitWhileLoading() - + if (browserType === "chrome") { + chrome.runtime.onMessage.addListener((message, sender) => { + if (message.state === "ready") { + ui = new UI(); + } + }); + } else { + let state = _backgroundPage.state; + function waitWhileLoading() { + if (state === "ready") { + ui = new UI(); + } else { + setTimeout(waitWhileLoading, 100); + } + } + waitWhileLoading(); + } })(); diff --git a/sw.js b/sw.js index 335bc3a..a71ac8d 100644 --- a/sw.js +++ b/sw.js @@ -769,11 +769,26 @@ 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 + }); +} + 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); + } + } +}) \ No newline at end of file