-
Notifications
You must be signed in to change notification settings - Fork 0
/
notionNotes.js
421 lines (343 loc) · 13.1 KB
/
notionNotes.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// ONLY => used for dev purposes, can && should delete
/* security error specific to Notion when manipulating DOM via extension, works via dev tools > Console
NOTION WARNING: Reverting mutation of childList in component
...
Mutation Record {type:"childList"...}
*/
// ONLY - clear all chrome.storage
// chrome.storage.local.clear(() => {
// let error = chrome.runtime.lastError;
// if (error) {
// console.error(error);
// } else {
// console.log('cleared');
// }
// });
// chrome.storage.local.clear();
// globals
const currentURL = window.location.href;
const head = document.head || document.getElementsByName('head')[0];
const body_div_ID = "#notion-app";
const editTypes = ['h_', 'u_', 'd_'];
let parentTag;
let divTagList = [];
const stickyMenu =
'<div class="sticky-toolbar-container">' +
'<button type="button" class="open-toolbar toggle-toolbar">' +
'<img src="chrome-extension://jmnggonelndondgnmoafagecppgfpdhp/Assets/arrow_left.png">' +
'</button>' +
'<div class="sticky-toolbar">' +
'<button id="highlight">' +
'<img src="chrome-extension://jmnggonelndondgnmoafagecppgfpdhp/Assets/highlighter.png">' +
'</button>' +
'<button id="underline">' +
'<img src="chrome-extension://jmnggonelndondgnmoafagecppgfpdhp/Assets/underline.png">' +
'</button>' +
'<button id="delete">' +
'<img src="chrome-extension://jmnggonelndondgnmoafagecppgfpdhp/Assets/delete.png">' +
'</button>' +
'<button id="summary">' +
'<img src="chrome-extension://jmnggonelndondgnmoafagecppgfpdhp/Assets/summary.png">' +
'</button>' +
'<button id="load">' +
'<img src="chrome-extension://jmnggonelndondgnmoafagecppgfpdhp/Assets/load.png">' +
'</button>' +
'<button type="button" class="close-toolbar toggle-toolbar">' +
'<img src="chrome-extension://jmnggonelndondgnmoafagecppgfpdhp/Assets/arrow_right.png">' +
'</button></div></div>';
const addCSS =
'.sticky-toolbar-container {' +
'position: fixed;' +
'top: 50%;' +
'right: 0;' +
'transform: translateY(-50%);' +
'width: 30px;' +
'z-index: 2;' +
'text-align: center}' +
'.sticky-toolbar-container .toggle-toolbar.open-toolbar {' +
'position: absolute;' +
'top: 50%;' +
'right: 0;' +
'transform: translateY(-50%)}' +
'.sticky-toolbar-container .sticky-toolbar {' +
'display: flex;' +
'flex-direction: column;' +
'transform: translateX(100%)}' +
'.sticky-toolbar-container .toggle-toolbar.open-toolbar, .sticky-toolbar-container .sticky-toolbar {' +
'transition: transform 0.2s;}' +
'.sticky-toolbar-container .sticky-toolbar > *, .sticky-toolbar-container .toggle-toolbar.open-toolbar {' +
'padding: 2px}' +
'.sticky-toolbar-container .sticky-toolbar {' +
'position: relative;' +
'display: inline-block;' +
'margin-bottom: 1px}' +
'.sticky-toolbar-container .sticky-toolbar a::before, .sticky-toolbar-container .sticky-toolbar a::after {' +
'position: absolute;' +
'top: 50%;' +
'transform: translateY(-50%);' +
'opacity: 0;' +
'pointer-events: none;' +
'transition: opacity 0.2s}' +
'.sticky-toolbar-container .sticky-toolbar a:hover::before, .sticky-toolbar-container .sticky-toolbar a:hover::after {' +
'opacity: 1}' +
'.sticky-toolbar-container.show-toolbar .open-toolbar {' +
'transform: translateY(-50%) translateX(100%)}' +
'.sticky-toolbar-container.show-toolbar .sticky-toolbar {' +
'transform: none}' +
'.highlighted{' +
'background-color: yellow}' +
'.underlined{' +
'text-decoration: underline red}' +
'.deleted{' +
'text-decoration: line-through}';
chrome.storage.local.get(null, data => console.info(data));
// console.log([...document.getElementsByTagName('*')]);
// 1. add <style> to <head>
style = document.createElement('style');
style.type = 'text/css';
head.appendChild(style);
// 2. add CSS content to <style>
if (style.styleSheet) {
style.styleSheet.cssText = addCSS;
} else {
style.appendChild(document.createTextNode(addCSS));
}
// 1. add toolbar to (mid-right of) DOM
document.body.insertAdjacentHTML('afterbegin', stickyMenu);
// 2. add toggle to toolbar
const toggleToolbar = document.querySelectorAll(".toggle-toolbar");
const stickyToolbarContainer = document.querySelector(".sticky-toolbar-container");
toggleToolbar.forEach((element) => {
element.addEventListener("click", () => {
stickyToolbarContainer.classList.toggle("show-toolbar");
});
});
// 3. add command to toolbar>buttons
document.getElementById("highlight").addEventListener("click", () => {
highlight();
})
document.getElementById("underline").addEventListener("click", () => {
underline();
})
document.getElementById("delete").addEventListener("click", () => {
lineThrough();
})
document.getElementById("summary").addEventListener("click", () => {
addSummary();
})
// 0. call function
setDivTagList();
// get list of <div>s in page for iteration
// mutation observer as page contains dynamic elements
function setDivTagList() {
parentTag = document.getElementsByClassName("notion-frame")[0];
if (!parentTag) {
setTimeout(setDivTagList, 500);
return;
} else {
let observer = new MutationObserver(record => {
// console.log(record); // ONLY
parentTag = [...document.getElementsByClassName("notion-frame")][0];
divTagList = [...parentTag.getElementsByTagName("div")];
document.getElementById("load").addEventListener("click", () => {
loadChanges();
})
observer.disconnect();
})
observer.observe(parentTag, {
childList: true,
attributes: true,
characterData: true,
subtree: true
})
}
};
// load summary
function loadSummary() {
chrome.storage.local.get(['c_' + currentURL], result => {
// console.log(result); // ONLY
if (Object.keys(result).length > 0) {
shortSummary.value = result['c_' + currentURL];
}
})
}
// load other changes
function loadChanges() {
for (let i = 0; i < editTypes.length; i++) {
chrome.storage.local.get([editTypes[i] + currentURL], result => {
if (Object.keys(result).length > 0) {
if (editTypes[i] === 'h_') {
// console.log(result); // ONLY
for (let j = 0; j < result['h_' + currentURL].length; j++) {
reBuildChanges(
result['h_' + currentURL][j]['containerInnerText'],
result['h_' + currentURL][j]['containerTag'],
result['h_' + currentURL][j]['startOffset'],
result['h_' + currentURL][j]['endOffset']
);
}
} else if (editTypes[i] === 'u_') {
for (let i = 0; i < Object.keys(result).length; i++) {
// コピペ
}
} else if (editTypes[i] === 'd_') {
for (let i = 0; i < Object.keys(result).length; i++) {
// コピペ
}
}
}
})
}
}
// rebuild stored changes
function reBuildChanges(containerInnerText, containerTag, startOffset, endOffset) {
if (containerTag === "DIV") { // reduce load
for (let i = 0; i < divTagList.length; i++) {
if (
divTagList[i].innerText === containerInnerText &&
divTagList[i].childElementCount === 0
// [...divTagList[i].childNodes][0].firstChild === null
){
let targetTag = divTagList[i];
let range = document.createRange();
range.setStart(targetTag.firstChild, startOffset);
range.setEnd(targetTag.firstChild, endOffset);
// return range;
let newElement = document.createElement('span');
newElement.classList.add('highlighted');
range.surroundContents(newElement);
}
}
} else if (containerTag !== "DIV") {
let targetTags = [...parentTag.getElementsByTagName(containerTag)];
// targetTags = [...targetTags];
for (let i = 0; i < targetTags.length; i++) {
if (
targetTags[i].childElementCount === 0 &&
targetTags[i].innerText === containerInnerText
// [...targetTags[i].childNodes][0].firstChild === null
){
// コピペ
}
}
}
}
// set range object
function getSelectedText() {
let selected = window.getSelection();
if (selected.rangeCount && selected.getRangeAt) {
range = selected.getRangeAt(0);
}
if (range) {
selected.removeAllRanges();
selected.addRange(range);
}
console.log(range);
let flag = range.commonAncestorContainer.parentNode;
if (!flag.isContentEditable) {
flag.contentEditable = true;
}
console.log(range);
}
// surround selected text with new tag
function createSpan(addClass) {
try {
let newElement = document.createElement('span');
newElement.classList.add(addClass);
range.surroundContents(newElement);
console.log('surrounded');
} catch (error) {
if (error instanceof DOMException) {
alert('overlapping edits are disallowed');
}
}
}
// store change made to chrome local
function storeChange(command) {
let changesMade = {};
chrome.storage.local.get([command + currentURL], result => {
if (Object.keys(result).length === 0) {
changesMade[command + currentURL] = [
{
'containerTag': range.startContainer.parentNode.tagName,
'containerInnerText': range.commonAncestorContainer.parentNode.innerText,
'startOffset': range.startOffset,
'endOffset': range.endOffset
}
]
chrome.storage.local.set(changesMade, () => {
console.log(changesMade); // ONLY
})
} else {
let addChange = {
'containerTag': range.startContainer.parentNode.tagName,
'containerInnerText': range.commonAncestorContainer.parentNode.innerText,
'startOffset': range.startOffset,
'endOffset': range.endOffset
}
result[command + currentURL].push(addChange);
chrome.storage.local.set(result, () => {
console.log(result); // ONLY
})
}
})
// chrome.storage.local.get(null, data => console.info(data)); // ONLY
}
// highlight selected text
function highlight() {
try {
getSelectedText();
storeChange('h_').then(createSpan('highlighted'));
} catch (error) {
if (error instanceof DOMException) {
alert('overlapping edits are disallowed');
}
}
}
// underline selected text
function underline() {
try {
getSelectedText();
storeChange('u_').then(createSpan('underlined'));
} catch (error) {
if (error instanceof DOMException) {
alert('overlapping edits are disallowed');
}
}
}
// delete (line-through) selected text
function lineThrough() {
try {
getSelectedText();
storeChange('d_').then(createSpan('deleted'));
} catch (error) {
if (error instanceof DOMException) {
alert('overlapping edits are disallowed');
}
}
}
// add summary to page
function addSummary() {
let shortSummary = document.getElementById('shortSummary');
if (!shortSummary) {
const inputElmt = document.createElement('textarea');
inputElmt.setAttribute('id', 'shortSummary');
inputElmt.setAttribute('rows', '4');
inputElmt.setAttribute('cols', '50');
inputElmt.setAttribute('style', 'display: inline');
document.body.prepend(inputElmt);
loadSummary();
} else if (shortSummary && shortSummary.style.display === 'inline') {
if (shortSummary.value) {
let changesMade = {};
changesMade['c_' + currentURL] = shortSummary.value;
chrome.storage.local.set(changesMade, () => {
// console.log('logged summary'); // ONLY
})
// chrome.storage.local.get(null, data => console.info(data)); // ONLY
}
shortSummary.style.display = 'none';
} else if (shortSummary && shortSummary.style.display === 'none') {
shortSummary.style.display = 'inline';
}
}