Initial commit

This commit is contained in:
2021-07-13 23:11:58 +02:00
commit 96063abc3a
9 changed files with 228 additions and 0 deletions

View File

@@ -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;
}
});
})();

Binary file not shown.

After

Width:  |  Height:  |  Size: 541 B

24
manifest.json Normal file
View File

@@ -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
}
}

55
options/options.css Normal file
View File

@@ -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;
}

43
options/options.html Normal file
View File

@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="options.css" />
</head>
<body>
<form>
<div class="row">
<div class="col-25">
<label for="local">Local</label>
</div>
<div class="col-75">
<input type="text" id="local" placeholder="https://localhost:8080/">
</div>
</div>
<div class="row">
<div class="col-25">
<label for="test">Test</label>
</div>
<div class="col-75">
<input type="text" id="test" placeholder="https://test.mydomain.com/">
</div>
</div>
<div class="row">
<div class="col-25">
<label for="prod">Production</label>
</div>
<div class="col-75">
<input type="text" id="prod" placeholder="https://mydomain.com/">
</div>
</div>
<div class="row">
<input type="submit" value="Save">
</div>
</form>
<script src="options.js"></script>
</body>
</html>

26
options/options.js Normal file
View File

@@ -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);

15
popup/choose_url.css Normal file
View File

@@ -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;
}

18
popup/choose_url.html Normal file
View File

@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="choose_url.css" />
</head>
<body>
<div id="popup-content">
<div id="local" class="button" data-url="">Local</div>
<div id="test" class="button" data-url="">Test</div>
<div id="prod" class="button" data-url="">Production</div>
</div>
<script src="choose_url.js"></script>
</body>
</html>

31
popup/choose_url.js Normal file
View File

@@ -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);