forked from drmproductions/ActiveCollabInlineTimers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
45 lines (36 loc) · 1.63 KB
/
client.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
// only set everything up if this page is an activecollab page
if (Array.prototype.slice.call(document.getElementsByTagName('script')).some(function (scriptEl) {
return !(['activecollab', '/api/v1', 'activecollab_csrf', 'angie.api_url'].some(function (val) {
if (scriptEl.innerHTML.toLowerCase().indexOf(val) === -1) return true;
}))
})) {
console.log('client.js loaded!');
window.isActiveCollab = true;
// listen for messages from the popup
chrome.runtime.onMessage.addListener(function (request, sender, respond) {
if (!request || !(request.from === 'SERVER' || request.from === 'POPUP') || !request.payload) return;
request.from = 'CLIENT_EXTERNAL';
window.postMessage(request, "*");
});
// setup direct message passing from CLIENT_INTERNAL to the main extension background process
window.addEventListener('message', function (event) {
if (event.source != window || !event.data.from || event.data.from != 'CLIENT_INTERNAL') return;
const data = JSON.parse(JSON.stringify(event.data));
data.from = 'CLIENT_EXTERNAL';
chrome.runtime.sendMessage(data, function (response) {
if (!response || !response.from || response.from != 'SERVER' || !response.payload) return;
response.from = 'CLIENT_EXTERNAL';
window.postMessage(response, "*");
});
}, false);
// inject the javascript
var script = document.createElement('script');
script.src = chrome.extension.getURL('page.js');
document.body.appendChild(script);
// inject the css
var styles = document.createElement('link');
styles.href = chrome.extension.getURL('page.css');
styles.rel = 'stylesheet';
styles.type = 'text/css';
document.head.appendChild(styles);
}