aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorGravatar √Čtienne Deparis 2020-09-17 22:02:44 +0200
committerGravatar √Čtienne Deparis 2020-09-17 22:02:44 +0200
commit3f2436fef013b3bd0851829a7e8a5ae3da7b23a8 (patch)
treee20489f6598b0be6d206af65c26aaf7638bbb973
parentde688668fe0272b0be4bbc7b6a152a65a929344d (diff)
downloadsimple_hinting-3f2436fef013b3bd0851829a7e8a5ae3da7b23a8.tar.gz
simple_hinting-3f2436fef013b3bd0851829a7e8a5ae3da7b23a8.tar.xz
simple_hinting-3f2436fef013b3bd0851829a7e8a5ae3da7b23a8.zip
feat: Synchronize domains and params each weeks from upstream
-rw-r--r--js/background.js38
-rw-r--r--js/hints.js59
2 files changed, 47 insertions, 50 deletions
diff --git a/js/background.js b/js/background.js
index ee67900..f93032a 100644
--- a/js/background.js
+++ b/js/background.js
@@ -112,3 +112,41 @@ browser.commands.onCommand.addListener(function(command) {
});
}
});
+
+
+/**
+ * Sync domains and params info
+ */
+function onError(error) {
+ console.error(`[Simple Hinting extension] ${error}`);
+}
+
+function fetchUnwantedParams() {
+ return fetch("https://unshorten.umaneti.net/params").then(function(response) {
+ return response.json();
+ }).then(function(upstream_params) {
+ browser.storage.local.set({ "unwanted_params": upstream_params });
+ }).catch(onError);
+}
+
+function fetchTinyDomains() {
+ return fetch("https://unshorten.umaneti.net/domains").then(function(response) {
+ return response.json();
+ }).then(function(upstream_tiny_domains) {
+ browser.storage.local.set({ "tiny_domains_list": upstream_tiny_domains });
+ }).catch(onError);
+}
+
+let opts = ["sync_dt"];
+browser.storage.local.get(opts).then(function (result) {
+ let last_sync = result.sync_dt || 0;
+ let one_week_in_ms = 7 * 24 * 60 * 60 * 1000;
+ let now_in_ms = Date.now();
+ if ((last_sync + one_week_in_ms) < now_in_ms) {
+ // Time to sync again
+ console.log("[Simple Hinting extension] Synchronizing now params and domains.");
+ fetchTinyDomains();
+ fetchUnwantedParams();
+ browser.storage.local.set({ "sync_dt": now_in_ms });
+ }
+}, onError);
diff --git a/js/hints.js b/js/hints.js
index 7fe2daa..2ae273e 100644
--- a/js/hints.js
+++ b/js/hints.js
@@ -13,43 +13,6 @@ const actionkeys = {
"a": "cleanall"
};
-function onError(error) {
- let message = `[Simple Hinting extension] ${error}`;
- let pref_msg_status = document.getElementById("save-status");
- if (pref_msg_status) {
- pref_msg_status.textContent = message;
- pref_msg_status.style.color = "red";
- } else {
- console.error(message);
- }
-}
-
-var unwanted_params = [];
-function fetchUnwantedParams() {
- return fetch("https://unshorten.umaneti.net/params").then(
- function(response) {
- return response.json();
- }, onError
- ).then(
- function(upstream_params) {
- unwanted_params = upstream_params;
- browser.storage.local.set({ "unwanted_params": unwanted_params });
- }, onError
- );
-}
-
-var tiny_domains = [];
-function fetchTinyDomains() {
- return fetch("https://unshorten.umaneti.net/domains").then(
- function(response) {
- return response.json();
- }, onError
- ).then(function(upstream_tiny_domains) {
- tiny_domains = upstream_tiny_domains;
- browser.storage.local.set({ "tiny_domains_list": tiny_domains });
- }, onError);
-}
-
// Globals
function SimpleHinting () {
@@ -335,8 +298,9 @@ function is_command (key) {
let is_c = false;
try {
is_c = Object.keys(actionkeys).indexOf(key) !== -1;
- } catch (_e) {
- onError(`Failed reading key: ${key}`);
+ } catch {
+ let message = `[Simple Hinting extension] Failed reading key: ${key}`;
+ console.error(message);
}
return is_c;
}
@@ -409,28 +373,23 @@ function fix_all_links () {
}
+var unwanted_params = [];
+var tiny_domains = [];
const main_simple_hinting = new SimpleHinting();
function SimpleHintingManager(callback) {
let opts = ["unwanted_params", "tiny_domains_list"];
browser.storage.local.get(opts).then(function (result) {
- let initMethods = [];
if (result.unwanted_params && Array.isArray(result.unwanted_params)) {
unwanted_params = result.unwanted_params;
- } else {
- initMethods.push(fetchUnwantedParams());
}
if (result.tiny_domains_list && Array.isArray(result.tiny_domains_list)) {
tiny_domains = result.tiny_domains_list;
- } else {
- initMethods.push(fetchTinyDomains());
}
- if (initMethods.length > 0) {
- Promise.all(initMethods).then(callback);
- } else {
- callback.call(null);
- }
- }, onError);
+ callback.call(null);
+ }).catch(function (error) {
+ console.error(`[Simple Hinting extension] ${error}`);
+ });
}
browser.runtime.onMessage.addListener(function(data, sender) {