-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinjected.js
41 lines (34 loc) · 1.05 KB
/
injected.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
function debug() {
console.log(arguments)
}
function error() {
console.log(arguments)
}
function highlightSelector(sel) {
$('.highlightSelector').removeClass('highlightSelector');
$(sel).addClass('highlightSelector');
}
chrome.runtime.onConnect.addListener(port => {
console.assert(port.name == "port");
port.onMessage.addListener(msg => {
debug('injected messageListener: msg =', msg);
if (msg.path) {
highlightSelector(msg.path);
}
});
const cssSelGen = new CssSelectorGenerator();
function handler(e) {
e.preventDefault(); // don't follow clicked on links, buttons etc.
e.stopPropagation(); // don't run handlers registered on nested elements
debug('injected handler: target =', e.target);
const sel = cssSelGen.getSelector(e.target);
debug('injected handler: sel =', sel);
highlightSelector(sel);
port.postMessage({path: sel});
}
document.addEventListener(
'click', handler,
{ capture: true } // call us BEFORE handlers registered on nested elements
);
});
({ msg: "injected: done" });