mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-27 13:29:34 +07:00
* 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 * Removed long list of suggestions Only show past history * Move host permissions * Rename extension and folder * REmove await * Move icons into images folder
29 lines
911 B
JavaScript
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;
|
|
}
|
|
});
|