-
Notifications
You must be signed in to change notification settings - Fork 3
/
content-script.js
48 lines (34 loc) · 1.48 KB
/
content-script.js
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
43
44
45
46
47
48
// 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 instMsg = 'is-installed-' + chrome.runtime.id;
var extMsg = 'get-sourceId-from-ext-' + chrome.runtime.id;
var allowedMessages = {};
allowedMessages[extMsg] = true;
allowedMessages[instMsg] = 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;
// if message is not own
if(!allowedMessages[event.data]) return;
// if browser is asking whether extension is already installed
if(event.data == instMsg) {
return window.postMessage('loowid-extension-loaded', '*');
}
// if it is something that need to be shared with background script
if(event.data == extMsg) {
// forward message to background script
port.postMessage(event.data);
}
});
// inform browser that you're available!
window.postMessage('loowid-extension-loaded', '*');