forked from reZach/page-recorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
69 lines (58 loc) · 1.65 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var findBestParent = function(event){
// Skip these tags,
// as they usually don't hold good identifiers
var skip = [
"path",
"svg"
];
var cursor = event;
while(cursor !== false){
if (skip.indexOf(cursor.tagName) >= 0){
if (typeof cursor.parentNode !== "undefined" && cursor.parentNode !== null){
cursor = cursor.parentNode;
} else {
cursor = false;
}
} else {
// Good element!
if (cursor.target !== null){
cursor = cursor.target;
break;
} else {
cursor = false;
}
}
}
return cursor;
}
var createElementSelector = function(element){
var _tag = element.tagName;
var _id = element.id;
var _class = element.className;
var _query = `${_tag.toLowerCase()}`;
if (_id.length > 0){
_query += `#${_id}`;
}
if (_class.length > 0){
_query += `.${_class.replace(new RegExp(" ", "g"), ".")}`;
}
return _query;
}
var buildMessage = function(element){
return {
url: window.location.href,
selector: element
};
}
document.addEventListener("click", function(event){
var bestParent = findBestParent(event);
if (bestParent === false){
console.warn("Could not find parent for click!");
return;
}
var element = createElementSelector(bestParent);
console.log(`${element}`);
chrome.runtime.sendMessage(buildMessage(element), function(response){
console.log(response);
});
});