-
Notifications
You must be signed in to change notification settings - Fork 0
/
chrome-state-manager.js
75 lines (60 loc) · 1.81 KB
/
chrome-state-manager.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(function() {
var _isRoot = false;
var _isIndex = false;
var _routedEventObject = null;
function init() {
if (document.location.href.substring(0, 7) != 'file://') return;
// this is true if this is the root item
var _isRoot = (window == parent);
var mainFrame = document.getElementById('mainFrame');
_isIndex = mainFrame && true; // one way to cast it to a bool
var eventDiv = document.getElementById('axureEventReceiverDiv');
if (eventDiv) {
_routedEventObject = document.createEvent('Event');
_routedEventObject.initEvent('axureMessageReceiverEvent', true, true);
} else {
return;
}
if (_isRoot && _isIndex) {
chrome.extension.sendRequest({
message : "showIcon"
});
} else if (_isRoot) {
chrome.extension.sendRequest({
message : "hideIcon"
});
}
if (_isIndex) { initIndex(); }
var eventSenderDiv = document.getElementById('axureEventSenderDiv');
eventSenderDiv.addEventListener('axureMessageSenderEvent', handleSentEvent);
sendMessage(JSON.stringify({
message:'initialize'
}));
}
init();
function handleSentEvent() {
var eventSenderDiv = document.getElementById('axureEventSenderDiv');
var message = eventSenderDiv.innerText;
chrome.extension.sendRequest({
message : "routeEvent",
data : message
});
}
function initIndex() {
document.body.setAttribute('pluginDetected', 'true');
}
function initPage() {}
function sendMessage(requestJson) {
var axureEventDiv = document.getElementById('axureEventReceiverDiv');
if (axureEventDiv) {
axureEventDiv.innerText = requestJson;
axureEventDiv.dispatchEvent(_routedEventObject);
}
}
function onRequest(request, sender, callback) {
if (request.message == 'routeEvent') {
sendMessage(request.data);
}
};
chrome.extension.onRequest.addListener(onRequest);
})();