You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+158-1
Original file line number
Diff line number
Diff line change
@@ -64,4 +64,161 @@ If you're building an html page for your extension, just add the following tag t
64
64
<scriptsrc='bgscript.js'></script>
65
65
```
66
66
67
-
Of course, the background script location might be different in your project.
67
+
## How to use it
68
+
69
+
### Get a variable from the background script
70
+
71
+
Background script:
72
+
```js
73
+
let shared = {
74
+
variable:10
75
+
}
76
+
77
+
// This function shows how to use shared variables within the script to avoid problems with object references
78
+
functionsetVariable(val) {
79
+
shared.variable= val;
80
+
}
81
+
82
+
// Initialize the Background script handler passing the object to be shared
83
+
let bgHandler =newBackgroundHandler(shared);
84
+
```
85
+
86
+
Content script:
87
+
```js
88
+
// Initialize the background script object
89
+
let bgScript =newBackgroundScript();
90
+
91
+
asyncfunctionfoo() {
92
+
// Get a connection to the background script
93
+
let connection =awaitbgScript.getConnection();
94
+
95
+
// Get the variable from the background script
96
+
let variable =awaitconnection.variable;
97
+
98
+
console.log(variable);
99
+
}
100
+
101
+
foo();
102
+
```
103
+
104
+
### Execute a method from the background script
105
+
106
+
Background script:
107
+
```js
108
+
var variable =null;
109
+
110
+
functionsetVariable(val) {
111
+
variable = val;
112
+
}
113
+
114
+
functionshowVariable() {
115
+
return variable;
116
+
}
117
+
118
+
let shared = {
119
+
setVariable,
120
+
showVariable
121
+
}
122
+
123
+
// Initialize the Background script handler passing the object to be shared
124
+
let bgHandler =newBackgroundHandler(shared);
125
+
```
126
+
127
+
Content Script:
128
+
```js
129
+
let bgScript =newBackgroundScript();
130
+
131
+
asyncfunctionfoo() {
132
+
let connection =awaitbgScript.getConnection();
133
+
134
+
awaitconnection.setVariable("Hello world");
135
+
136
+
let variable =awaitconnection.showVariable();
137
+
138
+
console.log(variable);
139
+
}
140
+
141
+
foo();
142
+
```
143
+
144
+
### Shortcut for setting a shared variable
145
+
146
+
Instead of creating a setter function and using it like I showed in the examples above, if the variable you want to change is shared, you can do it this way:
147
+
148
+
Background script:
149
+
```js
150
+
151
+
let shared = {
152
+
variable:null
153
+
}
154
+
155
+
let bgHandler =newBackgroundHandler(shared);
156
+
```
157
+
158
+
Content script:
159
+
```js
160
+
let bgScript =newBackgroundScript();
161
+
162
+
asyncfunctionfoo() {
163
+
let connection =awaitbgScript.getConnection();
164
+
165
+
// Set the variable. The brackets are there to avoid syntax errors.
166
+
await (connection.variable="Hello world");
167
+
168
+
// Show the new variable value
169
+
console.log(awaitconnection.variable);
170
+
}
171
+
```
172
+
173
+
Note that when you set a variable, the new result will be returned (just like when you set a normal variable). This means that doing it this way will give you the same result as before:
## Using the sendMessage API alongside this library
182
+
183
+
Sometimes it could still be useful to have access to the sendMessage API directly. That is, for example, if you need to notify a content script about something that happened in the background script. In that situation, you can use the sendMessage API to send information in this way:
This is very similar to the original `sendMessage` API, but you won't listen directly to the `chrome.runtime.onMessage` event, instead you will listen to the `bgScript.onMessage` event, which will remove all the messages sent and received from the library for its internal use.
203
+
204
+
Sending a message from the content script is very similar:
chrome.runtime.sendMessage("Custom message from the content script", (response) => {
220
+
// Handle the response
221
+
});
222
+
```
223
+
224
+
**Important**: You can send to the background script any kind of messages, but there is one thing to keep in mind: if the message is an object, it **must not** have a `type` property with a value of `"bootstrap"`.
0 commit comments