avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 1 | var mutationObserver, intersectionObserver, options; |
| 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); |
| 19 | var container = document.createElement('div'); |
| 20 | container.style.margin = '3px 0'; |
| 21 | |
| 22 | var link = document.createElement('a'); |
| 23 | link.setAttribute( |
| 24 | 'href', 'https://support.google.com/s/community/search/' + urlpart); |
| 25 | link.innerText = chrome.i18n.getMessage('inject_previousposts_' + type); |
| 26 | |
| 27 | container.appendChild(link); |
| 28 | node.querySelector('.main-card-content').appendChild(container); |
| 29 | } |
| 30 | |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 31 | function mutationCallback(mutationList, observer) { |
| 32 | mutationList.forEach((mutation) => { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 33 | if (mutation.type == 'childList') { |
| 34 | mutation.addedNodes.forEach(function(node) { |
| 35 | if (typeof node.classList !== 'undefined') { |
| 36 | if (options.thread && node.classList.contains('load-more-bar')) { |
| 37 | intersectionObserver.observe( |
| 38 | node.querySelector('.load-more-button')); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 39 | } |
| 40 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 41 | if (options.threadall && node.classList.contains('load-more-bar')) { |
| 42 | intersectionObserver.observe( |
| 43 | node.querySelector('.load-all-button')); |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 44 | } |
| 45 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 46 | if (options.history && ('parentNode' in node) && |
| 47 | node.parentNode !== null && ('tagName' in node.parentNode) && |
| 48 | node.parentNode.tagName == 'EC-USER') { |
| 49 | var nameElement = node.querySelector('.name span'); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 50 | if (nameElement !== null) { |
avm99963 | 943b849 | 2020-08-31 23:40:43 +0200 | [diff] [blame] | 51 | var forumId = |
| 52 | location.href.split('/forum/')[1].split('/')[0] || '0'; |
| 53 | |
| 54 | var name = escapeUsername(nameElement.innerHTML); |
| 55 | var query1 = encodeURIComponent( |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 56 | '(creator:"' + name + '" | replier:"' + name + |
| 57 | '") forum:' + forumId); |
avm99963 | 943b849 | 2020-08-31 23:40:43 +0200 | [diff] [blame] | 58 | var query2 = encodeURIComponent( |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 59 | '(creator:"' + name + '" | replier:"' + name + |
| 60 | '") forum:any'); |
avm99963 | 943b849 | 2020-08-31 23:40:43 +0200 | [diff] [blame] | 61 | addProfileHistoryLink(node, 'forum', query1); |
| 62 | addProfileHistoryLink(node, 'all', query2); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | } |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 66 | }); |
| 67 | } |
| 68 | }); |
| 69 | } |
| 70 | |
avm99963 | adf9086 | 2020-04-12 13:27:45 +0200 | [diff] [blame] | 71 | function intersectionCallback(entries, observer) { |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 72 | entries.forEach(entry => { |
| 73 | if (entry.isIntersecting) { |
| 74 | entry.target.click(); |
| 75 | } |
| 76 | }); |
| 77 | }; |
| 78 | |
| 79 | var observerOptions = { |
| 80 | childList: true, |
| 81 | attributes: true, |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 82 | subtree: true, |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 83 | } |
| 84 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 85 | var intersectionOptions = { |
| 86 | root: document.querySelector('.scrollable-content'), |
| 87 | rootMargin: '0px', |
| 88 | threshold: 1.0, |
| 89 | }; |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 90 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 91 | chrome.storage.sync.get(null, function(items) { |
| 92 | options = items; |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 93 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 94 | mutationObserver = new MutationObserver(mutationCallback); |
| 95 | mutationObserver.observe( |
| 96 | document.querySelector('.scrollable-content'), observerOptions); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 97 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 98 | intersectionObserver = |
| 99 | new IntersectionObserver(intersectionCallback, intersectionOptions); |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 100 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 101 | if (options.fixedtoolbar) { |
| 102 | injectStyles( |
avm99963 | 0bc113a | 2020-09-07 13:02:11 +0200 | [diff] [blame] | 103 | '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] | 104 | } |
avm99963 | ae6a26d | 2020-04-12 14:03:51 +0200 | [diff] [blame] | 105 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 106 | if (options.increasecontrast) { |
avm99963 | 0bc113a | 2020-09-07 13:02:11 +0200 | [diff] [blame] | 107 | injectStyles( |
| 108 | '.thread-summary.read{background: var(--TWPT-thread-read-background, #ecedee)!important;}'); |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 109 | } |
avm99963 | 0f9503f | 2020-07-27 13:56:52 +0200 | [diff] [blame] | 110 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 111 | if (options.stickysidebarheaders) { |
| 112 | injectStyles( |
avm99963 | 0bc113a | 2020-09-07 13:02:11 +0200 | [diff] [blame] | 113 | 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}'); |
| 114 | } |
| 115 | |
| 116 | if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') { |
| 117 | injectStylesheet( |
| 118 | chrome.runtime.getURL('injections/ccdarktheme_switch.css')); |
| 119 | |
| 120 | var darkThemeSwitch = document.createElement('material-button'); |
| 121 | darkThemeSwitch.classList.add('TWPT-dark-theme'); |
| 122 | darkThemeSwitch.setAttribute('button', ''); |
| 123 | darkThemeSwitch.setAttribute( |
| 124 | 'title', chrome.i18n.getMessage('inject_ccdarktheme_helper')); |
| 125 | |
| 126 | darkThemeSwitch.addEventListener('click', e => { |
| 127 | chrome.storage.sync.get(null, currentOptions => { |
| 128 | currentOptions.ccdarktheme_switch_status = |
| 129 | !options.ccdarktheme_switch_status; |
| 130 | chrome.storage.sync.set(currentOptions, _ => { |
| 131 | location.reload(); |
| 132 | }); |
| 133 | }); |
| 134 | }); |
| 135 | |
| 136 | var switchContent = document.createElement('div'); |
| 137 | switchContent.classList.add('content'); |
| 138 | |
| 139 | var icon = document.createElement('material-icon'); |
| 140 | |
| 141 | var i = document.createElement('i'); |
| 142 | i.classList.add('material-icon-i', 'material-icons-extended'); |
| 143 | i.textContent = 'brightness_4'; |
| 144 | |
| 145 | icon.appendChild(i); |
| 146 | switchContent.appendChild(icon); |
| 147 | darkThemeSwitch.appendChild(switchContent); |
| 148 | |
| 149 | var rightControl = document.querySelector('header .right-control'); |
| 150 | rightControl.style.width = |
| 151 | (parseInt(window.getComputedStyle(rightControl).width) + 58) + 'px'; |
| 152 | rightControl.insertAdjacentElement('afterbegin', darkThemeSwitch); |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 153 | } |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 154 | }); |