From 5d7cc33895a1410ed82943d7e44161e2b08dd592 Mon Sep 17 00:00:00 2001 From: Arsenii Izmailov <67082372+Turbocat6413@users.noreply.github.com> Date: Fri, 16 Sep 2022 02:31:32 +0300 Subject: [PATCH 1/2] Alarms instead of setInterval --- metadata/manifest.template.json | 5 +++-- metadata/manifest3.template.json | 3 ++- sources/background.js | 33 ++++++++++++++++++++++++++++---- sources/metabot.js | 16 ---------------- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/metadata/manifest.template.json b/metadata/manifest.template.json index d8e831e..daf8804 100755 --- a/metadata/manifest.template.json +++ b/metadata/manifest.template.json @@ -26,7 +26,7 @@ "background": { "page": "background.html", - "persistent": false + "persistent": true }, "web_accessible_resources": [ @@ -34,7 +34,8 @@ ], "permissions": [ - "storage" + "storage", + "alarms" ], // Firefox only diff --git a/metadata/manifest3.template.json b/metadata/manifest3.template.json index 1fc3b2a..d72afe9 100644 --- a/metadata/manifest3.template.json +++ b/metadata/manifest3.template.json @@ -38,7 +38,8 @@ }, "permissions": [ - "storage" + "storage", + "alarms" ], // Firefox only diff --git a/sources/background.js b/sources/background.js index c8219a2..b266a62 100644 --- a/sources/background.js +++ b/sources/background.js @@ -1,5 +1,8 @@ import { retrieveItemFromStorage } from './common_impex.js'; +chrome.alarms.create("updateLabelsAlarm", {periodInMinutes: 1}) + + function getStorageDataPromise(sKey) { return new Promise(function(resolve, reject) { @@ -167,15 +170,37 @@ chrome.runtime.onInstalled.addListener(function(details) { considerRefreshingJSON() }); +/* + chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) { if (request === 'getLabels') { considerRefreshingJSON() - var outcome; - outcome="Fetched" - sendResponse({updateResult: outcome}); - return true + var outcome; + outcome="Fetched" + sendResponse({updateResult: outcome}); + return true } }); + +*/ + + +function updateLabels() { + considerRefreshingJSON() + + var outcome; + outcome="Fetched"; + sendResponse({updateResult: outcome}); + + return true; +} + + +chrome.alarms.onAlarm.addListener(function(alarm) { + updateLabels(); +}); + +updateLabels(); \ No newline at end of file diff --git a/sources/metabot.js b/sources/metabot.js index 587eb7d..1096bd6 100755 --- a/sources/metabot.js +++ b/sources/metabot.js @@ -256,22 +256,6 @@ function markTweets() setTimeout(markTweets, 100); } -function requestLabels() -{ - // 1. Send the background a message requesting the user's data - chrome.runtime.sendMessage('getLabels', (response) => { - // 2. Got an asynchronous response with the data from the background -//// console.log('metabot received response: ', response); - }) - -////// Production-time delay: - setTimeout(requestLabels, 60*1000); - -////// Debug-time delay: -// setTimeout(requestLabels, 15*1000); -} - -requestLabels(); loadPrepackagedLabels(); From 6277a7706421fc9912cfaa7701aea096e8d3337f Mon Sep 17 00:00:00 2001 From: Arsenii Izmailov <67082372+Turbocat6413@users.noreply.github.com> Date: Fri, 16 Sep 2022 02:32:24 +0300 Subject: [PATCH 2/2] Revert "Alarms instead of setInterval" --- metadata/manifest.template.json | 5 ++--- metadata/manifest3.template.json | 3 +-- sources/background.js | 33 ++++---------------------------- sources/metabot.js | 16 ++++++++++++++++ 4 files changed, 23 insertions(+), 34 deletions(-) diff --git a/metadata/manifest.template.json b/metadata/manifest.template.json index daf8804..d8e831e 100755 --- a/metadata/manifest.template.json +++ b/metadata/manifest.template.json @@ -26,7 +26,7 @@ "background": { "page": "background.html", - "persistent": true + "persistent": false }, "web_accessible_resources": [ @@ -34,8 +34,7 @@ ], "permissions": [ - "storage", - "alarms" + "storage" ], // Firefox only diff --git a/metadata/manifest3.template.json b/metadata/manifest3.template.json index d72afe9..1fc3b2a 100644 --- a/metadata/manifest3.template.json +++ b/metadata/manifest3.template.json @@ -38,8 +38,7 @@ }, "permissions": [ - "storage", - "alarms" + "storage" ], // Firefox only diff --git a/sources/background.js b/sources/background.js index b266a62..c8219a2 100644 --- a/sources/background.js +++ b/sources/background.js @@ -1,8 +1,5 @@ import { retrieveItemFromStorage } from './common_impex.js'; -chrome.alarms.create("updateLabelsAlarm", {periodInMinutes: 1}) - - function getStorageDataPromise(sKey) { return new Promise(function(resolve, reject) { @@ -170,37 +167,15 @@ chrome.runtime.onInstalled.addListener(function(details) { considerRefreshingJSON() }); -/* - chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) { if (request === 'getLabels') { considerRefreshingJSON() - var outcome; - outcome="Fetched" - sendResponse({updateResult: outcome}); - return true + var outcome; + outcome="Fetched" + sendResponse({updateResult: outcome}); + return true } }); - -*/ - - -function updateLabels() { - considerRefreshingJSON() - - var outcome; - outcome="Fetched"; - sendResponse({updateResult: outcome}); - - return true; -} - - -chrome.alarms.onAlarm.addListener(function(alarm) { - updateLabels(); -}); - -updateLabels(); \ No newline at end of file diff --git a/sources/metabot.js b/sources/metabot.js index 1096bd6..587eb7d 100755 --- a/sources/metabot.js +++ b/sources/metabot.js @@ -256,6 +256,22 @@ function markTweets() setTimeout(markTweets, 100); } +function requestLabels() +{ + // 1. Send the background a message requesting the user's data + chrome.runtime.sendMessage('getLabels', (response) => { + // 2. Got an asynchronous response with the data from the background +//// console.log('metabot received response: ', response); + }) + +////// Production-time delay: + setTimeout(requestLabels, 60*1000); + +////// Debug-time delay: +// setTimeout(requestLabels, 15*1000); +} + +requestLabels(); loadPrepackagedLabels();