Skip to content

Commit 7a422a6

Browse files
committed
Track frames
1 parent bd57a1a commit 7a422a6

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

source/active-tab.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ export type ActiveTab = {
1111

1212
export const possiblyActiveTabs = new Map<TabId, Origin>();
1313

14-
const emitter = new SimpleEventTarget<ActiveTab>();
14+
const newActiveTabs = new SimpleEventTarget<ActiveTab>();
1515

1616
const browserAction = chrome.action ?? chrome.browserAction;
1717

1818
function listener({url, id}: chrome.tabs.Tab): void {
19-
if (id && url && isScriptableUrl(url)) {
19+
if (id && url &&!possiblyActiveTabs.has(id) && isScriptableUrl(url)) {
2020
const {origin} = new URL(url);
2121
possiblyActiveTabs.set(id, origin);
22-
emitter.emit({id, origin});
22+
newActiveTabs.emit({id, origin});
2323
}
2424
}
2525

@@ -45,9 +45,10 @@ export function stopActiveTabTracking() {
4545
chrome.contextMenus?.onClicked.removeListener(altListener);
4646
chrome.commands?.onCommand.removeListener(altListener);
4747
chrome.tabs.onRemoved.removeListener(removalListener);
48+
possiblyActiveTabs.clear();
4849
}
4950

5051
export function onActiveTab(callback: (tab: ActiveTab) => void) {
5152
startActiveTabTracking();
52-
emitter.add(callback);
53+
newActiveTabs.add(callback);
5354
}

source/including-active-tab.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@
22
import '.';
33
import {type ContentScript} from 'webext-content-scripts/types';
44
import {injectContentScript} from 'webext-content-scripts';
5-
import {type ActiveTab, onActiveTab} from './active-tab.js';
5+
import {type ActiveTab, onActiveTab, possiblyActiveTabs} from './active-tab.js';
66
import {isContentScriptRegistered} from './utils.js';
77

8+
type InjectionDetails = {
9+
tabId: number;
10+
frameId: number;
11+
url: string;
12+
};
13+
14+
const gotNavigation = typeof chrome === 'object' && 'webNavigation' in chrome;
15+
816
const scripts = chrome.runtime.getManifest().content_scripts as ContentScript;
917

10-
async function injectToTab({id, origin}: ActiveTab): Promise<void> {
18+
async function injectToTabUnlessRegistered({id, origin}: ActiveTab): Promise<void> {
1119
if (id && !(await isContentScriptRegistered(origin))) {
1220
// Warning: This might cause duplicate injections on frames of activeTabs with different origins. Some details in:
1321
// https://github.com/fregante/webext-dynamic-content-scripts/pull/44
@@ -16,4 +24,34 @@ async function injectToTab({id, origin}: ActiveTab): Promise<void> {
1624
}
1725
}
1826

19-
onActiveTab(injectToTab);
27+
async function injectIfActive(
28+
{tabId, frameId, url}: InjectionDetails,
29+
): Promise<void> {
30+
if (possiblyActiveTabs.has(tabId) && !(await isContentScriptRegistered(url))) {
31+
await injectContentScript({tabId, frameId}, scripts);
32+
}
33+
}
34+
35+
async function tabListener(
36+
tabId: number,
37+
{status}: chrome.tabs.TabChangeInfo,
38+
{url}: chrome.tabs.Tab,
39+
): Promise<void> {
40+
// Only status updates are relevant
41+
// No URL = no permission
42+
if (status === 'loading' && url) {
43+
await injectIfActive({tabId, url, frameId: 0});
44+
}
45+
}
46+
47+
function init() {
48+
onActiveTab(injectToTabUnlessRegistered);
49+
50+
if (gotNavigation) {
51+
chrome.webNavigation.onCommitted.addListener(injectIfActive);
52+
} else {
53+
chrome.tabs.onUpdated.addListener(tabListener);
54+
}
55+
}
56+
57+
init();

0 commit comments

Comments
 (0)