-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapLayout.js
240 lines (168 loc) · 7.96 KB
/
mapLayout.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
window.onload = () => {
// var mutationObserver = new MutationObserver(function(mutations) {
// mutations.forEach(mutationFunctionDebounced)
// });
// mutationObserver.observe(document.body, {attributes: false, subtree: true, childList: true, characterData: true});
var allElements = document.querySelectorAll(
// 'a[href], button, input, textarea, select, details,[tabindex]:not([tabindex="-1"])'
'a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]'
);
let allElementsArrayUnfiltered = Array.from(allElements);
const allElementsArray2 = allElementsArrayUnfiltered.filter(el =>
!el.hasAttribute('disabled')
&& !el.getAttribute("aria-hidden") );
function isHidden(el) {
var style = window.getComputedStyle(el);
return (style.display === 'none')
}
allElementsArray = allElementsArray2.filter(el => !isHidden(el));
var myMap = new Map();
allElementsArray.map((i, index) => {
var rect = i.getBoundingClientRect();
const newObject = {
"domElement" : i,
"x" : rect.x,
"y" : rect.y
}
i.setAttribute("data-customAttribute", index);
myMap.set(index, newObject);
})
chrome.storage.local.set({"myMap": [...myMap]}, function() {
console.log('myMap committed to storage');
});
var dataMap = new Map();
// key -> index of node
// val -> Array of string [leftIndex, rightIndex]
dataMap.set("0", ["0", "1"])
var i;
for (i=1; i<myMap.size-2; i++) {
dataMap.set(i.toString(), [(i-1).toString(), (i+1).toString()])
}
dataMap.set(i.toString(), [(i-1).toString(), "0"])
console.log(dataMap);
chrome.storage.local.set({"dataMap": [...dataMap]}, function() {
console.log('data map set');
});
chrome.storage.local.set({"currentIndex": "0"}, function() {
console.log('current index set');
});
chrome.storage.local.get("autofocus", function(data) {
var autofocus = data.autofocus;
if (autofocus) {
myMap.get(0)['domElement'].focus();
}
})
document.addEventListener('keydown', function(event) {
var keyPressed = "";
var nextIndex = -1;
if (event.key == "ArrowLeft" && event.shiftKey) {
event.preventDefault();
nextIndex = 0;
} else if (event.key == "ArrowRight" && event.shiftKey) {
event.preventDefault();
nextIndex = 1;
}
var loadedDataMap;
if (nextIndex != -1) {
chrome.storage.local.get(["dataMap", "currentIndex"], function(data) {
loadedDataMap = data.dataMap;
loadedCurrentIndex = data.currentIndex;
// get the data attribute of the active element
var activeElement = document.activeElement;
var lookupIndex = activeElement.getAttribute('data-customAttribute')
// look up the index of the next item
var nextFocusItem = loadedDataMap[lookupIndex][1][nextIndex];
// find and focus on the new item
var newElement = document.querySelectorAll('[data-customAttribute="' + nextFocusItem + '"]');
newElement[0].focus();
// check newly focused element for index
activeElement = document.activeElement;
var newIndexItem = activeElement.getAttribute('data-customAttribute')
// if the index of the new item in focus is the same as the index before,
// then go through a loop to find the next focus-able item
var newIndexItemInt = parseInt(newIndexItem);
var lookupIndexInt = parseInt(lookupIndex);
var newLookupIndex = lookupIndexInt;
while (newIndexItemInt === lookupIndexInt && nextIndex === 1) {
newLookupIndex += 1;
newElement = document.querySelectorAll('[data-customAttribute="' + newLookupIndex + '"]');
newElement[0].focus();
activeElement = document.activeElement;
newIndexItemInt = parseInt(activeElement.getAttribute('data-customAttribute'));
}
if (nextIndex === 1) {
loadedDataMap[lookupIndexInt][1][nextIndex] = newIndexItemInt;
loadedDataMap[newIndexItemInt][1][0] = lookupIndexInt;
}
// add updated map to local storage
chrome.storage.local.set({"dataMap": [...loadedDataMap]}, function() {
});
})
}
})
// function mutationFunction() {
// var allElements = document.querySelectorAll(
// 'a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]'
// );
// let allElementsArrayUnfiltered = Array.from(allElements);
// const allElementsArray2 = allElementsArrayUnfiltered.filter(el =>
// !el.hasAttribute('disabled')
// && !el.getAttribute("aria-hidden") );
// function isHidden(el) {
// var style = window.getComputedStyle(el);
// return (style.display === 'none')
// }
// allElementsArray = allElementsArray2.filter(el => !isHidden(el));
// chrome.storage.local.get(["myMap", "dataMap"], function(data) {
// var mySet = new Set();
// allElementsArray.map((i, index) => {
// mySet.add(index);
// })
// var myMap = data.myMap;
// var dataMap = data.dataMap;
// console.log(myMap);
// var attributeData;
// var dataMapOriginalSize = dataMap.length;
// allElementsArray.map((i, index) => {
// attributeData = parseInt(i.getAttribute('data-customAttribute'));
// console.log(attributeData);
// if (mySet.has(attributeData)) {
// //
// } else {
// var rect = i.getBoundingClientRect();
// const newObject = {
// "domElement" : i,
// "x" : rect.x,
// "y" : rect.y
// }
// console.log(i);
// i.setAttribute("data-customAttribute", index);
// myMap.push([index, newObject])
// }
// })
// chrome.storage.local.set({"myMap": [...myMap]}, function() {
// console.log('myMap set in storage');
// })
// // --------------------------------------------------------------------------------------------------------
// var i;
// for (i=dataMapOriginalSize-1; i<myMap.size-2; i++) {
// dataMap.set(i.toString(), [(i-1).toString(), (i+1).toString()])
// }
// dataMap.set(i.toString(), [(i-1).toString(), i.toString()])
// chrome.storage.local.set({"dataMap": [...dataMap]}, function() {
// console.log('data map set');
// });
// })
// }
// function debounce_leading(func, timeout = 5000){
// let timer;
// return (...args) => {
// clearTimeout(timer);
// timer = setTimeout(() => {
// func.apply(this, args);
// timer = undefined;
// }, timeout);
// };
// }
// const mutationFunctionDebounced = debounce_leading(() => mutationFunction());
}