-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcontent_script.js
More file actions
66 lines (61 loc) · 2.43 KB
/
content_script.js
File metadata and controls
66 lines (61 loc) · 2.43 KB
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
// inject script into page
var injection = document.createElement('script');
injection.src = chrome.runtime.getURL('snkit.js');
(document.head||document.documentElement).appendChild(injection);
injection.onload = () => {
injection.parentNode.removeChild(injection);
};
//Create a custom DOM event for communication with the page
function sendCmd(cmd, cmdData) {
var cmdEvent = new CustomEvent('snkitRequest', {detail: { cmd: cmd, cmdData: cmdData } });
window.dispatchEvent(cmdEvent);
}
function relayResponse(sendResponse) {
var _response = sendResponse;
window.addEventListener("message", function handler(event) {
// immediately remove the event
event.currentTarget.removeEventListener(event.type, handler);
// We only accept messages from ourselves
if (event.source != window)
return;
if (event.data.type && (event.data.type == "from_page")) {
_response( { data: event.data.text, cmd: event.data.cmd });
}
}, false);
}
function highlightWidget(widgetIdentityObj) {
if(widgetIdentityObj.id)
var target = document.getElementById(widgetIdentityObj.id);
else
var target = document.querySelector("." + widgetIdentityObj.class);
if(target)
Object.assign(target.style, {animation: "pulse 500ms", boxShadow: "0px 0px 25px #881391"});
}
function removeWidgetHighlight(widgetIdentityObj) {
if(widgetIdentityObj.id)
var target = document.getElementById(widgetIdentityObj.id);
else
var target = document.querySelector("." + widgetIdentityObj.class);
if(target)
Object.assign(target.style, {animation: "", boxShadow: "" });
}
// Make a decision about where the command will be executed;
// either on the page or in the content script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type == "EVENT_PAGE" && message.cmdType == "page") {
sendCmd(message.text, message.data);
relayResponse(sendResponse);
// According to chrome API documentation this allows for the
// message channel to stay open for the async callback sendResponse
return true;
} else if (message.type == "EVENT_PAGE" && message.cmdType == "content_script") {
// create a local variable to persist the data object from the original message
var data = message.data;
if(message.text == "highlightWidget"){
highlightWidget(data);
} else if(message.text == "removeWidgetHighlight"){
removeWidgetHighlight(data);
}
}
}
)