-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
355 lines (326 loc) · 10.2 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
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
/* global tinymce, chrome */
import tinymceConfig from './config/tinymce';
import debounce from './utils/debounce';
import extend from './utils/extend';
import frontToBack from './utils/front-to-back';
import defaultConfig from './config/extension-defaults';
import addImagePreviews from './utils/add-image-previews';
import highlightCode from './utils/highlight-code';
import injectAgent from './utils/agent';
import profiler from './utils/profiler';
import expandThreads from './utils/expand-threads';
import twemojiSetup from './utils/twemoji';
/**
* Main function which initializes editor
*/
const fn = function () {
/**
* First we should get config
*/
chrome.storage.sync.get(function (conf) {
let extensionConfig = extend({}, defaultConfig, conf);
if (extensionConfig.profiler) {
profiler.enable();
} else {
profiler.disable();
}
injectAgent();
let defaultMCEConfig = tinymceConfig(extensionConfig);
/**
* Base url for icons and stuff like that
*/
tinymce.baseURL = chrome.runtime.getURL("vendor/tinymce");
/**
* Callbacks for comments mutation observer to detect comment's submission
*/
let commentsMutationCallbacks = [];
const targetNode = document.getElementById('commentsList');
if (targetNode) {
const mutationConfig = {subtree: true, childList: true};
const callback = debounce(function (mutations) {
let edited = false, added = false;
mutations.forEach(mutation => {
if (mutation.target && mutation.target.id === "commentsList") {
mutation.addedNodes.forEach(node => {
if (node.classList && node.classList.contains("b-comment")) {
added = true;
}
});
}
if (
mutation.target &&
mutation.target.classList &&
mutation.target.classList.contains("b-typo")
) {
mutation.addedNodes.forEach(node => {
if (
node.classList &&
!node.classList.contains("dou-enhancer-image-preview") &&
!node.classList.contains("dou-enhancer-lightbox")
) {
edited = true;
}
});
}
});
const args = arguments;
commentsMutationCallbacks.forEach(cb => {
cb(added, edited, ...args);
});
}, 100);
const observer = new MutationObserver(callback);
observer.observe(targetNode, mutationConfig);
}
const focusBeforeTemplate = function (editor) {
editor.getContainer()
.querySelector('.mce-panel button .mce-i-template')
.parentElement
.addEventListener('click', function () {
editor.focus();
});
};
/**
* Config for first-level comment form
*/
let defaultFormConfig = extend(true, {}, defaultMCEConfig, {
selector: '#inlineForm textarea',
init_instance_callback(editor) {
commentsMutationCallbacks.push(function (added) {
if (added) {
editor.load();
}
});
/**
* On focus imitate original textarea focus event and update tinymce to fix placeholder
*/
editor.on('Focus', function () {
frontToBack({
target: 'defaultForm',
evt: 'focus'
});
setTimeout(() => {
editor.load();
}, 10);
});
/**
* On blur imitate original blur event
*/
editor.on('Blur', function () {
frontToBack({
target: 'defaultForm',
evt: 'blur'
});
setTimeout(() => {
editor.load();
}, 10);
});
/**
* On change and keyup update original textarea (tinymce does not do this)
*/
const changeHandler = function () {
const textarea = document.querySelector('#inlineForm textarea');
if (textarea) {
editor.save();
let evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
textarea.dispatchEvent(evt);
}
};
editor.on('Change', changeHandler);
editor.on('keyup', changeHandler);
editor.shortcuts.add("ctrl+13", "Submit!", function () {
frontToBack({
target: 'defaultForm',
evt: 'enter'
});
});
focusBeforeTemplate(editor);
}
});
tinymce.init(defaultFormConfig);
/**
* Config for "floating" form (answers to comments)
*/
let floatFormConfig = extend(true, {}, defaultMCEConfig, {
selector: '#floatForm textarea',
init_instance_callback(editor) {
/**
* Listen for messages from "other side"
*/
window.addEventListener("backToFront", function (e) {
if (e.detail) {
switch (e.detail.target) {
case 'floatForm':
switch (e.detail.evt) {
case 'close':
/**
* Remove editor when user closes floating form
*/
editor.remove();
break;
}
break;
}
}
});
editor.on('Focus', function () {
frontToBack({
target: 'floatForm',
evt: 'focus'
});
setTimeout(() => {
editor.load();
}, 10);
});
editor.on('Blur', function () {
frontToBack({
target: 'floatForm',
evt: 'blur'
});
setTimeout(() => {
editor.load();
}, 10);
});
/**
* On change and keyup update original textarea (tinymce does not do this)
*/
const changeHandler = function () {
const textarea = document.querySelector('#floatForm textarea');
if (textarea) {
editor.save();
let evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
textarea.dispatchEvent(evt);
}
};
editor.on('Change', changeHandler);
editor.on('keyup', changeHandler);
editor.shortcuts.add("ctrl+13", "Submit!", function () {
frontToBack({
target: 'floatForm',
evt: 'enter'
});
});
focusBeforeTemplate(editor);
/**
* Three lines below should move cursor to the end of text (when editing comment) but unfortunately this
* method doesn't work for some reason. I'll left this code here, anyway. Maybe I'll find a way to fix it.
*/
editor.focus();
editor.selection.select(editor.getBody().lastChild, true);
editor.selection.collapse(false);
/**
* Need to call change handler to disable button if editor is empty
*/
changeHandler();
}
});
/**
* Listen for messages from other side
*/
window.addEventListener("backToFront", function (e) {
if (e.detail) {
switch (e.detail.target) {
case 'floatForm':
switch (e.detail.evt) {
case 'insert':
/**
* Floating form is open - init tinymce
*/
tinymce.init(floatFormConfig);
break;
case 'insert-edit': {
let timer;
/**
* Floating form is open in edit mode - init tinymce, wait for content and update editor
*/
const targetNode = document.querySelector('#floatForm textarea');
const mutationConfig = {attributes: true, characterData: true, childList: true};
const callback = debounce(function (changes, observer) {
clearTimeout(timer);
observer.disconnect();
tinymce.init(floatFormConfig);
}, 100);
const observer = new MutationObserver(callback);
observer.observe(targetNode, mutationConfig);
/**
* Ensure that callback will be called in cases of too fast/slow connection
*/
timer = setTimeout(function () {
callback(null, observer);
}, 5000);
break;
}
}
break;
}
}
});
/**
* Add image and video previews
*/
addImagePreviews(extensionConfig);
/**
* Highlight all code
*/
highlightCode(extensionConfig);
/**
* Expand all threads
*/
if (extensionConfig.expandThreads) {
expandThreads();
}
/**
* Replace emojis with pictures
*/
if (extensionConfig.twemoji) {
twemojiSetup();
}
/**
* When comment list changes - update previews and code highlighting
* TODO: Try to modify comments manager to know exactly when new comments arrive. Mutation observer is not the best
* tool for what we need
*/
commentsMutationCallbacks.push(function (added, edited) {
if (added || edited) {
addImagePreviews(extensionConfig);
highlightCode(extensionConfig);
if (extensionConfig.expandThreads) {
expandThreads();
}
if (extensionConfig.twemoji) {
twemojiSetup();
}
}
});
/**
* Listen for storage changes
*/
chrome.storage.onChanged.addListener((changes, area) => {
if (area === 'sync') {
chrome.storage.sync.get(function (conf) {
extensionConfig = extend({}, defaultConfig, conf);
if (extensionConfig.profiler) {
profiler.enable();
} else {
profiler.disable();
}
addImagePreviews(extensionConfig, true);
highlightCode(extensionConfig);
if (extensionConfig.expandThreads) {
expandThreads();
}
if (extensionConfig.twemoji) {
twemojiSetup();
}
});
}
});
});
};
if (["complete", "interactive"].indexOf(document.readyState) > -1) {
fn();
}
else {
document.addEventListener("DOMContentLoaded", fn);
}