Skip to content

Commit 4d6fdf5

Browse files
committed
Improved readme with some examples
1 parent 619a111 commit 4d6fdf5

File tree

1 file changed

+158
-1
lines changed

1 file changed

+158
-1
lines changed

README.md

+158-1
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,161 @@ If you're building an html page for your extension, just add the following tag t
6464
<script src='bgscript.js'></script>
6565
```
6666

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+
function setVariable(val) {
79+
shared.variable = val;
80+
}
81+
82+
// Initialize the Background script handler passing the object to be shared
83+
let bgHandler = new BackgroundHandler(shared);
84+
```
85+
86+
Content script:
87+
```js
88+
// Initialize the background script object
89+
let bgScript = new BackgroundScript();
90+
91+
async function foo() {
92+
// Get a connection to the background script
93+
let connection = await bgScript.getConnection();
94+
95+
// Get the variable from the background script
96+
let variable = await connection.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+
function setVariable(val) {
111+
variable = val;
112+
}
113+
114+
function showVariable() {
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 = new BackgroundHandler(shared);
125+
```
126+
127+
Content Script:
128+
```js
129+
let bgScript = new BackgroundScript();
130+
131+
async function foo() {
132+
let connection = await bgScript.getConnection();
133+
134+
await connection.setVariable("Hello world");
135+
136+
let variable = await connection.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 = new BackgroundHandler(shared);
156+
```
157+
158+
Content script:
159+
```js
160+
let bgScript = new BackgroundScript();
161+
162+
async function foo() {
163+
let connection = await bgScript.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(await connection.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:
174+
```js
175+
...
176+
// Set the variable and log its new value
177+
console.log(await (connection.variable = "Hello world"));
178+
...
179+
```
180+
181+
## 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:
184+
185+
Background script:
186+
```js
187+
chrome.tabs.sendMessage(tabId, "Custom message text", (response) => {
188+
// Handle the response
189+
});
190+
```
191+
192+
Content script:
193+
```js
194+
let bgScript = new BackgroundScript();
195+
196+
bgScript.onMessage.addListener( (request, sender, sendResponse) => {
197+
// ...
198+
sendResponse("Reponse from the content script");
199+
});
200+
```
201+
202+
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:
205+
206+
Background script:
207+
```js
208+
let shared = {};
209+
210+
let bgHandler = new BackgroundHandler(shared);
211+
212+
bgHandler.onMessage.addListener( (message, sender, sendResponse) => {
213+
// Handle the message
214+
});
215+
```
216+
217+
Content script:
218+
```js
219+
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

Comments
 (0)