@livequery/rpc 2.0.102 → 2.0.105
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -1
- package/dist/ExtensionChannel.d.ts.map +1 -1
- package/dist/ExtensionChannel.js +49 -19
- package/dist/ExtensionChannel.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -78,7 +78,12 @@ const linker = new ServiceLinker(channel)
|
|
|
78
78
|
const counter = linker.linkService<WorkerService<CounterService>>("counter")
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
-
`ExtensionChannel`
|
|
81
|
+
`ExtensionChannel` auto-detects its context at construction time:
|
|
82
|
+
|
|
83
|
+
- **Background service worker** (`typeof window === 'undefined'`): listens on `chrome.runtime.onMessage` and routes responses back to the originating tab via `chrome.tabs.sendMessage`, or falls back to `chrome.runtime.sendMessage` when the message did not originate from a tab.
|
|
84
|
+
- **Foreground context** (popup, options page, content script): listens on `chrome.runtime.onMessage` for responses arriving from the background and sends requests with `chrome.runtime.sendMessage`.
|
|
85
|
+
|
|
86
|
+
If `chrome` is not available (e.g. during SSR), all operations silently no-op.
|
|
82
87
|
|
|
83
88
|
## When To Use This Package
|
|
84
89
|
|
|
@@ -306,6 +311,29 @@ const worker = new SharedWorker(new URL("./worker.ts", import.meta.url), { type:
|
|
|
306
311
|
const channel = new SharedWorkerChannel(worker)
|
|
307
312
|
```
|
|
308
313
|
|
|
314
|
+
### `ExtensionChannel`
|
|
315
|
+
|
|
316
|
+
Concrete `RpcChannel` implementation for Chrome extension Manifest V3 messaging.
|
|
317
|
+
|
|
318
|
+
Auto-detects its context at construction time — no argument is required.
|
|
319
|
+
|
|
320
|
+
```ts
|
|
321
|
+
const channel = new ExtensionChannel()
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
**Background service worker** (`typeof window === 'undefined'`):
|
|
325
|
+
|
|
326
|
+
- listens on `chrome.runtime.onMessage`
|
|
327
|
+
- responds to tab-originated messages via `chrome.tabs.sendMessage(tabId, …)`
|
|
328
|
+
- responds to non-tab messages via `chrome.runtime.sendMessage(…)`
|
|
329
|
+
|
|
330
|
+
**Foreground context** (popup, options page, content script):
|
|
331
|
+
|
|
332
|
+
- listens on `chrome.runtime.onMessage` for responses from the background
|
|
333
|
+
- sends requests with `chrome.runtime.sendMessage`
|
|
334
|
+
|
|
335
|
+
If `chrome` is unavailable (e.g. during SSR), all operations silently no-op.
|
|
336
|
+
|
|
309
337
|
### `WorkerManager`
|
|
310
338
|
|
|
311
339
|
Registers named services and routes incoming RPC requests.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExtensionChannel.d.ts","sourceRoot":"","sources":["../src/ExtensionChannel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"ExtensionChannel.d.ts","sourceRoot":"","sources":["../src/ExtensionChannel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAyB7D,qBAAa,gBAAiB,SAAQ,UAAU;;;IAmD5C,IAAI,CAAC,OAAO,EAAE,UAAU,GAAG,IAAI;CAIlC"}
|
package/dist/ExtensionChannel.js
CHANGED
|
@@ -1,32 +1,62 @@
|
|
|
1
1
|
import { RpcChannel } from "./RpcChannel.js";
|
|
2
|
-
const
|
|
3
|
-
const runtime = globalThis.chrome?.runtime;
|
|
4
|
-
if (!runtime) {
|
|
5
|
-
throw new Error("chrome.runtime is not available");
|
|
6
|
-
}
|
|
7
|
-
return runtime;
|
|
8
|
-
})();
|
|
2
|
+
const chrome = globalThis.chrome;
|
|
9
3
|
function isRpcMessage(value) {
|
|
10
4
|
return !!value && typeof value === "object" && "id" in value;
|
|
11
5
|
}
|
|
12
6
|
export class ExtensionChannel extends RpcChannel {
|
|
13
7
|
constructor() {
|
|
14
8
|
super();
|
|
15
|
-
|
|
9
|
+
if (typeof window == 'undefined') {
|
|
10
|
+
this.#initBackground();
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
this.#initForegound();
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
#initForegound() {
|
|
17
|
+
if (!chrome)
|
|
18
|
+
return;
|
|
19
|
+
const runtime = chrome.runtime;
|
|
20
|
+
runtime?.onMessage.addListener((message, sender, sendResponse) => {
|
|
21
|
+
if (!isRpcMessage(message))
|
|
22
|
+
return;
|
|
23
|
+
const respond = (response) => {
|
|
24
|
+
runtime?.sendMessage({
|
|
25
|
+
id: message.id,
|
|
26
|
+
response
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
this.next({ ...message, respond });
|
|
30
|
+
});
|
|
16
31
|
}
|
|
17
|
-
#
|
|
18
|
-
if (!
|
|
32
|
+
#initBackground() {
|
|
33
|
+
if (!chrome)
|
|
19
34
|
return;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
35
|
+
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
|
36
|
+
if (!isRpcMessage(message))
|
|
37
|
+
return;
|
|
38
|
+
const tabId = sender.tab?.id;
|
|
39
|
+
const respond = (response) => {
|
|
40
|
+
if (tabId) {
|
|
41
|
+
chrome.tabs.sendMessage(tabId, {
|
|
42
|
+
id: message.id,
|
|
43
|
+
response
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
chrome.runtime.sendMessage({
|
|
48
|
+
id: message.id,
|
|
49
|
+
response
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
this.next({ ...message, respond });
|
|
54
|
+
});
|
|
55
|
+
}
|
|
28
56
|
send(message) {
|
|
29
|
-
|
|
57
|
+
if (!chrome)
|
|
58
|
+
return;
|
|
59
|
+
chrome.runtime.sendMessage(message);
|
|
30
60
|
}
|
|
31
61
|
}
|
|
32
62
|
//# sourceMappingURL=ExtensionChannel.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExtensionChannel.js","sourceRoot":"","sources":["../src/ExtensionChannel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAmB,MAAM,iBAAiB,CAAA;AAY7D,MAAM,
|
|
1
|
+
{"version":3,"file":"ExtensionChannel.js","sourceRoot":"","sources":["../src/ExtensionChannel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAmB,MAAM,iBAAiB,CAAA;AAY7D,MAAM,MAAM,GAAI,UAOd,CAAC,MAAM,CAAA;AAET,SAAS,YAAY,CAAC,KAAc;IAChC,OAAO,CAAC,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,IAAI,IAAI,KAAK,CAAA;AAChE,CAAC;AAED,MAAM,OAAO,gBAAiB,SAAQ,UAAU;IAI5C;QACI,KAAK,EAAE,CAAA;QACP,IAAI,OAAO,MAAM,IAAI,WAAW,EAAE,CAAC;YAC/B,IAAI,CAAC,eAAe,EAAE,CAAA;QAC1B,CAAC;aAAM,CAAC;YACJ,IAAI,CAAC,cAAc,EAAE,CAAA;QACzB,CAAC;IACL,CAAC;IAED,cAAc;QACV,IAAI,CAAC,MAAM;YAAE,OAAM;QACnB,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAA;QAC9B,OAAO,EAAE,SAAS,CAAC,WAAW,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,EAAE;YAC7D,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC;gBAAE,OAAM;YAClC,MAAM,OAAO,GAAG,CAAC,QAAgC,EAAE,EAAE;gBACjD,OAAO,EAAE,WAAW,CAAC;oBACjB,EAAE,EAAE,OAAO,CAAC,EAAE;oBACd,QAAQ;iBACU,CAAC,CAAA;YAC3B,CAAC,CAAA;YACD,IAAI,CAAC,IAAI,CA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