-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
118 lines (100 loc) · 3.32 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
let currentDOMContent;
function captureDOM() {
currentDOMContent = document.documentElement.outerHTML;
}
window.onload = captureDOM;
class DataEvent {
constructor(type, event) {
this.target = event.target // target object
this.type = type;
this.value = event.target.value || "";
this.contextDOM = currentDOMContent;
this.element = event.target.outerHTML;
this.tagName = event.target.tagName;
this.timestamp = new Date().toISOString();
}
toDict() {
return {
type: this.type,
value: this.value,
contextDOM: this.contextDOM,
element: this.element,
tagName: this.tagName,
timestamp: this.timestamp
};
}
logEvent() {
chrome.storage.local.get(['logs', 'currentTask'], function (data) {
if (data.currentTask) {
let logs = data.logs || [];
let log = this.toDict();
log['task'] = data.currentTask
logs.push(log);
chrome.storage.local.set({ logs }, function () {
console.log('Log stored for element:', log.element);
});
}
}.bind(this));
captureDOM();
}
}
document.addEventListener('click', function (e) {
const event = new DataEvent('click', e);
event.logEvent();
});
document.addEventListener('keyup', function (e) {
if (!e.target.matches('input, select, textarea')) {
const event = new DataEvent('type', e);
event.logEvent();
}
});
document.addEventListener('change', function (e) {
if (e.target.matches('input, textarea')) {
const event = new DataEvent('type', e);
event.logEvent();
} else if (e.target.matches('select')) {
const event = new DataEvent('select', e);
event.logEvent();
}
});
// HOVER FUNCTIONALITY
let oldBorder;
let hoverBox = document.createElement('div');
hoverBox.style.position = 'absolute';
hoverBox.style.padding = '5px 10px';
hoverBox.style.background = 'black';
hoverBox.style.color = 'white';
hoverBox.style.borderRadius = '5px';
hoverBox.style.fontSize = '12px';
hoverBox.style.zIndex = '10000';
hoverBox.style.pointerEvents = 'none';
hoverBox.style.display = 'none';
document.body.appendChild(hoverBox);
document.addEventListener('mouseover', function (e) {
chrome.storage.local.get(['currentTask'], function (data) {
if (data.currentTask) {
oldBorder = e.target.style.border;
e.target.style.border = '2px dashed black';
hoverBox.textContent = e.target.tagName + ' element';
hoverBox.style.display = 'block';
hoverBox.style.left = (e.pageX + 10) + 'px';
hoverBox.style.top = (e.pageY + 10) + 'px';
}
});
});
document.addEventListener('mouseout', function (e) {
chrome.storage.local.get(['currentTask'], function (data) {
if (data.currentTask) {
e.target.style.border = oldBorder;
hoverBox.style.display = 'none';
}
});
});
document.addEventListener('mousemove', function (e) {
chrome.storage.local.get(['currentTask'], function (data) {
if (data.currentTask) {
hoverBox.style.left = (e.pageX + 10) + 'px';
hoverBox.style.top = (e.pageY + 10) + 'px';
}
});
});