-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathframe-script.js
55 lines (43 loc) · 1.2 KB
/
frame-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
/* See license.txt for terms of usage */
"use strict";
(function({content, addMessageListener, sendAsyncMessage, removeMessageListener}) {
const document = content.document;
const window = content;
/**
* Listener for requests (messages) sent from a test (chrome scope).
* A response is always sent back together with a result.
*/
function requestListener(message) {
const { type, data } = message.data;
let element;
let result;
// Perform requested action.
switch (type) {
case "exist":
element = document.querySelector(data.selector);
break;
case "click":
element = document.querySelector(data.selector);
if (element) {
element.click();
}
break;
}
result = element ? true : false;
postChromeResponse(type, result);
};
addMessageListener("test/request", requestListener);
window.addEventListener("unload", event => {
removeMessageListener("test/request", requestListener);
})
/**
* Send a message back to the parent panel (chrome scope).
*/
function postChromeResponse(type, args, objects) {
let data = {
type: type,
args: args,
};
sendAsyncMessage("test/response", data, objects);
}
})(this);