-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent_script.js
76 lines (72 loc) · 1.93 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
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
76
/**
* This file will interact with page and will perform given actions.
* # Listen all post message events.
* # Will read and modify we page.
*/
window.addEventListener("message", (event)=> {
checkEvent(event);
});
/**
* Check and convert data to JSON object.
* @param event {Event} post message event object.
*/
const CAN_ACCESS_FROM = "CSS_REG_WEB_PAGE";
const FROM = "CSS_REG_EXT";
const TYPE_HAND_SHAKE = "HAND_SHAKE";
const TYPE_MOCK_DATA = "MOCK_DATA";
const TYPE_CAPTURE_SCREEN_SHOTS = "CAPTURE_SCREEN_SHOTS";
function checkEvent(event) {
var data;
if ((event.origin === "http://localhost:8080" || event.origin === "https://css-reg.appspot.com")) {
try {
data = JSON.parse(event.data);
if (data.from === CAN_ACCESS_FROM) {
console.log("FROM_EXTENSION");
processEventData(data);
}
} catch (e) {
console.error(e);
}
}
}
/**
*
* @param data {Object} JSON object of event data.
*/
function processEventData(data) {
console.log(data);
switch (data.type) {
case TYPE_HAND_SHAKE:
sendHandShakeResp();
break;
case TYPE_MOCK_DATA:
runtimeSendMessage(data);
break;
case TYPE_CAPTURE_SCREEN_SHOTS:
runtimeSendMessage(data);
break;
default:
console.log("Not a valid type");
}
}
function sendHandShakeResp() {
var data = {
'from': FROM,
'type': TYPE_HAND_SHAKE,
'msg': 'YES_IAM_HERE'
};
sendPostMessage(JSON.stringify(data));
}
function sendPostMessage(dataStr) {
window.postMessage(dataStr, '*');
}
function runtimeSendMessage(data) {
chrome.runtime.sendMessage(null, data);
}
chrome.runtime.onMessage.addListener((msg, sender, resp)=> {
var data = {msg: msg};
data.from = FROM;
data.type = TYPE_MOCK_DATA;
sendPostMessage(JSON.stringify(data));
return;
});