Skip to content

Commit

Permalink
feat : adding alarms API (lingua-libre#97)
Browse files Browse the repository at this point in the history
* chore:added alarms permission

* feat : added alarm functionality

* refactor: Initialize UI with respect to alarms API

* style:better readabiity

* style : better readabiity

* revert : keeping the old approach for Firefox

* revert : keeping the old approach for Firefox

* chore : removed log messages
  • Loading branch information
kabir-afk authored Jun 19, 2024
1 parent 328cbef commit 6f4a994
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"storage",
"webRequest",
"webRequestBlocking",
"offscreen"
"alarms"
],
"content_scripts": [
{
Expand Down
28 changes: 17 additions & 11 deletions popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
})();
21 changes: 18 additions & 3 deletions sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
});

chrome.alarms.onAlarm.addListener((alarm)=>{
if (alarm.name === WAIT_ALARM) {
if (state === 'ready') {
chrome.runtime.sendMessage({state:'ready'});
chrome.alarms.clear(WAIT_ALARM);
}
}
})

0 comments on commit 6f4a994

Please sign in to comment.