Files
amysteamdev 90249bc956 [New sample] Open Chrome API reference page (omnibox, alarms, messaging sample) (#848)
* First draft

* Rename folder

* Tweak comments

* Update index.html

* remove extra closing div

* Rename sw file

* Apply first round of @sebastianbenz suggestions

* Fix import

* Update popover

* Second tech review round
2024-08-21 13:53:13 +02:00

29 lines
911 B
JavaScript

console.log('sw-tips.js');
// Fetch tip & save in storage
const updateTip = async () => {
const response = await fetch('https://extension-tips.glitch.me/tips.json');
const tips = await response.json();
const randomIndex = Math.floor(Math.random() * tips.length);
await chrome.storage.local.set({ tip: tips[randomIndex] });
};
// Create a daily alarm and retrieves the first tip when extension is installed.
chrome.runtime.onInstalled.addListener(({ reason }) => {
if (reason === 'install') {
chrome.alarms.create({ delayInMinutes: 1, periodInMinutes: 1440 });
updateTip();
}
});
// Retrieve tip of the day
chrome.alarms.onAlarm.addListener(updateTip);
// Send tip to content script via messaging
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.greeting === 'tip') {
chrome.storage.local.get('tip').then(sendResponse);
return true;
}
});