avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 1 | var mutationObserver, intersectionObserver, options, authuser; |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 2 | |
avm99963 | af7860e | 2019-06-04 03:33:26 +0200 | [diff] [blame] | 3 | function parseUrl(url) { |
| 4 | var forum_a = url.match(/forum\/([0-9]+)/i); |
| 5 | var thread_a = url.match(/thread\/([0-9]+)/i); |
| 6 | |
| 7 | if (forum_a === null || thread_a === null) { |
| 8 | return false; |
| 9 | } |
| 10 | |
| 11 | return { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 12 | 'forum': forum_a[1], |
| 13 | 'thread': thread_a[1], |
avm99963 | af7860e | 2019-06-04 03:33:26 +0200 | [diff] [blame] | 14 | }; |
| 15 | } |
| 16 | |
avm99963 | 943b849 | 2020-08-31 23:40:43 +0200 | [diff] [blame] | 17 | function addProfileHistoryLink(node, type, query) { |
| 18 | var urlpart = encodeURIComponent('query=' + query); |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 19 | var authuserpart = |
| 20 | (authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser)); |
avm99963 | 943b849 | 2020-08-31 23:40:43 +0200 | [diff] [blame] | 21 | var container = document.createElement('div'); |
| 22 | container.style.margin = '3px 0'; |
| 23 | |
| 24 | var link = document.createElement('a'); |
| 25 | link.setAttribute( |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 26 | 'href', |
| 27 | 'https://support.google.com/s/community/search/' + urlpart + |
| 28 | authuserpart); |
avm99963 | 943b849 | 2020-08-31 23:40:43 +0200 | [diff] [blame] | 29 | link.innerText = chrome.i18n.getMessage('inject_previousposts_' + type); |
| 30 | |
| 31 | container.appendChild(link); |
avm99963 | 0616775 | 2020-09-08 00:50:36 +0200 | [diff] [blame] | 32 | node.appendChild(container); |
avm99963 | 943b849 | 2020-08-31 23:40:43 +0200 | [diff] [blame] | 33 | } |
| 34 | |
avm99963 | 8e0c100 | 2020-12-03 16:54:20 +0100 | [diff] [blame] | 35 | function applyDragAndDropFix(node) { |
| 36 | console.debug('Adding link drag&drop fix to ', node); |
| 37 | node.addEventListener('drop', e => { |
| 38 | if (e.dataTransfer.types.includes('text/uri-list')) { |
| 39 | e.stopImmediatePropagation(); |
| 40 | console.debug('Stopping link drop event propagation.'); |
| 41 | } |
| 42 | }, true); |
| 43 | } |
| 44 | |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 45 | function mutationCallback(mutationList, observer) { |
| 46 | mutationList.forEach((mutation) => { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 47 | if (mutation.type == 'childList') { |
| 48 | mutation.addedNodes.forEach(function(node) { |
| 49 | if (typeof node.classList !== 'undefined') { |
| 50 | if (options.thread && node.classList.contains('load-more-bar')) { |
| 51 | intersectionObserver.observe( |
| 52 | node.querySelector('.load-more-button')); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 53 | } |
| 54 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 55 | if (options.threadall && node.classList.contains('load-more-bar')) { |
| 56 | intersectionObserver.observe( |
| 57 | node.querySelector('.load-all-button')); |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 58 | } |
| 59 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 60 | if (options.history && ('parentNode' in node) && |
| 61 | node.parentNode !== null && ('tagName' in node.parentNode) && |
| 62 | node.parentNode.tagName == 'EC-USER') { |
| 63 | var nameElement = node.querySelector('.name span'); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 64 | if (nameElement !== null) { |
avm99963 | 943b849 | 2020-08-31 23:40:43 +0200 | [diff] [blame] | 65 | var forumId = |
| 66 | location.href.split('/forum/')[1].split('/')[0] || '0'; |
| 67 | |
avm99963 | 14116b0 | 2020-11-02 14:04:14 +0100 | [diff] [blame] | 68 | var name = escapeUsername(nameElement.textContent); |
avm99963 | 943b849 | 2020-08-31 23:40:43 +0200 | [diff] [blame] | 69 | var query1 = encodeURIComponent( |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 70 | '(creator:"' + name + '" | replier:"' + name + |
| 71 | '") forum:' + forumId); |
avm99963 | 943b849 | 2020-08-31 23:40:43 +0200 | [diff] [blame] | 72 | var query2 = encodeURIComponent( |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 73 | '(creator:"' + name + '" | replier:"' + name + |
| 74 | '") forum:any'); |
avm99963 | 0616775 | 2020-09-08 00:50:36 +0200 | [diff] [blame] | 75 | |
| 76 | var container = document.createElement('div'); |
| 77 | container.classList.add('TWPT-previous-posts'); |
| 78 | |
| 79 | var badge = document.createElement('div'); |
| 80 | badge.classList.add('TWPT-badge'); |
| 81 | badge.setAttribute( |
| 82 | 'title', |
| 83 | chrome.i18n.getMessage( |
| 84 | 'inject_extension_badge_helper', |
| 85 | [chrome.i18n.getMessage('appName')])); |
| 86 | |
| 87 | var badgeI = document.createElement('i'); |
| 88 | badgeI.classList.add( |
| 89 | 'material-icon-i', 'material-icons-extended'); |
| 90 | badgeI.textContent = 'repeat'; |
| 91 | |
| 92 | badge.appendChild(badgeI); |
| 93 | container.appendChild(badge); |
| 94 | |
| 95 | var linkContainer = document.createElement('div'); |
| 96 | linkContainer.classList.add('TWPT-previous-posts--links'); |
| 97 | |
| 98 | addProfileHistoryLink(linkContainer, 'forum', query1); |
| 99 | addProfileHistoryLink(linkContainer, 'all', query2); |
| 100 | |
| 101 | container.appendChild(linkContainer); |
| 102 | |
| 103 | node.querySelector('.main-card-content').appendChild(container); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 104 | } |
| 105 | } |
avm99963 | 8e0c100 | 2020-12-03 16:54:20 +0100 | [diff] [blame] | 106 | |
| 107 | // We target both tags because in different contexts different |
| 108 | // elements containing the text editor get added to the DOM structure. |
| 109 | // Sometimes it's a EC-MOVABLE-DIALOG which already contains the |
| 110 | // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR |
| 111 | // directly. |
| 112 | if (options.ccdragndropfix && ('tagName' in node) && |
| 113 | (node.tagName == 'EC-MOVABLE-DIALOG' || |
| 114 | node.tagName == 'EC-RICH-TEXT-EDITOR')) { |
| 115 | applyDragAndDropFix(node); |
| 116 | } |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 117 | } |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 118 | }); |
| 119 | } |
| 120 | }); |
| 121 | } |
| 122 | |
avm99963 | adf9086 | 2020-04-12 13:27:45 +0200 | [diff] [blame] | 123 | function intersectionCallback(entries, observer) { |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 124 | entries.forEach(entry => { |
| 125 | if (entry.isIntersecting) { |
| 126 | entry.target.click(); |
| 127 | } |
| 128 | }); |
| 129 | }; |
| 130 | |
| 131 | var observerOptions = { |
| 132 | childList: true, |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 133 | subtree: true, |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 134 | } |
| 135 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 136 | var intersectionOptions = { |
| 137 | root: document.querySelector('.scrollable-content'), |
| 138 | rootMargin: '0px', |
| 139 | threshold: 1.0, |
| 140 | }; |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 141 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 142 | chrome.storage.sync.get(null, function(items) { |
| 143 | options = items; |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 144 | |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 145 | var startup = |
| 146 | JSON.parse(document.querySelector('html').getAttribute('data-startup')); |
| 147 | authuser = startup[2][1] || '0'; |
| 148 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 149 | mutationObserver = new MutationObserver(mutationCallback); |
avm99963 | e4cac40 | 2020-12-03 16:10:58 +0100 | [diff] [blame] | 150 | mutationObserver.observe(document.body, observerOptions); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 151 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 152 | intersectionObserver = |
| 153 | new IntersectionObserver(intersectionCallback, intersectionOptions); |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 154 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 155 | if (options.fixedtoolbar) { |
| 156 | injectStyles( |
avm99963 | 0bc113a | 2020-09-07 13:02:11 +0200 | [diff] [blame] | 157 | 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}'); |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 158 | } |
avm99963 | ae6a26d | 2020-04-12 14:03:51 +0200 | [diff] [blame] | 159 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 160 | if (options.increasecontrast) { |
avm99963 | 0bc113a | 2020-09-07 13:02:11 +0200 | [diff] [blame] | 161 | injectStyles( |
avm99963 | a2a0644 | 2020-11-25 21:11:10 +0100 | [diff] [blame] | 162 | '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}'); |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 163 | } |
avm99963 | 0f9503f | 2020-07-27 13:56:52 +0200 | [diff] [blame] | 164 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 165 | if (options.stickysidebarheaders) { |
| 166 | injectStyles( |
avm99963 | 0bc113a | 2020-09-07 13:02:11 +0200 | [diff] [blame] | 167 | 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}'); |
| 168 | } |
| 169 | |
| 170 | if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') { |
| 171 | injectStylesheet( |
| 172 | chrome.runtime.getURL('injections/ccdarktheme_switch.css')); |
| 173 | |
| 174 | var darkThemeSwitch = document.createElement('material-button'); |
| 175 | darkThemeSwitch.classList.add('TWPT-dark-theme'); |
| 176 | darkThemeSwitch.setAttribute('button', ''); |
| 177 | darkThemeSwitch.setAttribute( |
| 178 | 'title', chrome.i18n.getMessage('inject_ccdarktheme_helper')); |
| 179 | |
| 180 | darkThemeSwitch.addEventListener('click', e => { |
| 181 | chrome.storage.sync.get(null, currentOptions => { |
| 182 | currentOptions.ccdarktheme_switch_status = |
| 183 | !options.ccdarktheme_switch_status; |
| 184 | chrome.storage.sync.set(currentOptions, _ => { |
| 185 | location.reload(); |
| 186 | }); |
| 187 | }); |
| 188 | }); |
| 189 | |
| 190 | var switchContent = document.createElement('div'); |
| 191 | switchContent.classList.add('content'); |
| 192 | |
| 193 | var icon = document.createElement('material-icon'); |
| 194 | |
| 195 | var i = document.createElement('i'); |
| 196 | i.classList.add('material-icon-i', 'material-icons-extended'); |
| 197 | i.textContent = 'brightness_4'; |
| 198 | |
| 199 | icon.appendChild(i); |
| 200 | switchContent.appendChild(icon); |
| 201 | darkThemeSwitch.appendChild(switchContent); |
| 202 | |
avm99963 | 0616775 | 2020-09-08 00:50:36 +0200 | [diff] [blame] | 203 | var badgeContent = document.createElement('div'); |
| 204 | badgeContent.classList.add('TWPT-badge'); |
| 205 | badgeContent.setAttribute( |
| 206 | 'title', chrome.i18n.getMessage('inject_extension_badge_helper', [ |
| 207 | chrome.i18n.getMessage('appName') |
| 208 | ])); |
| 209 | |
| 210 | var badgeI = document.createElement('i'); |
| 211 | badgeI.classList.add('material-icon-i', 'material-icons-extended'); |
| 212 | badgeI.textContent = 'repeat'; |
| 213 | |
| 214 | badgeContent.appendChild(badgeI); |
| 215 | darkThemeSwitch.appendChild(badgeContent); |
| 216 | |
avm99963 | 0bc113a | 2020-09-07 13:02:11 +0200 | [diff] [blame] | 217 | var rightControl = document.querySelector('header .right-control'); |
| 218 | rightControl.style.width = |
| 219 | (parseInt(window.getComputedStyle(rightControl).width) + 58) + 'px'; |
| 220 | rightControl.insertAdjacentElement('afterbegin', darkThemeSwitch); |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 221 | } |
avm99963 | 129942f | 2020-09-08 02:07:18 +0200 | [diff] [blame] | 222 | |
| 223 | if (options.ccforcehidedrawer) { |
| 224 | var drawer = document.querySelector('material-drawer'); |
| 225 | if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) { |
| 226 | document.querySelector('.material-drawer-button').click(); |
| 227 | } |
| 228 | } |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 229 | }); |