blob: ab0abca82658900f492e469140e689424aad313b [file] [log] [blame]
avm9996311707032021-02-05 19:11:25 +01001var mutationObserver, intersectionObserver, intersectionOptions, options,
2 authuser;
avm99963cbea3142019-03-28 00:48:15 +01003
avm99963f5923962020-12-07 16:44:37 +01004function removeChildNodes(node) {
5 while (node.firstChild) {
6 node.removeChild(node.firstChild);
avm99963af7860e2019-06-04 03:33:26 +02007 }
avm99963f5923962020-12-07 16:44:37 +01008}
avm99963af7860e2019-06-04 03:33:26 +02009
avm9996390cc2e32021-02-05 18:14:16 +010010function getNParent(node, n) {
11 if (n <= 0) return node;
12 if (!('parentNode' in node)) return null;
13 return getNParent(node.parentNode, n - 1);
14}
15
avm99963f5923962020-12-07 16:44:37 +010016function createExtBadge() {
17 var badge = document.createElement('div');
18 badge.classList.add('TWPT-badge');
19 badge.setAttribute(
20 'title', chrome.i18n.getMessage('inject_extension_badge_helper', [
21 chrome.i18n.getMessage('appName')
22 ]));
23
24 var badgeI = document.createElement('i');
25 badgeI.classList.add('material-icon-i', 'material-icons-extended');
26 badgeI.textContent = 'repeat';
27
28 badge.append(badgeI);
29 return badge;
avm99963af7860e2019-06-04 03:33:26 +020030}
31
avm99963943b8492020-08-31 23:40:43 +020032function addProfileHistoryLink(node, type, query) {
33 var urlpart = encodeURIComponent('query=' + query);
avm99963a2945b62020-11-27 00:32:02 +010034 var authuserpart =
35 (authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser));
avm99963943b8492020-08-31 23:40:43 +020036 var container = document.createElement('div');
37 container.style.margin = '3px 0';
38
39 var link = document.createElement('a');
40 link.setAttribute(
avm99963a2945b62020-11-27 00:32:02 +010041 'href',
42 'https://support.google.com/s/community/search/' + urlpart +
43 authuserpart);
avm99963943b8492020-08-31 23:40:43 +020044 link.innerText = chrome.i18n.getMessage('inject_previousposts_' + type);
45
46 container.appendChild(link);
avm9996306167752020-09-08 00:50:36 +020047 node.appendChild(container);
avm99963943b8492020-08-31 23:40:43 +020048}
49
avm999638e0c1002020-12-03 16:54:20 +010050function applyDragAndDropFix(node) {
51 console.debug('Adding link drag&drop fix to ', node);
52 node.addEventListener('drop', e => {
53 if (e.dataTransfer.types.includes('text/uri-list')) {
54 e.stopImmediatePropagation();
55 console.debug('Stopping link drop event propagation.');
56 }
57 }, true);
58}
59
avm99963f5923962020-12-07 16:44:37 +010060function nodeIsReadToggleBtn(node) {
61 return ('tagName' in node) && node.tagName == 'MATERIAL-BUTTON' &&
62 node.getAttribute('debugid') !== null &&
63 (node.getAttribute('debugid') == 'mark-read-button' ||
64 node.getAttribute('debugid') == 'mark-unread-button') &&
65 ('parentNode' in node) && node.parentNode !== null &&
66 ('parentNode' in node.parentNode) &&
67 node.parentNode.querySelector('[debugid="batchlock"]') === null &&
68 node.parentNode.parentNode !== null &&
69 ('tagName' in node.parentNode.parentNode) &&
70 node.parentNode.parentNode.tagName == 'EC-BULK-ACTIONS';
71}
72
73function addBatchLockBtn(readToggle) {
74 var clone = readToggle.cloneNode(true);
75 clone.setAttribute('debugid', 'batchlock');
76 clone.classList.add('TWPT-btn--with-badge');
77 clone.setAttribute('title', chrome.i18n.getMessage('inject_lockbtn'));
78 clone.querySelector('material-icon').setAttribute('icon', 'lock');
79 clone.querySelector('i.material-icon-i').textContent = 'lock';
80
81 var badge = createExtBadge();
82 clone.append(badge);
83
84 clone.addEventListener('click', function() {
85 var modal = document.querySelector('.pane[pane-id="default-1"]');
86
87 var dialog = document.createElement('material-dialog');
88 dialog.setAttribute('role', 'dialog');
89 dialog.setAttribute('aria-modal', 'true');
90 dialog.classList.add('TWPT-dialog');
91
92 var header = document.createElement('header');
93 header.setAttribute('role', 'presentation');
94 header.classList.add('TWPT-dialog-header');
95
96 var title = document.createElement('div');
97 title.classList.add('TWPT-dialog-header--title', 'title');
98 title.textContent = chrome.i18n.getMessage('inject_lockbtn');
99
100 header.append(title);
101
102 var main = document.createElement('main');
103 main.setAttribute('role', 'presentation');
104 main.classList.add('TWPT-dialog-main');
105
106 var p = document.createElement('p');
107 p.textContent = chrome.i18n.getMessage('inject_lockdialog_desc');
108
109 main.append(p);
110
111 dialog.append(header, main);
112
113 var footers = [['lock', 'unlock', 'cancel'], ['reload', 'close']];
114
115 for (var i = 0; i < footers.length; ++i) {
116 var footer = document.createElement('footer');
117 footer.setAttribute('role', 'presentation');
118 footer.classList.add('TWPT-dialog-footer');
119 footer.setAttribute('data-footer-id', i);
120
121 if (i > 0) footer.classList.add('is-hidden');
122
123 footers[i].forEach(action => {
124 var btn = document.createElement('material-button');
125 btn.setAttribute('role', 'button');
126 btn.classList.add('TWPT-dialog-footer-btn');
127 if (i == 1) btn.classList.add('is-disabled');
128
129 switch (action) {
130 case 'lock':
131 case 'unlock':
132 btn.addEventListener('click', _ => {
133 if (btn.classList.contains('is-disabled')) return;
134 var message = {
135 action,
136 prefix: 'TWPT-batchlock',
137 };
138 window.postMessage(message, '*');
139 });
140 break;
141
142 case 'cancel':
143 case 'close':
144 btn.addEventListener('click', _ => {
145 if (btn.classList.contains('is-disabled')) return;
146 modal.classList.remove('visible');
147 modal.style.display = 'none';
148 removeChildNodes(modal);
149 });
150 break;
151
152 case 'reload':
153 btn.addEventListener('click', _ => {
154 if (btn.classList.contains('is-disabled')) return;
155 window.location.reload()
156 });
157 break;
158 }
159
160 var content = document.createElement('div');
161 content.classList.add('content', 'TWPT-dialog-footer-btn--content');
162 content.textContent =
163 chrome.i18n.getMessage('inject_lockdialog_btn_' + action);
164
165 btn.append(content);
166 footer.append(btn);
167 });
168
169 var clear = document.createElement('div');
170 clear.style.clear = 'both';
171
172 footer.append(clear);
173 dialog.append(footer);
174 }
175
176 removeChildNodes(modal);
177 modal.append(dialog);
178 modal.classList.add('visible', 'modal');
179 modal.style.display = 'flex';
180 });
181 readToggle.parentNode.insertBefore(
182 clone, (readToggle.nextSibling || readToggle));
183}
184
avm9996390cc2e32021-02-05 18:14:16 +0100185function injectPreviousPostsLinks(nameElement) {
186 var mainCardContent = getNParent(nameElement, 3);
187 if (mainCardContent === null) {
188 console.error(
189 '[previousposts] Couldn\'t find |.main-card-content| element.');
190 return;
avm99963490114d2021-02-05 16:12:20 +0100191 }
avm9996390cc2e32021-02-05 18:14:16 +0100192
193 var forumId = location.href.split('/forum/')[1].split('/')[0] || '0';
194
195 var name = escapeUsername(nameElement.textContent);
196 var query1 = encodeURIComponent(
197 '(creator:"' + name + '" | replier:"' + name + '") forum:' + forumId);
198 var query2 = encodeURIComponent(
199 '(creator:"' + name + '" | replier:"' + name + '") forum:any');
200
201 var container = document.createElement('div');
202 container.classList.add('TWPT-previous-posts');
203
204 var badge = createExtBadge();
205 container.appendChild(badge);
206
207 var linkContainer = document.createElement('div');
208 linkContainer.classList.add('TWPT-previous-posts--links');
209
210 addProfileHistoryLink(linkContainer, 'forum', query1);
211 addProfileHistoryLink(linkContainer, 'all', query2);
212
213 container.appendChild(linkContainer);
214
215 mainCardContent.appendChild(container);
avm99963490114d2021-02-05 16:12:20 +0100216}
217
218const watchedNodesSelectors = [
avm9996311707032021-02-05 19:11:25 +0100219 // App container (used to set up the intersection observer)
220 'ec-app',
221
avm99963490114d2021-02-05 16:12:20 +0100222 // Load more bar (for the "load more"/"load all" buttons)
223 '.load-more-bar',
224
avm9996390cc2e32021-02-05 18:14:16 +0100225 // Username span inside ec-user (user profile view)
226 'ec-user .main-card .header > .name > span',
avm99963490114d2021-02-05 16:12:20 +0100227
228 // Rich text editor
229 'ec-movable-dialog',
230 'ec-rich-text-editor',
231
232 // Read/unread bulk action in the list of thread, for the batch lock feature
233 'ec-bulk-actions material-button[debugid="mark-read-button"]',
234 'ec-bulk-actions material-button[debugid="mark-unread-button"]',
235];
236
237function handleCandidateNode(node) {
238 if (typeof node.classList !== 'undefined') {
avm9996311707032021-02-05 19:11:25 +0100239 // Set up the intersectionObserver
240 if (typeof intersectionObserver === 'undefined' && ('tagName' in node) &&
241 node.tagName == 'EC-APP') {
242 intersectionOptions = {
243 root: node.querySelector('.scrollable-content'),
244 rootMargin: '0px',
245 threshold: 1.0,
246 };
247
248 intersectionObserver =
249 new IntersectionObserver(intersectionCallback, intersectionOptions);
avm99963490114d2021-02-05 16:12:20 +0100250 }
251
avm9996311707032021-02-05 19:11:25 +0100252 // Start the intersectionObserver for the "load more"/"load all" buttons
253 // inside a thread
254 if ((options.thread || options.threadall) &&
255 node.classList.contains('load-more-bar')) {
256 if (typeof intersectionObserver !== 'undefined') {
257 if (options.thread)
258 intersectionObserver.observe(node.querySelector('.load-more-button'));
259 if (options.threadall)
260 intersectionObserver.observe(node.querySelector('.load-all-button'));
261 } else {
262 console.warn(
263 '[infinitescroll] ' +
264 'The intersectionObserver is not ready yet.');
265 }
avm99963490114d2021-02-05 16:12:20 +0100266 }
267
268 // Show the "previous posts" links
269 // Here we're selecting the 'ec-user > div' element (unique child)
avm9996390cc2e32021-02-05 18:14:16 +0100270 if (options.history &&
271 node.matches('ec-user .main-card .header > .name > span')) {
avm99963490114d2021-02-05 16:12:20 +0100272 injectPreviousPostsLinks(node);
273 }
274
275 // Fix the drag&drop issue with the rich text editor
276 //
277 // We target both tags because in different contexts different
278 // elements containing the text editor get added to the DOM structure.
279 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
280 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
281 // directly.
282 if (options.ccdragndropfix && ('tagName' in node) &&
283 (node.tagName == 'EC-MOVABLE-DIALOG' ||
284 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
285 applyDragAndDropFix(node);
286 }
287
288 // Inject the batch lock button in the thread list
289 if (options.batchlock && nodeIsReadToggleBtn(node)) {
290 addBatchLockBtn(node);
291 }
292 }
293}
294
avm99963847ee632019-03-27 00:57:44 +0100295function mutationCallback(mutationList, observer) {
296 mutationList.forEach((mutation) => {
avm99963b69eb3d2020-08-20 02:03:44 +0200297 if (mutation.type == 'childList') {
298 mutation.addedNodes.forEach(function(node) {
avm99963490114d2021-02-05 16:12:20 +0100299 handleCandidateNode(node);
avm99963847ee632019-03-27 00:57:44 +0100300 });
301 }
302 });
303}
304
avm99963adf90862020-04-12 13:27:45 +0200305function intersectionCallback(entries, observer) {
avm99963847ee632019-03-27 00:57:44 +0100306 entries.forEach(entry => {
307 if (entry.isIntersecting) {
308 entry.target.click();
309 }
310 });
311};
312
313var observerOptions = {
314 childList: true,
avm99963b69eb3d2020-08-20 02:03:44 +0200315 subtree: true,
avm99963129fb502020-08-28 05:18:53 +0200316};
avm99963847ee632019-03-27 00:57:44 +0100317
avm99963129fb502020-08-28 05:18:53 +0200318chrome.storage.sync.get(null, function(items) {
319 options = items;
avm99963cbea3142019-03-28 00:48:15 +0100320
avm99963a2945b62020-11-27 00:32:02 +0100321 var startup =
322 JSON.parse(document.querySelector('html').getAttribute('data-startup'));
323 authuser = startup[2][1] || '0';
324
avm99963490114d2021-02-05 16:12:20 +0100325 // Before starting the mutation Observer, check whether we missed any
avm9996311707032021-02-05 19:11:25 +0100326 // mutations by manually checking whether some watched nodes already
327 // exist.
avm99963490114d2021-02-05 16:12:20 +0100328 var cssSelectors = watchedNodesSelectors.join(',');
329 document.querySelectorAll(cssSelectors)
330 .forEach(node => handleCandidateNode(node));
331
avm99963129fb502020-08-28 05:18:53 +0200332 mutationObserver = new MutationObserver(mutationCallback);
avm99963e4cac402020-12-03 16:10:58 +0100333 mutationObserver.observe(document.body, observerOptions);
avm99963cbea3142019-03-28 00:48:15 +0100334
avm99963129fb502020-08-28 05:18:53 +0200335 if (options.fixedtoolbar) {
336 injectStyles(
avm999630bc113a2020-09-07 13:02:11 +0200337 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
avm99963129fb502020-08-28 05:18:53 +0200338 }
avm99963ae6a26d2020-04-12 14:03:51 +0200339
avm99963129fb502020-08-28 05:18:53 +0200340 if (options.increasecontrast) {
avm999630bc113a2020-09-07 13:02:11 +0200341 injectStyles(
avm99963a2a06442020-11-25 21:11:10 +0100342 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
avm99963129fb502020-08-28 05:18:53 +0200343 }
avm999630f9503f2020-07-27 13:56:52 +0200344
avm99963129fb502020-08-28 05:18:53 +0200345 if (options.stickysidebarheaders) {
346 injectStyles(
avm999630bc113a2020-09-07 13:02:11 +0200347 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
348 }
349
350 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
avm999630bc113a2020-09-07 13:02:11 +0200351 var darkThemeSwitch = document.createElement('material-button');
avm99963f5923962020-12-07 16:44:37 +0100352 darkThemeSwitch.classList.add('TWPT-dark-theme', 'TWPT-btn--with-badge');
avm999630bc113a2020-09-07 13:02:11 +0200353 darkThemeSwitch.setAttribute('button', '');
354 darkThemeSwitch.setAttribute(
355 'title', chrome.i18n.getMessage('inject_ccdarktheme_helper'));
356
357 darkThemeSwitch.addEventListener('click', e => {
358 chrome.storage.sync.get(null, currentOptions => {
359 currentOptions.ccdarktheme_switch_status =
360 !options.ccdarktheme_switch_status;
361 chrome.storage.sync.set(currentOptions, _ => {
362 location.reload();
363 });
364 });
365 });
366
367 var switchContent = document.createElement('div');
368 switchContent.classList.add('content');
369
370 var icon = document.createElement('material-icon');
371
372 var i = document.createElement('i');
373 i.classList.add('material-icon-i', 'material-icons-extended');
374 i.textContent = 'brightness_4';
375
376 icon.appendChild(i);
377 switchContent.appendChild(icon);
378 darkThemeSwitch.appendChild(switchContent);
379
avm99963f5923962020-12-07 16:44:37 +0100380 var badgeContent = createExtBadge();
avm9996306167752020-09-08 00:50:36 +0200381
avm9996306167752020-09-08 00:50:36 +0200382 darkThemeSwitch.appendChild(badgeContent);
383
avm999630bc113a2020-09-07 13:02:11 +0200384 var rightControl = document.querySelector('header .right-control');
385 rightControl.style.width =
386 (parseInt(window.getComputedStyle(rightControl).width) + 58) + 'px';
387 rightControl.insertAdjacentElement('afterbegin', darkThemeSwitch);
avm99963129fb502020-08-28 05:18:53 +0200388 }
avm99963129942f2020-09-08 02:07:18 +0200389
390 if (options.ccforcehidedrawer) {
391 var drawer = document.querySelector('material-drawer');
392 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
393 document.querySelector('.material-drawer-button').click();
394 }
395 }
avm99963f5923962020-12-07 16:44:37 +0100396
397 if (options.batchlock) {
398 injectScript(chrome.runtime.getURL('injections/batchlock_inject.js'));
399 }
avm99963129fb502020-08-28 05:18:53 +0200400});