Add support for chrome browsers

This commit is contained in:
2021-07-14 22:01:41 +02:00
parent 96063abc3a
commit 34327b8775
3 changed files with 12 additions and 20 deletions

View File

@@ -4,7 +4,7 @@
} }
window.hasRun = true; window.hasRun = true;
browser.runtime.onMessage.addListener((message) => { chrome.runtime.onMessage.addListener((message) => {
if (message.command === "switch_base_url") { if (message.command === "switch_base_url") {
window.location = window.location =
message.baseUrl + message.baseUrl +

View File

@@ -1,6 +1,6 @@
function saveOptions(e) { function saveOptions(e) {
e.preventDefault(); e.preventDefault();
browser.storage.local.set({ chrome.storage.local.set({
local: document.querySelector("#local").value, local: document.querySelector("#local").value,
test: document.querySelector("#test").value, test: document.querySelector("#test").value,
prod: document.querySelector("#prod").value, prod: document.querySelector("#prod").value,
@@ -14,12 +14,7 @@ function restoreOptions() {
document.querySelector("#prod").value = restoredSettings.prod || ""; document.querySelector("#prod").value = restoredSettings.prod || "";
} }
function onError(error) { chrome.storage.local.get(null, updateUI);
console.log(`Error: ${error}`);
}
const gettingStoredSettings = browser.storage.local.get();
gettingStoredSettings.then(updateUI, onError);
} }
document.addEventListener("DOMContentLoaded", restoreOptions); document.addEventListener("DOMContentLoaded", restoreOptions);

View File

@@ -1,11 +1,12 @@
function listenForClicks() { async function listenForClicks() {
document.addEventListener("click", function (e) { document.addEventListener("click", function (e) {
if (!e.target.classList.contains("button")) { if (!e.target.classList.contains("button")) {
return; return;
} }
browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => { let queryOptions = { active: true, currentWindow: true };
browser.tabs.sendMessage(tabs[0].id, { chrome.tabs.query(queryOptions, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, {
command: "switch_base_url", command: "switch_base_url",
baseUrl: e.target.getAttribute("data-url"), baseUrl: e.target.getAttribute("data-url"),
}); });
@@ -19,13 +20,9 @@ function setUrls(settings) {
document.querySelector("#prod").setAttribute("data-url", settings.prod); document.querySelector("#prod").setAttribute("data-url", settings.prod);
} }
function onError(e) { chrome.storage.local.get(null, setUrls);
console.error(e);
}
const gettingStoredSettings = browser.storage.local.get(); chrome.tabs.executeScript(
gettingStoredSettings.then(setUrls, onError); { file: "/content_scripts/base-url-switcher.js" },
listenForClicks
browser.tabs );
.executeScript({ file: "/content_scripts/base-url-switcher.js" })
.then(listenForClicks);