commit 96063abc3ad34a756de5317e15827d3e4009439c Author: matteo Date: Tue Jul 13 23:11:58 2021 +0200 Initial commit 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 0000000..89f5ac5 Binary files /dev/null and b/icons/base-url-switcher-96.png differ diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..20c1414 --- /dev/null +++ b/manifest.json @@ -0,0 +1,24 @@ +{ + "manifest_version": 2, + "name": "Base Url Switcher", + "version": "1.0", + + "description": "Allows for fast switching between different base urls with the same url parameters.", + "icons": { + "96": "icons/base-url-switcher-96.png" + }, + + "options_ui": { + "page": "options/options.html", + "browser_style": true + }, + + "permissions": ["activeTab", "storage"], + + "browser_action": { + "default_icon": "icons/base-url-switcher-96.png", + "default_title": "Base Url Switcher", + "default_popup": "popup/choose_url.html", + "browser_style": true + } +} diff --git a/options/options.css b/options/options.css new file mode 100644 index 0000000..0c2fdfc --- /dev/null +++ b/options/options.css @@ -0,0 +1,55 @@ +body { + padding: 20px; +} + +input[type="text"], +select, +textarea { + width: 100%; + padding: 10px; + border: 1px solid #ccc; + border-radius: 4px; + resize: vertical; +} + +label { + padding: 10px 10px 10px 0; + display: inline-block; +} + +input[type="submit"] { + background-color: #1e65ff; + color: white; + padding: 10px 20px; + border: none; + border-radius: 4px; + cursor: pointer; + float: right; +} + +input[type="submit"]:hover { + background-color: #3071ff; +} + +.col-25 { + float: left; + width: 25%; + margin-top: 6px; +} + +.col-75 { + float: left; + width: 75%; + margin-top: 6px; +} + +.row { + margin: 5px; +} + +/* Clear floats after the columns */ +.row:after { + content: ""; + display: table; + clear: both; +} diff --git a/options/options.html b/options/options.html new file mode 100644 index 0000000..6234f86 --- /dev/null +++ b/options/options.html @@ -0,0 +1,43 @@ + + + + + + + + + +
+
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+
+ +
+
+ + + + + \ 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);