Skip to content

Commit

Permalink
Fixes #16. Intercept _self anchor navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
basilevs committed Mar 1, 2020
1 parent 575a126 commit fa3f803
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 23 deletions.
83 changes: 62 additions & 21 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ const backgroundPage = browser.runtime.getBackgroundPage();

if (!backgroundPage)
throw new Error("Background page is not found");

const onBeforeRequest = browser.webRequest.onBeforeRequest;

if (!onBeforeRequest)
throw new Error("onBeforeRequest is not available");

function debug() {
//console.debug.apply(console, arguments);
Expand All @@ -32,6 +37,18 @@ function handleError() {
console.error.apply(console, message);
}

function wrapErrors(asyncFunction) {
function wrapper(...args) {
try {
return Promise.resolve(asyncFunction(...args)).catch(handleError);
} catch (e) {
handleError(e);
}
}
return wrapper;
}


function logFunction(name, ...args) {
debug(name, ...args);
}
Expand Down Expand Up @@ -78,22 +95,18 @@ const watchedTabs = {};

async function reopenIn(originTab, targetTab) {
logFunction("reopenIn", originTab, targetTab);
try {
await tabs.remove(originTab.id);
} catch (e) {
handleError("Failed to remove a tab: ", e);
return;
}
await tabs.remove(originTab.id);
await navigate(targetTab, originTab.url, originTab.active);
}

async function navigate(targetTab, url, activate) {
logFunction("navigate", targetTab, url, activate);
const updateRequest = {
active: originTab.active
active: activate
};
if (targetTab.url !== originTab.url)
updateRequest.url = originTab.url;
try {
await tabs.update(targetTab.id, updateRequest);
} catch (e) {
handleError("Failed to perform navigation", e);
}
if (targetTab.url !== url)
updateRequest.url = url;
await tabs.update(targetTab.id, updateRequest);
}

async function findDuplicate(tab) {
Expand Down Expand Up @@ -143,7 +156,7 @@ tabs.onCreated.addListener(tab => {
setTimeout(() => delete watchedTabs[tabId], 10000);
});

tabs.onUpdated.addListener((tabId, change, tab) => {
tabs.onUpdated.addListener(wrapErrors(async (tabId, change, tab) => {
if (!change.url)
return;
const watched = watchedTabs[tabId];
Expand All @@ -152,12 +165,40 @@ tabs.onUpdated.addListener((tabId, change, tab) => {
return;
}
logFunction("onUpdated", tabId, change, tab);
tryReuseTab(tab)
.then((wasReused) => {
if (wasReused) delete watchedTabs[tabId];
})
.catch(e => handleError(e))
});
const wasReused = await tryReuseTab(tab);
if (wasReused) delete watchedTabs[tabId];
}));

onBeforeRequest.addListener(wrapErrors(async requestDetails => {
const tabId = requestDetails.tabId;
if (tabId == -1) {
return;
}
const sourceTab = await tabs.get(tabId);
if (!sourceTab)
throw new Error(`Invalid tabId: ${tabId}`);
logFunction("onBeforeRequest", requestDetails, sourceTab);
const targetState = {
id: sourceTab.id,
openerTabId: sourceTab.openerTabId,
url: requestDetails.url,
pinned: sourceTab.pinned,
active: sourceTab.active,
hidden: sourceTab.hidden
};
const duplicate = await findDuplicate(targetState);
if (duplicate) {
if (watchedTabs[tabId]) {
await tabs.remove(tabId);
}
await navigate(duplicate, requestDetails.url, sourceTab.active);
debug(`Cancelling in-tab navigation to ${requestDetails.url}`);
return {
redirectUrl:"javascript:"
};
}
return {};
}), {urls: ["<all_urls>"], types: ["main_frame"]}, ["blocking"]);

getMatcher().catch(handleError);

Expand Down
7 changes: 5 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"name": "Reuse Tab",
"permissions": [
"tabs",
"storage"
"storage",
"webRequest",
"webRequestBlocking",
"<all_urls>"
],
"incognito": "not_allowed",
"background": {
Expand All @@ -14,7 +17,7 @@
"options_ui": {
"page": "options.html"
},
"version": "0.12",
"version": "1.0",
"author": "Vasili Gulevich",
"icons": {
"48": "icon.png"
Expand Down

0 comments on commit fa3f803

Please sign in to comment.