Skip to content

Commit 619a111

Browse files
committed
added onmessage to the background handler, improved check for signed messages and fixed syntax error in the event trigger function
1 parent 9a40074 commit 619a111

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

README.md

+33-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ This project was inspired by comlink, and it started out as a way for me to bett
88
## How does it work?
99

1010
In your background script, you must create a BackgroundHandler class and pass it an object that contains the property and methods that you want to share:
11-
```
11+
12+
```js
1213
// background.js
1314

1415
function remoteFunction() {
@@ -22,7 +23,8 @@ let bgHandler = new BackgroundHandler({
2223
```
2324

2425
In your content script, you should create a BackgroundScript class and then use it in this way:
25-
```
26+
27+
```js
2628
var bgScript = new BackgroundScript();
2729

2830
async function foo() {
@@ -34,3 +36,32 @@ async function foo() {
3436

3537
}
3638
```
39+
40+
## Installation
41+
42+
Download the `bgscript.js` file and include it in your chrome extension in the following two ways.
43+
44+
In order to use it in your content scripts, include it in your manifest.json as the first content script:
45+
46+
```
47+
"content_scripts": [{
48+
"js": ["bgscript.js", "my-content-script.js", ...]
49+
}]
50+
```
51+
52+
Similarly, you need to declare it as first script in the "background scripts" section of your manifest file:
53+
54+
```
55+
"background": {
56+
"scripts": [ "bgscript.js", "my-background-script.js"]
57+
}
58+
```
59+
60+
When you do this, the two classes will be automatically available in your scripts.
61+
62+
If you're building an html page for your extension, just add the following tag to the head of your page, before any other script.
63+
```html
64+
<script src='bgscript.js'></script>
65+
```
66+
67+
Of course, the background script location might be different in your project.

bgscript.js

+30-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// This must be used in the content script (or extension page script)
22

3+
// Class that is used internally to emulate the chrome "onMessage" event handling for further user customization
34
class BSEvent {
45
listeners = []
56

@@ -14,9 +15,9 @@ class BSEvent {
1415
}
1516
}
1617

17-
trigger(details) {
18-
for (f of this.listeners) {
19-
f(details);
18+
trigger(...details) {
19+
for (let f of this.listeners) {
20+
f(...details);
2021
}
2122
}
2223
}
@@ -35,7 +36,7 @@ class BackgroundScript {
3536

3637
// Starts the event listener on message
3738
init() {
38-
chrome.runtime.onMessage.addListener((message) => {
39+
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
3940
// If the message is an object, and has a "type" property, the it may be for our internal use
4041
if (this.isScriptMessage(message)) {
4142

@@ -51,7 +52,7 @@ class BackgroundScript {
5152
}
5253

5354
// If it's not for our use, than redirect it to the user handler
54-
this.onMessage.trigger(message);
55+
this.onMessage.trigger(message, sender, sendResponse);
5556
});
5657
}
5758

@@ -100,7 +101,8 @@ class BackgroundScript {
100101
let request = {
101102
type: "call",
102103
name: property,
103-
args: args
104+
args: args,
105+
signature: this.signature
104106
}
105107

106108
chrome.runtime.sendMessage(request, (response) => {
@@ -138,6 +140,7 @@ class BackgroundScript {
138140
});
139141
}
140142

143+
// Check if the message received is coming from the bgscript library
141144
isScriptMessage(message) {
142145
if (typeof(message) === "object" && "signature" in message && message.signature == this.signature)
143146
return true;
@@ -146,6 +149,9 @@ class BackgroundScript {
146149

147150
_sendMessage(request, resolve, reject) {
148151
try {
152+
if (this.signature)
153+
request.signature = this.signature;
154+
149155
chrome.runtime.sendMessage(request, resolve);
150156
}
151157
catch (err) {
@@ -166,7 +172,8 @@ class BackgroundHandler {
166172
sharedMethods = {}
167173
sharedProps = {}
168174
currentCallId = 1
169-
signature = null;
175+
signature = null
176+
onMessage = new BSEvent()
170177

171178
constructor(sharedData) {
172179
// Split the shared data between functions and properties, for easier access
@@ -184,13 +191,18 @@ class BackgroundHandler {
184191

185192
// Initialization function, adds the message listener
186193
init() {
187-
194+
// Create a signature to help identify library messages
188195
this.signature = this._uuidv4();
189196

190197
// Add listener for incoming messages
191198
chrome.runtime.onMessage.addListener(
192199
(request, sender, sendResponse) => {
193-
sendResponse( this.handleRequest(request, sender) );
200+
if (this.isScriptMessage(request)) {
201+
sendResponse( this.handleRequest(request, sender) );
202+
}
203+
else {
204+
this.onMessage.trigger(request, sender, sendResponse);
205+
}
194206
}
195207
);
196208
}
@@ -254,6 +266,15 @@ class BackgroundHandler {
254266
});
255267
}
256268

269+
// Check if the message received is coming from the bgscript library
270+
isScriptMessage(message) {
271+
if (typeof(message) === "object") {
272+
if (message.type === "bootstrap") return true;
273+
if (message.signature == this.signature) return true;
274+
}
275+
return false;
276+
}
277+
257278
_promisify(func, args) {
258279
let result = null;
259280
try {

0 commit comments

Comments
 (0)