-
Notifications
You must be signed in to change notification settings - Fork 0
/
after-dom-content-loaded.js
executable file
·352 lines (291 loc) · 9.61 KB
/
after-dom-content-loaded.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Globals variables - There are saved in browser's storage
// for view state memorisation
var elementIdsToSave = {};
// Min period that a write can be periodically run
const MIN_PERIOD_FOR_WRITE_OPERATIONS_IN_SECONDS = 1 / (chrome.storage.MAX_WRITE_OPERATIONS_PER_MINUTE / 60) + 0.2;
/**
* EXPANDING and COLLAPSING ACTIONS
* =====================================
*/
function expand(target) {
// already collapsed
if ( target.style.display !== 'none' ) {
return;
}
var parentNode = target.parentNode;
var ellipsis = parentNode.getElementsByClassName('ellipsis')[0];
target.style.display = '';
parentNode.removeChild(ellipsis);
parentNode.getElementsByClassName('collapser')[0].innerHTML = '-';
// for view preservation functionality
if (!elementIdsToSave[parentNode.id]) {
elementIdsToSave[parentNode.id] = true;
updateElementIdsInBrowserStorage();
}
}
function collapse(target) {
// already collapsed
if ( target.style.display === 'none' ) {
return;
}
var ellipsis = document.createElement('span');
var parentNode = target.parentNode;
target.style.display = 'none';
ellipsis.className = 'ellipsis';
ellipsis.innerHTML = ' … ';
parentNode.insertBefore(ellipsis, target);
parentNode.getElementsByClassName('collapser')[0].innerHTML = '+';
// for view preservation functionality
if (elementIdsToSave.hasOwnProperty(parentNode.id) && elementIdsToSave[parentNode.id]) {
elementIdsToSave[parentNode.id] = undefined;
updateElementIdsInBrowserStorage();
}
}
function collapseAll() {
var items = document.getElementsByClassName('collapsible');
// except the first one, '0' is the root
for (var i = 1; i < items.length; i++) {
collapse(items[i]);
}
}
function addCollapser(item) {
var collapser = document.createElement('div');
collapser.className = 'collapser';
collapser.innerHTML = '-';
collapser.addEventListener('click', function (event) {
var collapser = event.target;
var target = collapser.parentNode.getElementsByClassName('collapsible');
if ( ! target.length ) {
return;
}
target = target[0];
if ( target.style.display == 'none' ) {
expand(target);
} else {
collapse(target);
}
}, false);
item.insertBefore(collapser, item.firstChild);
}
var items = document.getElementsByClassName('collapsible');
for( var i = 0; i < items.length; i++) {
addCollapser(items[i].parentNode);
}
document
.getElementById('expandAll')
.addEventListener('click', function() {
var items = document.getElementsByClassName('collapsible');
for( var i = 0; i < items.length; i++) {
expand(items[i]);
}
});
document
.getElementById('collapseAll')
.addEventListener('click', function() {
collapseAll();
});
const HIGHLIGHTED_CLASS_NAME = "highlighted";
/**
* JSON PROPERTY HIGHLIGHTING
* =====================================
*/
function clearPathToProperty() {
// clear the selected one
var highlightedOnes = document.getElementsByClassName(HIGHLIGHTED_CLASS_NAME);
for (var j = 0; j < highlightedOnes.length; j++) {
highlightedOnes[j].className = '';
}
// clear the error
document.getElementById('not-found-property-error').style.display = 'none';
}
document
.getElementById('clearPathToProperty')
.addEventListener('click', function() {
clearPathToProperty();
document.getElementById('pathToProperty').value = '';
updatePathPropertyInBrowserStorage();
});
document
.getElementById('pathToProperty')
.addEventListener('change', function (event) {
updatePathPropertyInBrowserStorage();
});
function updatePathToProperty(newValue, scrollTo) {
document.getElementById('pathToProperty').value = newValue;
if (scrollTo) {
scrollToProperty();
} else {
highlightProperty();
}
}
function highlightProperty() {
clearPathToProperty();
var str = document.getElementById('pathToProperty').value.trim();
if (str === '') {
return;
}
var path = str.split('.');
var tempPath = '';
for (var i = 0; i < path.length; i++) {
tempPath += (i === 0? '': '.') + path[i];
var foundElement = document.getElementById(tempPath);
if (foundElement) {
expand(foundElement.lastElementChild);
} else {
document.getElementById('not-found-property-error').style.display = 'block';
}
}
// highlight
var importantElement = document.getElementById(tempPath);
if (importantElement) {
importantElement.className += ' ' + HIGHLIGHTED_CLASS_NAME;
return importantElement;
} else {
return undefined;
}
}
Element.prototype.documentOffsetTop = function () {
return this.offsetTop + ( this.offsetParent ? this.offsetParent.documentOffsetTop() : 0 );
};
function scrollToTheMiddleOfThePageWith(element) {
if (element) {
var top = element.documentOffsetTop() - ( window.innerHeight / 2 );
window.scrollTo( 0, top );
}
}
function scrollToProperty() {
var highlightedElement = highlightProperty();
scrollToTheMiddleOfThePageWith(highlightedElement);
}
document
.getElementById('pathToProperty')
.addEventListener('keypress', function (e) {
var key = e.which || e.keyCode;
if (key === 13) { // 13 is enter
scrollToProperty();
}
});
document
.getElementById('showPathToProperty')
.addEventListener('click', function() {
scrollToProperty();
});
/*
**
* PATH DETECTOR - FOR ANY SELECTED PROPERTY
* =====================================
*/
var allJsonProperties = document.getElementsByClassName('prop');
for (var i = 0; i < allJsonProperties.length; i++) {
allJsonProperties[i].addEventListener('click', function(event) {
var propertyPath = event.target.parentNode.id;
updatePathToProperty(propertyPath);
updatePathPropertyInBrowserStorage()
});
}
/**
*
* VIEW STATE PRESERVATION
* =====================================
*/
// initially collapse all, in case view state is preseved, this will boots up performance a bit
collapseAll();
const UNSAVED_CHANGES_STATUS = 'Unsaved';
const SAVED_CHANGES_STATUS = 'Saved';
var viewStateChanged = {};
function resetViewStateChangedFlag() {
viewStateChanged = {
'autoPreserveViewState': false,
'elementIds': false,
'pathToProperty': false
};
}
function displayViewStateStatus(text) {
document.getElementById('saveViewStateStatus').innerHTML = text;
}
function updateElementIdsInBrowserStorage() {
viewStateChanged.elementIds = true;
displayViewStateStatus(UNSAVED_CHANGES_STATUS);
}
function updatePathPropertyInBrowserStorage() {
viewStateChanged.pathToProperty = true;
displayViewStateStatus(UNSAVED_CHANGES_STATUS);
}
// Read view states from the storage
chrome.storage.sync.get(['autoPreserveViewState', 'elementIds', 'pathToProperty'], function(items) {
var autoPreserveViewStateCheckbox = document.getElementById('autoPreserveViewStateCheckbox');
if (items['autoPreserveViewState']) {
autoPreserveViewStateCheckbox.checked = true;
var elementIds = items['elementIds'];
var foundElement;
for (var elementId in elementIds) {
if (elementIds.hasOwnProperty(elementId) && elementId) {
foundElement = document.getElementById(elementId);
if (foundElement) {
expand(foundElement.lastElementChild);
}
}
}
if (items['pathToProperty']) {
updatePathToProperty(items['pathToProperty'], true);
}
saveViewStateIntervalID = createSaveViewStateIntervalTask();
displayViewStateStatus(SAVED_CHANGES_STATUS);
} else {
autoPreserveViewStateCheckbox.checked = false;
displayViewStateStatus(UNSAVED_CHANGES_STATUS);
}
});
function writeToBrowserStorage(autoPreserveViewState, elementIds, pathToProperty) {
chrome.storage.sync.set({
'autoPreserveViewState': autoPreserveViewState,
'elementIds': elementIds,
'pathToProperty': pathToProperty
}, function() {
displayViewStateStatus(SAVED_CHANGES_STATUS);
log('Current view state is saved to browser storage.');
});
}
/**
* Save view state to browser storage
*/
function saveViewState() {
if (!viewStateChanged.elementIds && !viewStateChanged.pathToProperty) {
// nothing new to save
return;
}
var pathToProperty = document.getElementById('pathToProperty').value;
writeToBrowserStorage(true, elementIdsToSave, pathToProperty);
resetViewStateChangedFlag();
}
var saveViewStateIntervalID;
/**
* @returns {number} - interval id
*/
function createSaveViewStateIntervalTask() {
return setInterval(saveViewState, MIN_PERIOD_FOR_WRITE_OPERATIONS_IN_SECONDS * 1000);
}
document
.getElementById('autoPreserveViewStateCheckbox')
.addEventListener('change', function(event) {
if (event.target.checked === true) {
// don't create another interval task if there is one already
if (!saveViewStateIntervalID) {
saveViewStateIntervalID = createSaveViewStateIntervalTask();
}
} else {
// cancel existing one
if (saveViewStateIntervalID) {
clearInterval(saveViewStateIntervalID);
saveViewStateIntervalID = undefined;
}
setTimeout(function () {
chrome.storage.sync.set({
'autoPreserveViewState': false
}, function () {
displayViewStateStatus(UNSAVED_CHANGES_STATUS);
log('Auto-preserve is off');
});
}, MIN_PERIOD_FOR_WRITE_OPERATIONS_IN_SECONDS * 1000);
}
});