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;
browser.runtime.onMessage.addListener((message) => {
chrome.runtime.onMessage.addListener((message) => {
if (message.command === "switch_base_url") {
window.location =
message.baseUrl +

View File

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

View File

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