From 96063abc3ad34a756de5317e15827d3e4009439c Mon Sep 17 00:00:00 2001 From: matteo Date: Tue, 13 Jul 2021 23:11:58 +0200 Subject: [PATCH] Initial commit --- content_scripts/base-url-switcher.js | 16 ++++++++ icons/base-url-switcher-96.png | Bin 0 -> 541 bytes manifest.json | 24 ++++++++++++ options/options.css | 55 +++++++++++++++++++++++++++ options/options.html | 43 +++++++++++++++++++++ options/options.js | 26 +++++++++++++ popup/choose_url.css | 15 ++++++++ popup/choose_url.html | 18 +++++++++ popup/choose_url.js | 31 +++++++++++++++ 9 files changed, 228 insertions(+) create mode 100644 content_scripts/base-url-switcher.js create mode 100644 icons/base-url-switcher-96.png create mode 100644 manifest.json create mode 100644 options/options.css create mode 100644 options/options.html create mode 100644 options/options.js create mode 100644 popup/choose_url.css create mode 100644 popup/choose_url.html create mode 100644 popup/choose_url.js diff --git a/content_scripts/base-url-switcher.js b/content_scripts/base-url-switcher.js new file mode 100644 index 0000000..93d748a --- /dev/null +++ b/content_scripts/base-url-switcher.js @@ -0,0 +1,16 @@ +(function () { + if (window.hasRun) { + return; + } + window.hasRun = true; + + browser.runtime.onMessage.addListener((message) => { + if (message.command === "switch_base_url") { + window.location = + message.baseUrl + + window.location.pathname + + window.location.search + + window.location.hash; + } + }); +})(); diff --git a/icons/base-url-switcher-96.png b/icons/base-url-switcher-96.png new file mode 100644 index 0000000000000000000000000000000000000000..89f5ac51e22e7380d16b8be28ed185e6976aa6f2 GIT binary patch literal 541 zcmeAS@N?(olHy`uVBq!ia0vp^2_VeD1|%QND7OGoY)RhkE)4%caKYZ?lNlHoFL}B+ zhE&XXd*`&@VFMA@i_EK{{_SwKdXg=n(DGxqfnrd|*NHd2f4lK6PDnZA_*cL9cg{>e zgFryxuJ_ZY%O^yBSzjR=ubkfamLtK9!I)`=;DMa!CV6i+uMTUqfBjetCfLJ}#$uq{ zAjxCLehGz?ETstglZE#BSoqKPjU*gfsdkInh_zy7sj>Gq-?=Z7-w zyan&i+%Nj()>oQ$fYo{0IgCYeY0fnfLi!ZnIaHboy7_us5# zIR408Zue`3b4UAUId5l-iulv9vxPmQ(p*HlK>W(d*b6=E4--^{A7y4NxWxIt~OHno!XZ))QWk9|D$v@*D^HCynS&c8Q7 zEOyg29jt88?Y570d(a%o`iw)h+umC1Tj + + + + + + + + +
+
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+
+ +
+
+ + + + + \ No newline at end of file diff --git a/options/options.js b/options/options.js new file mode 100644 index 0000000..f072ed9 --- /dev/null +++ b/options/options.js @@ -0,0 +1,26 @@ +function saveOptions(e) { + e.preventDefault(); + browser.storage.local.set({ + local: document.querySelector("#local").value, + test: document.querySelector("#test").value, + prod: document.querySelector("#prod").value, + }); +} + +function restoreOptions() { + function updateUI(restoredSettings) { + document.querySelector("#local").value = restoredSettings.local || ""; + document.querySelector("#test").value = restoredSettings.test || ""; + document.querySelector("#prod").value = restoredSettings.prod || ""; + } + + function onError(error) { + console.log(`Error: ${error}`); + } + + const gettingStoredSettings = browser.storage.local.get(); + gettingStoredSettings.then(updateUI, onError); +} + +document.addEventListener("DOMContentLoaded", restoreOptions); +document.querySelector("form").addEventListener("submit", saveOptions); diff --git a/popup/choose_url.css b/popup/choose_url.css new file mode 100644 index 0000000..97daefa --- /dev/null +++ b/popup/choose_url.css @@ -0,0 +1,15 @@ +html, body { + width: 300px; +} + +.button { + width: 100%; + padding: 4px; + font-size: 1.5em; + text-align: center; + cursor: pointer; +} + +.button:hover { + background-color: #CFF2F2; +} \ No newline at end of file diff --git a/popup/choose_url.html b/popup/choose_url.html new file mode 100644 index 0000000..03fcfc2 --- /dev/null +++ b/popup/choose_url.html @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/popup/choose_url.js b/popup/choose_url.js new file mode 100644 index 0000000..61d961b --- /dev/null +++ b/popup/choose_url.js @@ -0,0 +1,31 @@ +function listenForClicks() { + document.addEventListener("click", function (e) { + if (!e.target.classList.contains("button")) { + return; + } + + browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => { + browser.tabs.sendMessage(tabs[0].id, { + command: "switch_base_url", + baseUrl: e.target.getAttribute("data-url"), + }); + }); + }); +} + +function setUrls(settings) { + document.querySelector("#local").setAttribute("data-url", settings.local); + document.querySelector("#test").setAttribute("data-url", settings.test); + document.querySelector("#prod").setAttribute("data-url", settings.prod); +} + +function onError(e) { + console.error(e); +} + +const gettingStoredSettings = browser.storage.local.get(); +gettingStoredSettings.then(setUrls, onError); + +browser.tabs + .executeScript({ file: "/content_scripts/base-url-switcher.js" }) + .then(listenForClicks);