From c72db40f7dd676762e0ef7dc82e8722e98a52095 Mon Sep 17 00:00:00 2001 From: Dave Gallant Date: Sat, 8 Apr 2023 00:04:33 -0400 Subject: [PATCH] Add popup that allows modifying the config url --- extensions/chrome/background.js | 14 +------ extensions/chrome/manifest.json | 3 +- extensions/chrome/popup.css | 63 ++++++++++++++++++++++++++++++++ extensions/chrome/popup.html | 16 ++++++++ extensions/chrome/popup.js | 25 +++++++++++++ extensions/chrome/utils.js | 22 +++++++++++ extensions/firefox/background.js | 14 +------ extensions/firefox/content.js | 5 +-- extensions/firefox/manifest.json | 5 ++- extensions/firefox/popup.css | 63 ++++++++++++++++++++++++++++++++ extensions/firefox/popup.html | 16 ++++++++ extensions/firefox/popup.js | 25 +++++++++++++ extensions/firefox/utils.js | 22 +++++++++++ 13 files changed, 262 insertions(+), 31 deletions(-) create mode 100644 extensions/chrome/popup.css create mode 100644 extensions/chrome/popup.html create mode 100644 extensions/chrome/popup.js create mode 100644 extensions/chrome/utils.js create mode 100644 extensions/firefox/popup.css create mode 100644 extensions/firefox/popup.html create mode 100644 extensions/firefox/popup.js create mode 100644 extensions/firefox/utils.js diff --git a/extensions/chrome/background.js b/extensions/chrome/background.js index 58cd4de..6dd2238 100644 --- a/extensions/chrome/background.js +++ b/extensions/chrome/background.js @@ -1,18 +1,8 @@ -function updateRedirects() { - var URL = - "https://raw.githubusercontent.com/davegallant/rfd-redirect-stripper/main/redirects.json"; - - fetch(URL) - .then((res) => res.json()) - .then((res) => { - chrome.storage.local.set({ - "rfd-redirects": res, - }); - }); -} +importScripts('utils.js'); chrome.runtime.onInstalled.addListener(() => { updateRedirects(); + setDefaultConfig(); }); setInterval(updateRedirects, 1 * 60 * 60 * 1000); diff --git a/extensions/chrome/manifest.json b/extensions/chrome/manifest.json index e727c73..f3b11db 100644 --- a/extensions/chrome/manifest.json +++ b/extensions/chrome/manifest.json @@ -11,7 +11,8 @@ } ], "action": { - "default_icon": "icon.png" + "default_icon": "icon.png", + "default_popup": "popup.html" }, "background": { "service_worker": "background.js" diff --git a/extensions/chrome/popup.css b/extensions/chrome/popup.css new file mode 100644 index 0000000..fa7826a --- /dev/null +++ b/extensions/chrome/popup.css @@ -0,0 +1,63 @@ +body { + background-color: #282828; + color: #ebdbb2; + font-family: "Font Name", sans-serif; + font-size: 16px; +} + +input[type="text"] { + background-color: #3c3836; + color: #ebdbb2; + border: none; + border-radius: 3px; + padding: 5px; + margin-bottom: 10px; + font-size: 16px; +} + +button#save-button { + background-color: #1e7325; + color: #ebdbb2; + border: none; + border-radius: 3px; + padding: 5px 10px; + font-size: 16px; +} + +button#save-button:hover { + background-color: #83a598; +} + +button#save-button:active { + transform: translateY(1px); + box-shadow: none; +} + +button#save-button:disabled { + background-color: #a89984; + color: #928374; +} + +button#reset-button { + background-color: #458588; + color: #ebdbb2; + border: none; + border-radius: 3px; + padding: 5px 10px; + font-size: 16px; +} + +button#reset-button:hover { + background-color: #83a598; +} + +button#reset-button:active { + transform: translateY(1px); + box-shadow: none; +} + +button#reset-button:disabled { + background-color: #a89984; + color: #928374; +} + diff --git a/extensions/chrome/popup.html b/extensions/chrome/popup.html new file mode 100644 index 0000000..0bb82af --- /dev/null +++ b/extensions/chrome/popup.html @@ -0,0 +1,16 @@ + + + + + rfd-redirect-stripper + + + + + Config URL: + + + + + + diff --git a/extensions/chrome/popup.js b/extensions/chrome/popup.js new file mode 100644 index 0000000..0a77a29 --- /dev/null +++ b/extensions/chrome/popup.js @@ -0,0 +1,25 @@ +const inputField = document.getElementById("input-field"); +const saveButton = document.getElementById("save-button"); +const resetButton = document.getElementById("reset-button"); + +const defaultConfig = + "https://raw.githubusercontent.com/davegallant/rfd-redirect-stripper/main/redirects.json"; + +chrome.storage.local.get("config").then((result) => { + const value = result.config; + if (value) { + inputField.value = value; + } +}); + +saveButton.addEventListener("click", () => { + const value = inputField.value; + chrome.storage.local.set({ config: value }); + updateRedirects(); +}); + +resetButton.addEventListener("click", () => { + setDefaultConfig(); + inputField.value = defaultConfig; + updateRedirects(); +}); diff --git a/extensions/chrome/utils.js b/extensions/chrome/utils.js new file mode 100644 index 0000000..75699e0 --- /dev/null +++ b/extensions/chrome/utils.js @@ -0,0 +1,22 @@ +function updateRedirects() { + chrome.storage.local.get("config", function (URL) { + fetch(URL.config) + .then((res) => res.json()) + .then((res) => { + chrome.storage.local + .set({ + redirects: res, + }) + .catch((error) => { + console.log(error); + }); + }); + }); +} + +function setDefaultConfig() { + chrome.storage.local.set({ + config: + "https://raw.githubusercontent.com/davegallant/rfd-redirect-stripper/main/redirects.json", + }); +} diff --git a/extensions/firefox/background.js b/extensions/firefox/background.js index 78b6fd8..b64cec4 100644 --- a/extensions/firefox/background.js +++ b/extensions/firefox/background.js @@ -1,18 +1,6 @@ -function updateRedirects() { - var URL = - "https://raw.githubusercontent.com/davegallant/rfd-redirect-stripper/main/redirects.json"; - - fetch(URL) - .then((res) => res.json()) - .then((res) => { - browser.storage.local.set({ - "rfd-redirects": res, - }); - }); -} - chrome.runtime.onInstalled.addListener(() => { updateRedirects(); + setDefaultConfig(); }); setInterval(updateRedirects, 1 * 60 * 60 * 1000); diff --git a/extensions/firefox/content.js b/extensions/firefox/content.js index 8946a53..5f9661f 100644 --- a/extensions/firefox/content.js +++ b/extensions/firefox/content.js @@ -20,11 +20,10 @@ function stripRedirect(URL, redirectRegex) { function stripRedirects() { var Links = document.querySelectorAll("a.postlink, a.autolinker_link"); - browser.storage.local.get("rfd-redirects", function (redirectRegex) { + browser.storage.local.get("redirects", function (result) { Links.forEach(function (Link) { var ReferralURL = Link.href; - - Link.href = stripRedirect(ReferralURL, redirectRegex["rfd-redirects"]); + Link.href = stripRedirect(ReferralURL, result["redirects"]); }); }); } diff --git a/extensions/firefox/manifest.json b/extensions/firefox/manifest.json index 8bb31ca..6641c67 100644 --- a/extensions/firefox/manifest.json +++ b/extensions/firefox/manifest.json @@ -11,10 +11,11 @@ } ], "action": { - "default_icon": "icon.png" + "default_icon": "icon.png", + "default_popup": "popup.html" }, "background": { - "scripts": ["background.js"] + "scripts": ["utils.js", "background.js"] }, "permissions": ["scripting", "storage"], "host_permissions": ["*://forums.redflagdeals.com/*"] diff --git a/extensions/firefox/popup.css b/extensions/firefox/popup.css new file mode 100644 index 0000000..fa7826a --- /dev/null +++ b/extensions/firefox/popup.css @@ -0,0 +1,63 @@ +body { + background-color: #282828; + color: #ebdbb2; + font-family: "Font Name", sans-serif; + font-size: 16px; +} + +input[type="text"] { + background-color: #3c3836; + color: #ebdbb2; + border: none; + border-radius: 3px; + padding: 5px; + margin-bottom: 10px; + font-size: 16px; +} + +button#save-button { + background-color: #1e7325; + color: #ebdbb2; + border: none; + border-radius: 3px; + padding: 5px 10px; + font-size: 16px; +} + +button#save-button:hover { + background-color: #83a598; +} + +button#save-button:active { + transform: translateY(1px); + box-shadow: none; +} + +button#save-button:disabled { + background-color: #a89984; + color: #928374; +} + +button#reset-button { + background-color: #458588; + color: #ebdbb2; + border: none; + border-radius: 3px; + padding: 5px 10px; + font-size: 16px; +} + +button#reset-button:hover { + background-color: #83a598; +} + +button#reset-button:active { + transform: translateY(1px); + box-shadow: none; +} + +button#reset-button:disabled { + background-color: #a89984; + color: #928374; +} + diff --git a/extensions/firefox/popup.html b/extensions/firefox/popup.html new file mode 100644 index 0000000..0bb82af --- /dev/null +++ b/extensions/firefox/popup.html @@ -0,0 +1,16 @@ + + + + + rfd-redirect-stripper + + + + + Config URL: + + + + + + diff --git a/extensions/firefox/popup.js b/extensions/firefox/popup.js new file mode 100644 index 0000000..e8a1cc5 --- /dev/null +++ b/extensions/firefox/popup.js @@ -0,0 +1,25 @@ +const inputField = document.getElementById("input-field"); +const saveButton = document.getElementById("save-button"); +const resetButton = document.getElementById("reset-button"); + +const defaultConfig = + "https://raw.githubusercontent.com/davegallant/rfd-redirect-stripper/main/redirects.json"; + +browser.storage.local.get("config").then((result) => { + const value = result.config; + if (value) { + inputField.value = value; + } +}); + +saveButton.addEventListener("click", () => { + const value = inputField.value; + browser.storage.local.set({ config: value }); + updateRedirects(); +}); + +resetButton.addEventListener("click", () => { + setDefaultConfig(); + inputField.value = defaultConfig; + updateRedirects(); +}); diff --git a/extensions/firefox/utils.js b/extensions/firefox/utils.js new file mode 100644 index 0000000..4b70eff --- /dev/null +++ b/extensions/firefox/utils.js @@ -0,0 +1,22 @@ +function updateRedirects() { + browser.storage.local.get("config", function (URL) { + fetch(URL.config) + .then((res) => res.json()) + .then((res) => { + browser.storage.local + .set({ + redirects: res, + }) + .catch((error) => { + console.log(error); + }); + }); + }); +} + +function setDefaultConfig() { + browser.storage.local.set({ + config: + "https://raw.githubusercontent.com/davegallant/rfd-redirect-stripper/main/redirects.json", + }); +}