-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathonclick.js
53 lines (49 loc) · 1.46 KB
/
onclick.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
var cssPath = function(el) {
var path = [];
while (el && el != document) {
console.log(el);
nodeName = el.nodeName.toLowerCase();
var selector = nodeName;
if(nodeName == 'body') {
break;
}
if (el.id) {
selector += '#' + el.id;
//break;
}
if (el.className) {
c = el.className.replace(/\s+/g, '.');
c = '.'+c;
selector += c.replace(/^\.+/, '.');
}
try {
if (el.getAttribute('name')) {
selector += '[name="'+el.getAttribute('name')+'"]'
}
}
catch(e) {
}
if(!el.id && !el.className) {
var sib = el, nth = 1;
while (sib.nodeType === Node.ELEMENT_NODE && (sib = sib.previousSibling) && nth++);
selector += ":nth-child("+nth+")";
}
path.unshift(selector);
el = el.parentNode;
}
return path.join(" ");
}
function __init_tab() {
var port = chrome.runtime.connect({name: "cssevent"});
function onclick(event) {
if(event.ctrlKey) {
event.preventDefault();
}
var path = cssPath(event.target);
port.postMessage(path);
window.interactionHistory.push({path:path, timeStamp: event.timeStamp})
}
window.interactionHistory = window.interactionHistory || [];
document.addEventListener('click', onclick, false);
}
__init_tab();