Skip to content

Commit 38e54d6

Browse files
committed
added ability to query the connected scripts tabs and fixed tests
1 parent e85d14f commit 38e54d6

5 files changed

Lines changed: 62 additions & 14 deletions

File tree

src/BackgroundHandler.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import { BgHandlerErrors as ERRORS, Error } from './Errors.js';
44

55
/**
66
* Class that will handle all the content scripts that will connect to the background script.
7-
*
8-
* @property {Map<string, Connection>} scriptConnections A Map that will relate every script ID to its Connection object.
9-
* @property {object} exposedData The properties and methods exposed to the connecting scripts.
10-
* @property {function} errorCallback A callback that gets fired whenever there is an error in the script. It will get passed some details about the error.
117
*/
128
class BackgroundHandler extends CustomEventTarget {
139

@@ -20,9 +16,14 @@ class BackgroundHandler extends CustomEventTarget {
2016
constructor(exposedData = {}, options = { runtime: chrome.runtime }) {
2117
super();
2218

19+
/** @type {Map<string, Connection>} scriptConnections A Map that will relate every script ID to its Connection object. */
2320
this.scriptConnections = new Map(); // script-id --> connection
21+
/** @property {object} exposedData The properties and methods exposed to the connecting scripts. */
2422
this.exposedData = exposedData;
23+
/** @property {function} errorCallback A callback that gets fired whenever there is an error in the script. It will get passed some details about the error. */
2524
this.errorCallback = options.errorCallback ?? null;
25+
26+
/** @property {chrome.runtime} runtime The runtime that will be used to create the connections. */
2627
this.runtime = options.runtime ?? chrome.runtime;
2728

2829
this.runtime.onConnect.addListener( (port) => this.handleNewConnection(port) );
@@ -170,6 +171,24 @@ class BackgroundHandler extends CustomEventTarget {
170171
return proxy;
171172
}
172173

174+
/**
175+
* Get all tab ids where a specific scriptId is present.
176+
*
177+
* @param {string} scriptId
178+
* @returns {number[]} The tab IDs
179+
*/
180+
getScriptTabs(scriptId) {
181+
let tabs = [];
182+
for (let [id, connection] of this.scriptConnections) {
183+
if (id.startsWith(scriptId)) {
184+
if (connection.port.sender?.tab?.id) {
185+
tabs.push(connection.port.sender.tab.id);
186+
}
187+
}
188+
}
189+
return tabs;
190+
}
191+
173192
/**
174193
* Check if a script with a specific id associated to a specific tab has made a connection to the background page.
175194
*

tests/Integrations.test.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ describe("The Background Handler", () => {
2828

2929
await waitFor(100);
3030

31-
assert.ok(bgHandler.scriptConnections.has(bgScript.scriptId));
31+
/** @type {string[]} */
32+
let scriptIds = [...bgHandler.scriptConnections.keys()];
33+
34+
assert.equal(scriptIds.length, 1);
35+
assert.ok(scriptIds[0].startsWith(bgScript.scriptId));
3236
});
3337

3438
test("should allow functions to be executed remotely", async () => {
@@ -116,7 +120,7 @@ describe("The Background Script should", () => {
116120
let [bgHandler, [bgScript, otherScript]] = setupScripts({}, [
117121
{ name: "test", exposed: {} },
118122
{ name: "test", exposed: {} }
119-
]);
123+
], 1);
120124

121125
let cb = mock.fn();
122126

@@ -149,4 +153,23 @@ describe("The Background Script should", () => {
149153
// just reset the value for next tests
150154
global.window = new MockedWindow();
151155
});
156+
});
157+
158+
describe("The Background Handler should", () => {
159+
test("be able to show the tabs to which a script is connected", async () => {
160+
let [bgHandler, [bgScript, otherScript, thirdScript]] = setupScripts({}, [
161+
{ name: "test", exposed: {} },
162+
{ name: "other", exposed: {} }
163+
]);
164+
165+
await waitFor(50);
166+
167+
let tabs = bgHandler.getScriptTabs("test");
168+
169+
assert.equal(tabs.length, 1);
170+
171+
tabs = bgHandler.getScriptTabs("other");
172+
173+
assert.equal(tabs.length, 1);
174+
});
152175
})

tests/mocks/mockedChromeRuntime.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import { getMockedPortPair } from "./mockedPort.js";
22
import { MockedEvent } from "./mockedEvent.js";
33

44
export class MockedChromeRuntime {
5-
constructor() {
5+
constructor(fixedTabId = null) {
66
this.onConnect = new MockedEvent();
7+
this.fixedTabId = fixedTabId;
78
}
89

910
connect({ name }) {
10-
let [port, otherPort] = getMockedPortPair(name);
11+
let [port, otherPort] = getMockedPortPair(name, this.fixedTabId);
1112

1213
// Don't do it in the same task
1314
setTimeout(() => {

tests/mocks/mockedPort.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { MockedEvent } from "./mockedEvent.js";
22

33
export class MockedPort {
4-
constructor(name) {
4+
constructor(name, tabId) {
55
this.name = name;
66
this.onMessage = new MockedEvent();
77
this.onDisconnect = new MockedEvent();
88
this.other = null;
9+
this.sender = {
10+
tab: {
11+
id: tabId ?? crypto.randomUUID()
12+
}
13+
}
914
}
1015

1116
_join(port) {
@@ -22,9 +27,9 @@ export class MockedPort {
2227
}
2328
}
2429

25-
export const getMockedPortPair = (name) => {
26-
let portA = new MockedPort(name);
27-
let portB = new MockedPort(name);
30+
export const getMockedPortPair = (name, tabId = null) => {
31+
let portA = new MockedPort(name, tabId);
32+
let portB = new MockedPort(name, tabId);
2833
portA._join(portB);
2934
portB._join(portA);
3035
return [portA, portB];

tests/utilities.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export function waitFor(ms) {
88
});
99
}
1010

11-
export function setupScripts(handlerData, scriptsData) {
12-
let runtime = new MockedChromeRuntime();
11+
export function setupScripts(handlerData, scriptsData, fixedTabId = null) {
12+
let runtime = new MockedChromeRuntime(fixedTabId);
1313
let bgHandler = new BackgroundHandler(handlerData, { runtime });
1414

1515
let scripts = [];

0 commit comments

Comments
 (0)