From 1c4e6ffcfb874e048f455239941802cd3673ee74 Mon Sep 17 00:00:00 2001 From: Mitesh Date: Sat, 12 Sep 2020 23:58:30 +0530 Subject: [PATCH] feat: options to add, edit, remove source lists (#36) * feat: options to add, edit, remove source lists * fix: failure recovery even if one or more sources fail --- background.js | 28 +++++++++++-------- options.html | 76 +++++++++++++++++++++++++++++---------------------- options.js | 68 +++++++++++++++++++++++++++++++++++++-------- 3 files changed, 116 insertions(+), 56 deletions(-) diff --git a/background.js b/background.js index b8c8274..a5f83fe 100644 --- a/background.js +++ b/background.js @@ -1,4 +1,4 @@ -var DEFAULT_LIST_URL = +let DEFAULT_LIST_URL = "https://cdn.jsdelivr.net/gh/nileshtrivedi/better/defaultlist.json"; var BETTER_ALTERNATIVES = []; @@ -14,14 +14,14 @@ chrome.runtime.onStartup.addListener(() => { }); function fetchAllLists(listUrls) { - var promises = listUrls.map((listUrl) => - fetch(listUrl) - .then((resp) => resp.json()) - .catch((e) => console.log("List errored out", e)) + var promises = listUrls.map( + (listUrl) => fetch(listUrl).then((resp) => resp.json()) + // .catch((e) => console.log("List errored out", e)) ); - Promise.all(promises).then((results) => { - BETTER_ALTERNATIVES = results; + // wait for all requests to complete, ignore errors + Promise.all(promises.map((p) => p.catch((e) => null))).then((results) => { + BETTER_ALTERNATIVES = results.filter((item) => item != null); chrome.storage.local.set({ betterSourceData: results }, function () { console.log("Set betterSource"); }); @@ -29,11 +29,15 @@ function fetchAllLists(listUrls) { } 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"]); + chrome.storage.sync.get(["betterSourceUrls"], function (result) { + let betterSourceUrls = []; + if (result && result.betterSourceUrls) { + betterSourceUrls = JSON.parse(result.betterSourceUrls); + } + if (betterSourceUrls.length === 0) { + betterSourceUrls = [DEFAULT_LIST_URL]; + } + fetchAllLists(betterSourceUrls); }); } diff --git a/options.html b/options.html index be2347c..24d72f4 100644 --- a/options.html +++ b/options.html @@ -1,39 +1,49 @@ - - - - -
-

Choose a different source of suggestions!

- - - + h1, + h2, + h3 { + line-height: 1.2 + } + + input[type=url] { + width: 100%; + padding: 12px 20px; + margin: 8px 0; + box-sizing: border-box; + } + + button { + padding: 5px 5px; + font-size: 16px; + } + + + + +
+

Choose a different source of suggestions!

+ +
- - + + + +
+

To delete a source: empty the field, hit save, and refresh the page.

+ + + \ No newline at end of file diff --git a/options.js b/options.js index 70dcf0b..fb71f16 100644 --- a/options.js +++ b/options.js @@ -1,14 +1,60 @@ -let input = document.getElementById('betterSourceText'); -let submit = document.getElementById('betterSourceSubmit'); +let DEFAULT_LIST_URL = + "https://cdn.jsdelivr.net/gh/nileshtrivedi/better/defaultlist.json"; +let addSourceButton = document.getElementById("addSourceButton"); +let submit = document.getElementById("betterSourceSubmit"); +let betterSourceInputContainer = document.getElementById( + "betterSourceInputContainer" +); -submit.addEventListener('click', function() { - chrome.storage.sync.set({betterSourceURL: input.value}, function() { - console.log('Set betterSource = ' + input.value); - }); +function storePrefs(sourceUrls) { + sourceUrls = sourceUrls.filter((url) => url != ""); + let prefs = { + betterSourceUrls: JSON.stringify(sourceUrls), + }; + chrome.storage.sync.set(prefs, function () { + console.log("Saved", prefs); + }); +} - chrome.runtime.sendMessage({type: 'reloadList', url: input.value}, (response) => { - if (response) { - console.log("BETTER_ALTERNATIVES list is reloaded"); - } - }); +function addUrlInput(value) { + let newInput = document.createElement("input"); + newInput.setAttribute("name", "url[]"); + newInput.setAttribute("type", "url"); + newInput.setAttribute("placeholder", "https://source.com/list.json"); + if (value) { + newInput.setAttribute("value", value); + } + betterSourceInputContainer.appendChild(newInput); +} + +addSourceButton.addEventListener("click", function (e) { + e.preventDefault(); + addUrlInput(null); +}); + +submit.addEventListener("click", function () { + let $sourceUrls = document.querySelectorAll("[name='url[]']"); + let sourceUrls = Array.from($sourceUrls).map((elem) => elem.value); + storePrefs(sourceUrls); + + chrome.runtime.sendMessage({ type: "reloadList" }, (response) => { + if (response) { + console.log("BETTER_ALTERNATIVES list is reloaded"); + } + }); +}); + +chrome.storage.sync.get(["betterSourceUrls"], (result) => { + // populate input fields from stored sources or show default source + let betterSourceUrls = []; + if (result && result.betterSourceUrls) { + betterSourceUrls = JSON.parse(result.betterSourceUrls); + } + if (betterSourceUrls.length === 0) { + betterSourceUrls = [DEFAULT_LIST_URL]; + } + + betterSourceUrls.map((sourceUrl) => { + addUrlInput(sourceUrl); + }); });