-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
274 lines (235 loc) · 8.41 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
var currentStep = [];
var currentContextMenuTarget = undefined;
async function addStep(step) {
return new Promise((resolve, reject) => {
chrome.storage.local.get('jmonData', function(previousSteps) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
reject();
}
allSteps = previousSteps.jmonData || [];
allSteps.push(step);
console.debug("All steps:");
console.debug(allSteps);
chrome.storage.local.set({ jmonData: allSteps }, function() {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
return resolve();
});
});
});
}
function getUniqueClassForTarget(target, parent) {
if ((! target.className) || typeof target.className !== "string") {
console.debug("Could not find any classes attached to target");
return;
}
// Iterate over each class of the target
for (let className of target.className.split(' ')) {
// Skip empty class names
if (! className.trim()) {
continue;
}
// Check if there's only one instance of class with the name
if (parent.getElementsByClassName(className).length == 1) {
return className;
} else {
console.debug(`${className} is not unique`);
}
}
return null;
}
function getUniqueContentForTarget(target, parent) {
// If target inner text is empty, return early
if (! target.textContent) {
return null;
}
// Ignore with large content or new lines
if (target.textContent.length > 30 || target.textContent.indexOf("\n") !== -1) {
return null;
}
// Iterate over each class that matches content
let found = false;
for (const div of parent.querySelectorAll(target.nodeName)) {
if (div.textContent == target.textContent) {
if (found) {
// If a duplicate is found, do not use as identifier
console.debug("Found duplicate element with duplicate content")
return null;
}
found = true;
}
}
return target.textContent;
}
function identifierLooksNoneRandom(id) {
console.debug(`Checking if identifier looks random: ${id}`);
let countCaps = (id.match(/[A-Z]/g) || '').length;
let countLower = (id.match(/[a-z]/g) || '').length;
let countDigits = (id.match(/[0-9]/g) || '').length;
// check if high proportion of string is numbers of capitals
if (countCaps > (id.length / 3) || countDigits > (id.length / 3)) {
console.debug("Looks random");
return false;
} else if ((countCaps + countDigits) > (countLower / 2) ) {
console.debug("Looks random")
return false;
}
console.debug("Does not look random");
return true;
}
function getParentStep(target, indentationCount, rootParent) {
let parent = target;
// Recurse down parents
while (parent !== document) {
parent = parent.parentNode;
let [parentStep, parentIndentation] = getExactFindForTarget(parent, indentationCount, rootParent);
if (parentStep !== null) {
let [childStep, childIndentation] = getExactFindForTarget(target, parentIndentation, parent);
if (childStep !== null) {
return [`${parentStep}\n${childStep}`, childIndentation];
}
}
}
return [null, indentationCount];
}
function getExactFindForTarget(target, indentationCount, parent=undefined) {
if (parent === undefined) {
parent = document;
}
let ind = ' '.repeat(indentationCount);
let step = `${ind}- find:\n`;
let uniqueClassName = getUniqueClassForTarget(target, parent);
let foundIdentifier = false;
// Check for ID match
if (target.id) {
step += `${ind} - id: ${target.id}`;
foundIdentifier = true;
// Otherwise check for placeholder, since this
// is more user-friendly
} else if (target.getAttribute('placeholder')) {
step += `${ind} - placeholder: ${target.getAttribute('placeholder')}`;
foundIdentifier = true;
// Check for non-unique class names
} else if (uniqueClassName && identifierLooksNoneRandom(uniqueClassName)) {
step += `${ind} - class: ${uniqueClassName}`;
foundIdentifier = true;
// Check for content
} else if (getUniqueContentForTarget(target, parent)) {
step += `${ind} - tag: ${target.nodeName}\n${ind} - text: ${getUniqueContentForTarget(target, parent)}`;
foundIdentifier = true;
// Use non-unique class name
} else if (uniqueClassName) {
step += `${ind} - class: ${uniqueClassName}`;
foundIdentifier = true;
}
return [foundIdentifier ? step : null , indentationCount + 2];
}
function getFindForTarget(target, indentationCount=3, parent=undefined) {
// Calculate how to find element
if (parent === undefined) {
parent = document;
}
let step = null;
let childIndentation = indentationCount;
// Check exact match
let exactFind = null;
[exactFind, childIndentation] = getExactFindForTarget(target, indentationCount, parent);
if (exactFind !== null) {
step = exactFind;
} else {
[parentFind, childIndentation] = getParentStep(target, indentationCount, parent);
// Limit to only the root indentation lookup
if (parentFind !== null) {
step = parentFind;
}
}
return [step, childIndentation];
}
async function handleDomClick(event) {
await checkCompleteStartStep("CLICK", event.target, undefined);
let [step, indentationCount] = getFindForTarget(event.target);
let indentation = ' '.repeat(indentationCount);
if (!step) {
await addStep('# WARNING: Unable to identifier for click step');
return;
}
step += `\n${indentation}- actions:\n${indentation} - click`;
await addStep(step);
}
async function handleDomInput(event) {
await checkCompleteStartStep("TYPE", event.target, event.data);
}
async function completeTypeStep(target, text) {
let [step, indentationCount] = getFindForTarget(target);
let indentation = ' '.repeat(indentationCount);
if (!step) {
await addStep('# WARNING: Unable to identifier for text typing');
return;
}
step += `\n${indentation}- actions:\n${indentation} - type: ${text}`;
await addStep(step);
}
async function addCheckStepContextMenuContent() {
await checkCompleteStartStep("CONTEXT_MENU_CONTENT", undefined, undefined);
if (! currentContextMenuTarget || ! currentContextMenuTarget.target) {
console.log("Cannot find current context menu item")
return;
}
let [step, indentationCount] = getFindForTarget(currentContextMenuTarget.target);
let indentation = ' '.repeat(indentationCount);
if (!step) {
await addStep("# WARNING: Unable to find identifier for element for manual content check");
return;
}
step += `\n${indentation}- check:\n${indentation} text: ${currentContextMenuTarget.target.textContent}`;
await addStep(step);
}
async function checkCompleteStartStep(stepType, target, value) {
return new Promise(async (resolve, reject) => {
// Check if a step is in progress and doens't match
// the current step
if (!currentStep || (currentStep[0] != stepType || currentStep[1] != target)) {
if (currentStep[0] == "TYPE") {
// Complete previous type step
await completeTypeStep(currentStep[1], currentStep[2]);
}
// Set current step to this step type
currentStep = [stepType, target, value];
} else {
// If step type and target are the same, append data
if (currentStep[0] == "TYPE" && value) {
currentStep[2] += value;
}
}
resolve();
});
}
function updateContextMenuElement(target) {
currentContextMenuTarget = target;
}
function injectDomListeners() {
console.log("Injecting DOM Listeners");
// Add listener for click/input events
document.addEventListener("click", handleDomClick);
document.addEventListener("input", handleDomInput);
// Add listener for context menu events, so that
// triggers to our context menu can capture the element
// it was triggered on
document.addEventListener("contextmenu", updateContextMenuElement);
}
// Listen for URL change messages from the background script
chrome.runtime.onMessage.addListener(async function(message, sender, sendResponse) {
if (message.type === 'goto') {
checkCompleteStartStep("GOTO", undefined, undefined);
await addStep(` - goto: "${message.url}"`);
} else if (message.type === 'urlChange') {
checkCompleteStartStep("REDIRECT", undefined, undefined);
await addStep(` - check:\n url: "${message.url}"`);
} else if (message.type == 'injectDomListener') {
injectDomListeners();
} else if (message.type == "checkContextMenuContent") {
addCheckStepContextMenuContent();
}
});