-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
47 lines (39 loc) · 1.2 KB
/
content.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
function sendToBackground(data){
chrome.runtime.sendMessage(data);
}
//no direct way to send to iframe, send to background and route to iframe from there.
function initOnLoad()
{
sendToBackground({from: 'content', message: 'hi from content'});
//create iframe and inject into page
var iframe = document.createElement("iframe");
iframe.src = chrome.extension.getURL('/iframe/hackbar.html?view=hackbar');
iframe.classList.add("hackbar");
iframe.id = 'hackbar';
//add iframe to Twitter page
document.body.appendChild(iframe);
console.log('Injected iframe');
//listen for messages from iframe
window.addEventListener('message', function(event) {
if(event.data.from)
{
console.log('Content.js received a message from iframe: ');
console.log(event.data);
}
}, false);
//listen for messages from background
chrome.extension.onMessage.addListener(function(request, sender, sendResponse){
if(request.from)
{
console.log('Received msg from BG');
console.log(request);
}
});
}
function scheduleInjection()
{
console.log('Page loaded');
//wait for 3 sec after page load event for other components to load
setTimeout(initOnLoad, 3000);
}
window.addEventListener("load", scheduleInjection);