Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions browser-extensions/gecko/background.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
const BtProtoPrefix = "x-bt://";
const ProtocolPage = browser.runtime.getURL("protocol_page.html");
const TabReturnDict = new Map();

async function openInBT(url) {
const destUrl = BtProtoPrefix + url;
const newTab = await browser.tabs.create({ url: destUrl });
// Gecko will no close x-bt:// tabs automatically
await browser.tabs.remove(newTab.id);
async function openInBT(url, returnTab) {
const destUrl = `${ProtocolPage}#${url}`;
const newTab = await browser.tabs.create({ url: destUrl, active: true, windowId: returnTab.windowId });

if (returnTab) {
TabReturnDict.set(newTab.id, {
returnTo: returnTab.id,
tabCount: (await browser.tabs.query({
windowId: returnTab.windowId
})).length - 1
});
}
}

browser.tabs.onRemoved.addListener(async (tabId, _) => {
if (TabReturnDict.has(tabId)) {
const returnTabId = TabReturnDict.get(tabId).returnTo;
const returnTab = await browser.tabs.get(returnTabId);

const currentTabs = (await browser.tabs.query({
windowId: returnTab.windowId
})).length;

if (currentTabs === TabReturnDict.get(tabId).tabCount) {
// If no new tab was opened, return to the original tab
await browser.tabs.update(TabReturnDict.get(tabId).returnTo, { active: true });
}
TabReturnDict.delete(tabId);
}
});

// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/browserAction/onClicked#addlistener_syntax
browser.browserAction.onClicked.addListener((activeTab) => {
browser.browserAction.onClicked.addListener(async (activeTab) => {
const url = activeTab.url;
// as we are sending the url to BT, we can close the current tab
browser.tabs.remove(activeTab.id);
await browser.tabs.remove(activeTab.id);

openInBT(url);
await openInBT(url);
});

browser.contextMenus.onClicked.addListener((info, tab) => {
browser.contextMenus.onClicked.addListener(async(info, tab) => {
const url = info.linkUrl;
openInBT(url);
await openInBT(url, tab);
});

// install context menu (
Expand Down
47 changes: 47 additions & 0 deletions browser-extensions/gecko/protocol_page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<!doctype html>

<html lang="en">

<head>
<title>Open a link in Browser Tamer. Or not.</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style>
#protocolAwaitingContainer .title {
color: red;
}

#text-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
</style>
</head>

<body>
<div xmlns="http://www.w3.org/1999/xhtml" id="errorPageContainer" class="container">
<div id="text-container">
<div id="protocolAwaitingContainer">
<div class="title">
<h1 class="title-text">Please confirm</h1>
</div>

<div class="description">
<div>
<p>Please confirm the usage of x-bt protocol for this extension</p>
<p>This is needed so the Browser Tamer extension could open links in Browser Tamer</p>
</div>
</div>
</div>
</div>

<script src="./protocol_page.js" type="module"></script>
</div>
</body>

</html>
14 changes: 14 additions & 0 deletions browser-extensions/gecko/protocol_page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const BtProtoPrefix = "x-bt://";
const url = window.location.hash.slice(1);

console.log(`Navigating to ${BtProtoPrefix + url}`);

window.location.href = BtProtoPrefix + url;

document.addEventListener("visibilitychange", () => {
// Close the tab on any focus change
// User confirmed protocol => page is visible => closed
// User canceled protocol => nothing happens => page remains open
// Protocol was confirmed permanently => page is visible => closed
window.close()
});