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 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 35 | function mutationCallback(mutationList, observer) { |
| 36 | mutationList.forEach((mutation) => { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 37 | if (mutation.type == 'childList') { |
| 38 | mutation.addedNodes.forEach(function(node) { |
| 39 | if (typeof node.classList !== 'undefined') { |
| 40 | if (options.thread && node.classList.contains('load-more-bar')) { |
| 41 | intersectionObserver.observe( |
| 42 | node.querySelector('.load-more-button')); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 43 | } |
| 44 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 45 | if (options.threadall && node.classList.contains('load-more-bar')) { |
| 46 | intersectionObserver.observe( |
| 47 | node.querySelector('.load-all-button')); |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 48 | } |
| 49 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 50 | if (options.history && ('parentNode' in node) && |
| 51 | node.parentNode !== null && ('tagName' in node.parentNode) && |
| 52 | node.parentNode.tagName == 'EC-USER') { |
| 53 | var nameElement = node.querySelector('.name span'); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 54 | if (nameElement !== null) { |
avm99963 | 943b849 | 2020-08-31 23:40:43 +0200 | [diff] [blame] | 55 | var forumId = |
| 56 | location.href.split('/forum/')[1].split('/')[0] || '0'; |
| 57 | |
avm99963 | 14116b0 | 2020-11-02 14:04:14 +0100 | [diff] [blame] | 58 | var name = escapeUsername(nameElement.textContent); |
avm99963 | 943b849 | 2020-08-31 23:40:43 +0200 | [diff] [blame] | 59 | var query1 = encodeURIComponent( |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 60 | '(creator:"' + name + '" | replier:"' + name + |
| 61 | '") forum:' + forumId); |
avm99963 | 943b849 | 2020-08-31 23:40:43 +0200 | [diff] [blame] | 62 | var query2 = encodeURIComponent( |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 63 | '(creator:"' + name + '" | replier:"' + name + |
| 64 | '") forum:any'); |
avm99963 | 0616775 | 2020-09-08 00:50:36 +0200 | [diff] [blame] | 65 | |
| 66 | var container = document.createElement('div'); |
| 67 | container.classList.add('TWPT-previous-posts'); |
| 68 | |
| 69 | var badge = document.createElement('div'); |
| 70 | badge.classList.add('TWPT-badge'); |
| 71 | badge.setAttribute( |
| 72 | 'title', |
| 73 | chrome.i18n.getMessage( |
| 74 | 'inject_extension_badge_helper', |
| 75 | [chrome.i18n.getMessage('appName')])); |
| 76 | |
| 77 | var badgeI = document.createElement('i'); |
| 78 | badgeI.classList.add( |
| 79 | 'material-icon-i', 'material-icons-extended'); |
| 80 | badgeI.textContent = 'repeat'; |
| 81 | |
| 82 | badge.appendChild(badgeI); |
| 83 | container.appendChild(badge); |
| 84 | |
| 85 | var linkContainer = document.createElement('div'); |
| 86 | linkContainer.classList.add('TWPT-previous-posts--links'); |
| 87 | |
| 88 | addProfileHistoryLink(linkContainer, 'forum', query1); |
| 89 | addProfileHistoryLink(linkContainer, 'all', query2); |
| 90 | |
| 91 | container.appendChild(linkContainer); |
| 92 | |
| 93 | node.querySelector('.main-card-content').appendChild(container); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | } |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 97 | }); |
| 98 | } |
| 99 | }); |
| 100 | } |
| 101 | |
avm99963 | adf9086 | 2020-04-12 13:27:45 +0200 | [diff] [blame] | 102 | function intersectionCallback(entries, observer) { |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 103 | entries.forEach(entry => { |
| 104 | if (entry.isIntersecting) { |
| 105 | entry.target.click(); |
| 106 | } |
| 107 | }); |
| 108 | }; |
| 109 | |
| 110 | var observerOptions = { |
| 111 | childList: true, |
| 112 | attributes: true, |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 113 | subtree: true, |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 114 | } |
| 115 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 116 | var intersectionOptions = { |
| 117 | root: document.querySelector('.scrollable-content'), |
| 118 | rootMargin: '0px', |
| 119 | threshold: 1.0, |
| 120 | }; |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 121 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 122 | chrome.storage.sync.get(null, function(items) { |
| 123 | options = items; |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 124 | |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 125 | var startup = |
| 126 | JSON.parse(document.querySelector('html').getAttribute('data-startup')); |
| 127 | authuser = startup[2][1] || '0'; |
| 128 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 129 | mutationObserver = new MutationObserver(mutationCallback); |
| 130 | mutationObserver.observe( |
| 131 | document.querySelector('.scrollable-content'), observerOptions); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 132 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 133 | intersectionObserver = |
| 134 | new IntersectionObserver(intersectionCallback, intersectionOptions); |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 135 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 136 | if (options.fixedtoolbar) { |
| 137 | injectStyles( |
avm99963 | 0bc113a | 2020-09-07 13:02:11 +0200 | [diff] [blame] | 138 | '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] | 139 | } |
avm99963 | ae6a26d | 2020-04-12 14:03:51 +0200 | [diff] [blame] | 140 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 141 | if (options.increasecontrast) { |
avm99963 | 0bc113a | 2020-09-07 13:02:11 +0200 | [diff] [blame] | 142 | injectStyles( |
avm99963 | a2a0644 | 2020-11-25 21:11:10 +0100 | [diff] [blame] | 143 | '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}'); |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 144 | } |
avm99963 | 0f9503f | 2020-07-27 13:56:52 +0200 | [diff] [blame] | 145 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 146 | if (options.stickysidebarheaders) { |
| 147 | injectStyles( |
avm99963 | 0bc113a | 2020-09-07 13:02:11 +0200 | [diff] [blame] | 148 | 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}'); |
| 149 | } |
| 150 | |
| 151 | if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') { |
| 152 | injectStylesheet( |
| 153 | chrome.runtime.getURL('injections/ccdarktheme_switch.css')); |
| 154 | |
| 155 | var darkThemeSwitch = document.createElement('material-button'); |
| 156 | darkThemeSwitch.classList.add('TWPT-dark-theme'); |
| 157 | darkThemeSwitch.setAttribute('button', ''); |
| 158 | darkThemeSwitch.setAttribute( |
| 159 | 'title', chrome.i18n.getMessage('inject_ccdarktheme_helper')); |
| 160 | |
| 161 | darkThemeSwitch.addEventListener('click', e => { |
| 162 | chrome.storage.sync.get(null, currentOptions => { |
| 163 | currentOptions.ccdarktheme_switch_status = |
| 164 | !options.ccdarktheme_switch_status; |
| 165 | chrome.storage.sync.set(currentOptions, _ => { |
| 166 | location.reload(); |
| 167 | }); |
| 168 | }); |
| 169 | }); |
| 170 | |
| 171 | var switchContent = document.createElement('div'); |
| 172 | switchContent.classList.add('content'); |
| 173 | |
| 174 | var icon = document.createElement('material-icon'); |
| 175 | |
| 176 | var i = document.createElement('i'); |
| 177 | i.classList.add('material-icon-i', 'material-icons-extended'); |
| 178 | i.textContent = 'brightness_4'; |
| 179 | |
| 180 | icon.appendChild(i); |
| 181 | switchContent.appendChild(icon); |
| 182 | darkThemeSwitch.appendChild(switchContent); |
| 183 | |
avm99963 | 0616775 | 2020-09-08 00:50:36 +0200 | [diff] [blame] | 184 | var badgeContent = document.createElement('div'); |
| 185 | badgeContent.classList.add('TWPT-badge'); |
| 186 | badgeContent.setAttribute( |
| 187 | 'title', chrome.i18n.getMessage('inject_extension_badge_helper', [ |
| 188 | chrome.i18n.getMessage('appName') |
| 189 | ])); |
| 190 | |
| 191 | var badgeI = document.createElement('i'); |
| 192 | badgeI.classList.add('material-icon-i', 'material-icons-extended'); |
| 193 | badgeI.textContent = 'repeat'; |
| 194 | |
| 195 | badgeContent.appendChild(badgeI); |
| 196 | darkThemeSwitch.appendChild(badgeContent); |
| 197 | |
avm99963 | 0bc113a | 2020-09-07 13:02:11 +0200 | [diff] [blame] | 198 | var rightControl = document.querySelector('header .right-control'); |
| 199 | rightControl.style.width = |
| 200 | (parseInt(window.getComputedStyle(rightControl).width) + 58) + 'px'; |
| 201 | rightControl.insertAdjacentElement('afterbegin', darkThemeSwitch); |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 202 | } |
avm99963 | 129942f | 2020-09-08 02:07:18 +0200 | [diff] [blame] | 203 | |
| 204 | if (options.ccforcehidedrawer) { |
| 205 | var drawer = document.querySelector('material-drawer'); |
| 206 | if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) { |
| 207 | document.querySelector('.material-drawer-button').click(); |
| 208 | } |
| 209 | } |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 210 | }); |