-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientScript.js
59 lines (46 loc) · 1.72 KB
/
clientScript.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
49
50
51
52
53
54
55
56
57
58
59
// communicate events
// communicate events
var _phantom;
window.callPhantom = function(data){
//var event = new Event('callPhantom', data); window.dispatchEvent(event);
chrome.runtime.sendMessage({type: "callPhantom","data": data});
};
window.addEventListener('error', function(event) {
chrome.runtime.sendMessage({type: "error","message": event.message || event.error , "trace":event.error});
}, false);
console.log = (function(fct){
return function(message) {
fct.call(console, message);
chrome.runtime.sendMessage({type: "console","message": message});
};
})(console.log);
// Create the toolbar ui iframe and inject it in the current page
function initPhantomClient() {
_phantom = {
replay : true,
confirm : function(m){var event = new CustomEvent('Phantom', {'detail': m}); window.dispatchEvent(event);return true;},
alert : function(){return true;},
prompt : function(){return '';},
callPhantom : function(){}
};
return _phantom;
}
function setPhantomCallback(_phantom, callbackName, callbackSource) {
var el = document.getElementById('phantomClient'+callbackName);
if (el){
el.parentNode.removeChild(el);
}
if (!callbackSource) return ;
el = document.createElement('script');
el.id = 'phantomClient'+callbackName;
el.innerText = "window." + callbackName + "=" + callbackSource + ";";
document.body.appendChild(el);
}
if (window.parent == window) {
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (!_phantom) {
_phantom = initPhantomClient();
}
setPhantomCallback(_phantom, request.callbackName, request.callbackSource);
});
}