Skip to content

Commit

Permalink
Simplify #18
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Nov 24, 2017
1 parent ba13d40 commit 2df48ed
Showing 1 changed file with 31 additions and 58 deletions.
89 changes: 31 additions & 58 deletions webextensions/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,35 @@ browser.windows.onRemoved.addListener(aWindowId => {
}
});

function tryRedirectToNewTab(aDetails) {
var tab = gTabs[aDetails.tabId];
log('tab ', tab);
var url = tab.previousUrl || tab.url;
if (configs.recycleBlankCurrentTab) {
if (url == 'about:blank' ||
(new RegExp(configs.recycleTabUrlPattern)).test(url))
return false;
}

// don't open new tab for in-page jump
if (url.split('#')[0] == aDetails.url.split('#')[0])
return false;

var newTabParams = {
active: true,
url: aDetails.url
};
var origin = extractOriginPart(aDetails.url);
if (origin && extractOriginPart(url)) {
if (!configs.newTabForSameOrigin)
return false;
if (configs.openAsChildIfSameOrigin)
newTabParams.openerTabId = aDetails.tabId;
}
browser.tabs.create(newTabParams);
return true;
}

browser.webRequest.onBeforeRequest.addListener(
aDetails => {
if (!configs.allowBlockRequest ||
Expand All @@ -57,33 +86,7 @@ browser.webRequest.onBeforeRequest.addListener(

log('onBeforeRequest loading on existing tab', aDetails);

var tab = gTabs[aDetails.tabId];
log('tab ', tab);
var url = tab.previousUrl || tab.url;
if (configs.recycleBlankCurrentTab) {
if (url == 'about:blank' ||
(new RegExp(configs.recycleTabUrlPattern)).test(url))
return { cancel: false };
}

// don't open new tab for in-page jump
if (url.split('#')[0] == aDetails.url.split('#')[0])
return { cancel: false };

var newTabParams = {
active: true,
url: aDetails.url
};
let origin = extractOriginPart(aDetails.url);
if (origin && extractOriginPart(url)) {
if (!configs.newTabForSameOrigin)
return { cancel: false };
if (configs.openAsChildIfSameOrigin)
newTabParams.openerTabId = aDetails.tabId;
}
browser.tabs.create(newTabParams);

return { cancel: true };
return { cancel: tryRedirectToNewTab(aDetails) };
},
{ urls: ['<all_urls>'] },
['blocking']
Expand All @@ -99,37 +102,7 @@ browser.webNavigation.onCommitted.addListener(
aDetails.transitionType != 'generated' /* search result */)
return;

var tab = gTabs[aDetails.tabId];
log('tab ', tab);
var url = tab.previousUrl || tab.url;
if (configs.recycleBlankCurrentTab) {
if (url == 'about:blank' ||
(new RegExp(configs.recycleTabUrlPattern)).test(url))
return;
}

// don't open new tab for in-page jump
if (url.split('#')[0] == aDetails.url.split('#')[0])
return;

var newTabParams = {
active: true,
url: aDetails.url
};
let origin = extractOriginPart(aDetails.url);
if (origin && extractOriginPart(url)) {
if (!configs.newTabForSameOrigin)
return;
if (configs.openAsChildIfSameOrigin)
newTabParams.openerTabId = aDetails.tabId;
}

browser.tabs.executeScript(aDetails.tabId, {
code: 'history.back()',
runAt: 'document_start'
}).then(() => {
browser.tabs.create(newTabParams);
});
tryRedirectToNewTab(aDetails);
}
);

Expand Down

0 comments on commit 2df48ed

Please sign in to comment.