Skip to content

Commit 2ca310f

Browse files
authored
feat: add chrome extension (#145)
1 parent c66a304 commit 2ca310f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1443
-80
lines changed

.babelrc

+5
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
"presets": [
33
"@babel/preset-react",
44
"@babel/preset-env"
5+
],
6+
"plugins": [
7+
["@babel/plugin-proposal-class-properties", {
8+
"loose": true
9+
}]
510
]
611
}

devtools/src/background/background.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This import needs to be here, we don't use the bridge here directly. But
2+
// simply importing crx-bridge, is what creates the messaging proxy.
3+
import 'crx-bridge';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import Bridge from 'crx-bridge';
2+
import setupHighlighter from './highlighter';
3+
4+
import parser from '../../../src/parser';
5+
import { getQueryAdvise } from '../../../src/lib';
6+
import inject from './lib/inject';
7+
import { setup } from '../window/testing-library';
8+
import onDocReady from './lib/onDocReady';
9+
10+
function init() {
11+
inject('../window/testing-library.js');
12+
setup(window);
13+
14+
window.__TESTING_PLAYGROUND__ = window.__TESTING_PLAYGROUND__ || {};
15+
const hook = window.__TESTING_PLAYGROUND__;
16+
17+
hook.highlighter = setupHighlighter({ view: window, onSelectNode });
18+
19+
function onSelectNode(node) {
20+
const { data, suggestion } = getQueryAdvise({
21+
rootNode: document.body,
22+
element: node,
23+
});
24+
25+
const result = parser.parse({
26+
rootNode: document.body,
27+
query: suggestion.expression,
28+
});
29+
30+
Bridge.sendMessage('SELECT_NODE', { result, data, suggestion }, 'devtools');
31+
}
32+
33+
Bridge.onMessage('PARSE_QUERY', function ({ data }) {
34+
const result = parser.parse({
35+
rootNode: document.body,
36+
query: data.query,
37+
});
38+
39+
if (data.highlight) {
40+
hook.highlighter.highlight({
41+
nodes: (result.elements || []).map((x) => x.target),
42+
hideAfterTimeout: data.hideAfterTimeout,
43+
});
44+
}
45+
46+
return { result };
47+
});
48+
49+
// when the selected element is changed by using the element inspector,
50+
// this method will be called from devtools/main.js
51+
hook.onSelectionChanged = function onSelectionChanged(el) {
52+
onSelectNode(el);
53+
};
54+
}
55+
56+
onDocReady(init);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
* Copyright (c) 2020, Stephan Meijer
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*
8+
**/
9+
10+
import Overlay from './Overlay';
11+
12+
const SHOW_DURATION = 2000;
13+
14+
let timeoutID = null;
15+
let overlay = null;
16+
17+
export function hideOverlay() {
18+
timeoutID = null;
19+
20+
if (overlay !== null) {
21+
overlay.remove();
22+
overlay = null;
23+
}
24+
}
25+
26+
export function showOverlay(elements, hideAfterTimeout) {
27+
if (window.document == null) {
28+
return;
29+
}
30+
31+
if (timeoutID !== null) {
32+
clearTimeout(timeoutID);
33+
}
34+
35+
if (elements == null) {
36+
return;
37+
}
38+
39+
if (overlay === null) {
40+
overlay = new Overlay();
41+
}
42+
43+
overlay.inspect(elements);
44+
45+
if (hideAfterTimeout) {
46+
timeoutID = setTimeout(hideOverlay, SHOW_DURATION);
47+
}
48+
}

0 commit comments

Comments
 (0)