Skip to content

Commit da04b05

Browse files
committed
Add ability to detect tabs opened from other applications
1 parent 5b0577f commit da04b05

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

webextensions/background/api-tabs-listener.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ function logUpdated(...args) {
5454
internalLogger('common/tabs-update', ...args);
5555
}
5656

57+
let mAppIsActive = false;
58+
5759
export function init() {
5860
browser.tabs.onActivated.addListener(onActivated);
5961
browser.tabs.onUpdated.addListener(onUpdated);
@@ -65,6 +67,10 @@ export function init() {
6567
browser.tabs.onDetached.addListener(onDetached);
6668
browser.windows.onCreated.addListener(onWindowCreated);
6769
browser.windows.onRemoved.addListener(onWindowRemoved);
70+
71+
browser.windows.getAll({}).then(windows => {
72+
mAppIsActive = windows.some(window => window.focused);
73+
});
6874
}
6975

7076
let mPromisedStartedResolver;
@@ -324,6 +330,7 @@ async function onNewTabTracked(tab, info) {
324330
const duplicatedInternally = window.duplicatingTabsCount > 0;
325331
const maybeOrphan = window.toBeOpenedOrphanTabs > 0;
326332
const activeTab = Tab.getActiveTab(window.id);
333+
const fromExternalApplication = !mAppIsActive;
327334

328335
// New tab's index can become invalid because the value of "index" is same to
329336
// the one given to browser.tabs.create() (new tab) or the original index
@@ -354,7 +361,8 @@ async function onNewTabTracked(tab, info) {
354361
positionedBySelf,
355362
mayBeReplacedWithContainer,
356363
maybeOrphan,
357-
activeTab
364+
activeTab,
365+
fromExternalApplication
358366
});
359367

360368
if (Tab.needToWaitTracked(tab.windowId, { exceptionTabId: tab.id }))
@@ -462,7 +470,8 @@ async function onNewTabTracked(tab, info) {
462470
restored,
463471
duplicated,
464472
duplicatedInternally,
465-
activeTab
473+
activeTab,
474+
fromExternalApplication
466475
});
467476
// don't do await if not needed, to process things synchronously
468477
if (moved instanceof Promise)
@@ -506,7 +515,8 @@ async function onNewTabTracked(tab, info) {
506515
duplicated,
507516
duplicatedInternally,
508517
originalTab: duplicated && Tab.get(uniqueId.originalTabId),
509-
treeForActionDetection
518+
treeForActionDetection,
519+
fromExternalApplication
510520
});
511521
tab.$TST.resolveOpened();
512522

@@ -967,3 +977,7 @@ async function onWindowRemoved(windowId) {
967977
}
968978
}
969979

980+
981+
browser.windows.onFocusChanged.addListener(windowId => {
982+
mAppIsActive = windowId > 0;
983+
});

0 commit comments

Comments
 (0)