open USO style settings even if the site tab is auto-activated
This commit is contained in:
parent
a7774c26fb
commit
4901e97f9f
|
@ -24,6 +24,11 @@ window.API_METHODS = Object.assign(window.API_METHODS || {}, {
|
||||||
openEditor,
|
openEditor,
|
||||||
updateIcon,
|
updateIcon,
|
||||||
|
|
||||||
|
// exposed for stuff that requires followup sendMessage() like popup::openSettings
|
||||||
|
// that would fail otherwise if another extension forced the tab to open
|
||||||
|
// in the foreground thus auto-closing the popup (in Chrome)
|
||||||
|
openURL,
|
||||||
|
|
||||||
closeTab: (msg, sender, respond) => {
|
closeTab: (msg, sender, respond) => {
|
||||||
chrome.tabs.remove(msg.tabId || sender.tab.id, () => {
|
chrome.tabs.remove(msg.tabId || sender.tab.id, () => {
|
||||||
if (chrome.runtime.lastError && msg.tabId !== sender.tab.id) {
|
if (chrome.runtime.lastError && msg.tabId !== sender.tab.id) {
|
||||||
|
|
|
@ -282,18 +282,28 @@ function getTabRealURL(tab) {
|
||||||
/**
|
/**
|
||||||
* Opens a tab or activates an existing one,
|
* Opens a tab or activates an existing one,
|
||||||
* reuses the New Tab page or about:blank if it's focused now
|
* reuses the New Tab page or about:blank if it's focused now
|
||||||
* @param {Object} params - or just a string e.g. openURL('foo')
|
* @param {Object} params
|
||||||
* @param {string} params.url - if relative, it's auto-expanded to the full extension URL
|
* or just a string e.g. openURL('foo')
|
||||||
* @param {number} [params.index] - move the tab to this index in the tab strip, -1 = last
|
* @param {string} params.url
|
||||||
* @param {Boolean} [params.active=tue] - true to activate the tab, false to open in background
|
* if relative, it's auto-expanded to the full extension URL
|
||||||
* @param {?Boolean} [params.currentWindow=true] - pass null to check all windows
|
* @param {number} [params.index]
|
||||||
* @returns {Promise}
|
* move the tab to this index in the tab strip, -1 = last
|
||||||
|
* @param {Boolean} [params.active]
|
||||||
|
* true to activate the tab (this is the default value in the extensions API),
|
||||||
|
* false to open in background
|
||||||
|
* @param {?Boolean} [params.currentWindow]
|
||||||
|
* pass null to check all windows
|
||||||
|
* @param {any} [params.message]
|
||||||
|
* JSONifiable data to be sent to the tab via sendMessage() repeatedly for 200ms
|
||||||
|
* until the remote end returns a non-undefined response
|
||||||
|
* @returns {Promise<Tab>} Promise that resolves to the opened/activated tab
|
||||||
*/
|
*/
|
||||||
function openURL({
|
function openURL({
|
||||||
url = arguments[0],
|
url = arguments[0],
|
||||||
index,
|
index,
|
||||||
active,
|
active,
|
||||||
currentWindow = true,
|
currentWindow = true,
|
||||||
|
message,
|
||||||
}) {
|
}) {
|
||||||
url = url.includes('://') ? url : chrome.runtime.getURL(url);
|
url = url.includes('://') ? url : chrome.runtime.getURL(url);
|
||||||
// [some] chromium forks don't handle their fake branded protocols
|
// [some] chromium forks don't handle their fake branded protocols
|
||||||
|
@ -305,7 +315,21 @@ function openURL({
|
||||||
FIREFOX && url.includes('%2F') ?
|
FIREFOX && url.includes('%2F') ?
|
||||||
url.replace(/%2F.*/, '*').replace(/#.*/, '') :
|
url.replace(/%2F.*/, '*').replace(/#.*/, '') :
|
||||||
url.replace(/#.*/, '');
|
url.replace(/#.*/, '');
|
||||||
return queryTabs({url: urlQuery, currentWindow}).then(maybeSwitch);
|
|
||||||
|
let task = queryTabs({url: urlQuery, currentWindow}).then(maybeSwitch);
|
||||||
|
|
||||||
|
if (message) {
|
||||||
|
task = task.then(function poll(tab, t0 = performance.now()) {
|
||||||
|
message.tabId = tab.id;
|
||||||
|
return sendMessage(message)
|
||||||
|
.then(ack => ack !== undefined ? tab : Promise.reject())
|
||||||
|
.catch(() => {
|
||||||
|
ignoreChromeError();
|
||||||
|
return performance.now() - t0 < 200 ? poll(tab) : tab;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return task;
|
||||||
|
|
||||||
function maybeSwitch(tabs = []) {
|
function maybeSwitch(tabs = []) {
|
||||||
const urlFF = FIREFOX && url.replace(/%2F/g, '/');
|
const urlFF = FIREFOX && url.replace(/%2F/g, '/');
|
||||||
|
|
|
@ -427,21 +427,11 @@ Object.assign(handleEvent, {
|
||||||
|
|
||||||
openURLandHide(event) {
|
openURLandHide(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const el = this;
|
|
||||||
const msg = tryJSONparse(el.dataset.sendMessage);
|
|
||||||
getActiveTab()
|
getActiveTab()
|
||||||
.then(activeTab => openURL({
|
.then(activeTab => API.openURL({
|
||||||
url: this.href || this.dataset.href,
|
url: this.href || this.dataset.href,
|
||||||
index: activeTab.index + 1,
|
index: activeTab.index + 1,
|
||||||
active: msg ? false : undefined,
|
message: tryJSONparse(this.dataset.sendMessage),
|
||||||
}))
|
|
||||||
.then(msg && (
|
|
||||||
function poll(tab, t0 = performance.now()) {
|
|
||||||
msg.tabId = tab.id;
|
|
||||||
return sendMessage(msg)
|
|
||||||
.catch(ignoreChromeError)
|
|
||||||
.then(handled => handled || performance.now() - t0 < 200 && poll(tab))
|
|
||||||
.then(() => chrome.tabs.update(tab.id, {active: true}));
|
|
||||||
}))
|
}))
|
||||||
.then(window.close);
|
.then(window.close);
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue
Block a user