-
Notifications
You must be signed in to change notification settings - Fork 3
/
content.js
53 lines (47 loc) · 1.54 KB
/
content.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/* globals chrome, exportFunction, reduceNode */
const reduceRequestMap = new Map();
if (window.wrappedJSObject) {
window.wrappedJSObject.reduceTestCase = exportFunction(function(
element,
settings
) {
const reduceRequestId = `consoleReq${Date.now()}`;
reduceRequestMap.set(reduceRequestId, element);
chrome.runtime.sendMessage({ reduceRequestId, settings });
return chrome.i18n.getMessage("checkDevtoolsPanel");
},
window);
}
chrome.runtime.onMessage.addListener((msg, _, sendResponse) => {
if (msg === "ping") {
return false;
}
// Browsers supporting useContentScriptContext will have already stored the
// inspected node on the content script for us. Other browsers have to store
// it on the actual page for us to read here (we should unset it now, too).
const requestId = msg.reduceRequest.id;
const { frameId } = msg.reduceRequest;
let node = reduceRequestMap.get(requestId);
if (node) {
reduceRequestMap.delete(requestId);
} else {
node = document.querySelector(`[r${requestId}]`);
}
if (!node) {
sendResponse({ requestId, frameId });
return false;
}
reduceNode(node, msg.reduceRequest).then(
result => {
sendResponse({ requestId, frameId, result });
},
error => {
sendResponse({ requestId, frameId, error });
}
);
return true;
});