-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-script.js
More file actions
42 lines (34 loc) · 1.35 KB
/
content-script.js
File metadata and controls
42 lines (34 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// this content-script plays role of medium to publish/subscribe messages from webpage to the background script
// this object is used to make sure our extension isn't conflicted with irrelevant messages!
var messages = {
'are-you-there': true,
'get-sourceId': true,
'audio-plus-tab': true
};
// this port connects with background script
var port = chrome.runtime.connect();
// if background script sent a message
port.onMessage.addListener(function (message) {
// get message from background script and forward to the webpage
window.postMessage(message, '*');
});
// this event handler watches for messages sent from the webpage
// it receives those messages and forwards to background script
window.addEventListener('message', function (event) {
// if invalid source
if (event.source != window)
return;
// it is 3rd party message
if(!messages[event.data]) return;
// if browser is asking whether extension is available
if(event.data == 'are-you-there') {
window.postMessage('extension-loaded', '*');
}
// if it is something that need to be shared with background script
if(event.data == 'get-sourceId' ) {
// forward message to background script
port.postMessage(event.data);
}
});
// inform browser that you're available!
window.postMessage('extension-loaded', '*');