From e17207a5f179a4d39eb4fa8a24526deed5e26884 Mon Sep 17 00:00:00 2001 From: Mitesh Shah Date: Sat, 12 Sep 2020 17:19:11 +0530 Subject: [PATCH] feat: multiple local lists --- background.js | 88 +++++++++++++++++++++++++++++++-------------------- lists/t1.json | 12 +++++++ lists/t2.json | 12 +++++++ manifest.json | 2 +- 4 files changed, 79 insertions(+), 35 deletions(-) create mode 100644 lists/t1.json create mode 100644 lists/t2.json diff --git a/background.js b/background.js index 376ffb8..b8c8274 100644 --- a/background.js +++ b/background.js @@ -1,54 +1,74 @@ -var DEFAULT_LIST_URL = 'https://cdn.jsdelivr.net/gh/nileshtrivedi/better/defaultlist.json' +var DEFAULT_LIST_URL = + "https://cdn.jsdelivr.net/gh/nileshtrivedi/better/defaultlist.json"; var BETTER_ALTERNATIVES = []; -chrome.runtime.onInstalled.addListener(function() { - console.log('onInstalled....'); +chrome.runtime.onInstalled.addListener(function () { + console.log("onInstalled...."); onStartup(); }); // fetch and save data when chrome restarted chrome.runtime.onStartup.addListener(() => { - console.log('onStartup....'); + console.log("onStartup...."); onStartup(); }); +function fetchAllLists(listUrls) { + var promises = listUrls.map((listUrl) => + fetch(listUrl) + .then((resp) => resp.json()) + .catch((e) => console.log("List errored out", e)) + ); -function onStartup(){ - chrome.storage.sync.get(['betterSourceURL'], function(result) { - console.log('Value currently is ' + (result.betterSourceURL || DEFAULT_LIST_URL)); - - var listUrl = result.betterSourceURL || DEFAULT_LIST_URL - // Uncomment this when testing list changes locally - listUrl = "/defaultlist.json" - fetch(listUrl) - .then(response => response.json()) - .then(data => { - console.log("Got data: ", data); - BETTER_ALTERNATIVES = data; - chrome.storage.local.set({betterSourceData: data}, function() { - console.log('Set betterSource = ' + data); - }) - }); + Promise.all(promises).then((results) => { + BETTER_ALTERNATIVES = results; + chrome.storage.local.set({ betterSourceData: results }, function () { + console.log("Set betterSource"); }); + }); } -function getMatch(url){ - var match = BETTER_ALTERNATIVES.find(pattern => url.match(new RegExp(pattern.urlPattern))); - if(match && match.alternatives) - return match; - else return null; +function onStartup() { + chrome.storage.sync.get(["betterSourceURL"], function (result) { + var listUrl = result.betterSourceURL || DEFAULT_LIST_URL; + // Uncomment this when testing list changes locally + // listUrl = "/defaultlist.json"; + fetchAllLists([listUrl, "/lists/t1.json", "/lists/t2.json"]); + }); +} + +function getMatch(url) { + var matches = BETTER_ALTERNATIVES.reduce((result, sourceList) => { + var match = sourceList.find((pattern) => + url.match(new RegExp(pattern.urlPattern)) + ); + if (match && match.alternatives) result.push(match); + return result; + }, []); + + if (matches.length) { + var combinedMatch = { + urlPattern: matches[0].urlPattern, + alternatives: matches.reduce( + (result, match) => result.concat(match.alternatives), + [] + ), + }; + return combinedMatch; + } + return null; } chrome.runtime.onMessage.addListener((msg, sender, response) => { switch (msg.type) { - case 'getMatch': - response(getMatch(msg.url)); - break; - case 'reloadList': - onStartup(); - break; - default: - response('unknown request'); - break; + case "getMatch": + response(getMatch(msg.url)); + break; + case "reloadList": + onStartup(); + break; + default: + response("unknown request"); + break; } }); \ No newline at end of file diff --git a/lists/t1.json b/lists/t1.json new file mode 100644 index 0000000..f99d73b --- /dev/null +++ b/lists/t1.json @@ -0,0 +1,12 @@ +[ + { + "urlPattern": "https://(www.)?miteshshah.com", + "alternatives": [ + { + "url": "https://oxal.org", + "name": "Mitesh", + "desc": "My other website" + } + ] + } +] diff --git a/lists/t2.json b/lists/t2.json new file mode 100644 index 0000000..2748e21 --- /dev/null +++ b/lists/t2.json @@ -0,0 +1,12 @@ +[ + { + "urlPattern": "https://(www.)?miteshshah.com", + "alternatives": [ + { + "url": "https://computableverse.com", + "name": "Miteshs Blog", + "desc": "My another tech blog" + } + ] + } +] diff --git a/manifest.json b/manifest.json index 9b3bc7d..5751808 100644 --- a/manifest.json +++ b/manifest.json @@ -27,5 +27,5 @@ "page": "options.html", "open_in_tab": false }, - "web_accessible_resources": ["defaultlist.json"] + "web_accessible_resources": ["defaultlist.json", "lists/*"] }