60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
function normalizeUrl(rawUrl) {
|
|
if (!rawUrl) {
|
|
return "";
|
|
}
|
|
|
|
if (/^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(rawUrl)) {
|
|
return rawUrl;
|
|
}
|
|
|
|
if (rawUrl.includes(" ") && !rawUrl.includes(".")) {
|
|
return `https://duckduckgo.com/?q=${encodeURIComponent(rawUrl)}`;
|
|
}
|
|
|
|
return `https://${rawUrl}`;
|
|
}
|
|
|
|
async function getActiveTab() {
|
|
const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
|
|
return tab;
|
|
}
|
|
|
|
document.getElementById("popup-url-form").addEventListener("submit", async (event) => {
|
|
event.preventDefault();
|
|
const input = document.getElementById("popup-url-input");
|
|
const value = input.value.trim();
|
|
|
|
if (!value) {
|
|
return;
|
|
}
|
|
|
|
await browser.runtime.sendMessage({ type: "open-url", url: normalizeUrl(value) });
|
|
window.close();
|
|
});
|
|
|
|
document.querySelectorAll("[data-action]").forEach((button) => {
|
|
button.addEventListener("click", async () => {
|
|
const action = button.dataset.action;
|
|
const tab = await getActiveTab();
|
|
|
|
if (!tab) {
|
|
return;
|
|
}
|
|
|
|
if (action === "back") {
|
|
await browser.tabs.goBack(tab.id).catch(() => {});
|
|
} else if (action === "forward") {
|
|
await browser.tabs.goForward(tab.id).catch(() => {});
|
|
} else if (action === "refresh") {
|
|
await browser.tabs.reload(tab.id);
|
|
} else if (action === "home") {
|
|
await browser.runtime.sendMessage({ type: "go-home" });
|
|
} else if (action === "trash") {
|
|
await browser.runtime.sendMessage({ type: "clear-session" });
|
|
} else if (action === "settings") {
|
|
await browser.runtime.sendMessage({ type: "open-settings" });
|
|
}
|
|
|
|
window.close();
|
|
});
|
|
});
|