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 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 17 | function mutationCallback(mutationList, observer) { |
| 18 | mutationList.forEach((mutation) => { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 19 | if (mutation.type == 'childList') { |
| 20 | mutation.addedNodes.forEach(function(node) { |
| 21 | if (typeof node.classList !== 'undefined') { |
| 22 | if (options.thread && node.classList.contains('load-more-bar')) { |
| 23 | intersectionObserver.observe( |
| 24 | node.querySelector('.load-more-button')); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 25 | } |
| 26 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 27 | if (options.threadall && node.classList.contains('load-more-bar')) { |
| 28 | intersectionObserver.observe( |
| 29 | node.querySelector('.load-all-button')); |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 30 | } |
| 31 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 32 | if (options.history && ('parentNode' in node) && |
| 33 | node.parentNode !== null && ('tagName' in node.parentNode) && |
| 34 | node.parentNode.tagName == 'EC-USER') { |
| 35 | var nameElement = node.querySelector('.name span'); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 36 | if (nameElement !== null) { |
avm99963 | 18d043a | 2020-05-29 12:20:47 +0200 | [diff] [blame] | 37 | var name = nameElement.innerHTML; |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 38 | var query = encodeURIComponent( |
| 39 | '(creator:"' + name + '" | replier:"' + name + '") -forum:0'); |
| 40 | var urlpart = encodeURIComponent('query=' + query); |
| 41 | var link = document.createElement('a'); |
| 42 | link.setAttribute( |
| 43 | 'href', |
| 44 | 'https://support.google.com/s/community/search/' + urlpart); |
| 45 | link.innerText = chrome.i18n.getMessage('inject_previousposts'); |
| 46 | node.querySelector('.main-card-content') |
| 47 | .appendChild(document.createElement('br')); |
| 48 | node.querySelector('.main-card-content').appendChild(link); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | } |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 52 | }); |
| 53 | } |
| 54 | }); |
| 55 | } |
| 56 | |
avm99963 | adf9086 | 2020-04-12 13:27:45 +0200 | [diff] [blame] | 57 | function intersectionCallback(entries, observer) { |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 58 | entries.forEach(entry => { |
| 59 | if (entry.isIntersecting) { |
| 60 | entry.target.click(); |
| 61 | } |
| 62 | }); |
| 63 | }; |
| 64 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 65 | function injectStylesheet(stylesheetName) { |
avm99963 | ae6a26d | 2020-04-12 14:03:51 +0200 | [diff] [blame] | 66 | var link = document.createElement('link'); |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 67 | link.setAttribute('rel', 'stylesheet'); |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 68 | link.setAttribute('href', stylesheetName); |
avm99963 | ae6a26d | 2020-04-12 14:03:51 +0200 | [diff] [blame] | 69 | document.head.appendChild(link); |
| 70 | } |
| 71 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 72 | function injectStyles(css) { |
| 73 | injectStylesheet('data:text/css;charset=UTF-8,' + encodeURIComponent(css)); |
| 74 | } |
| 75 | |
| 76 | function injectScript(scriptName) { |
| 77 | var script = document.createElement('script'); |
| 78 | script.src = scriptName; |
| 79 | document.head.appendChild(script); |
| 80 | } |
| 81 | |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 82 | var observerOptions = { |
| 83 | childList: true, |
| 84 | attributes: true, |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 85 | subtree: true, |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 86 | } |
| 87 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 88 | var intersectionOptions = { |
| 89 | root: document.querySelector('.scrollable-content'), |
| 90 | rootMargin: '0px', |
| 91 | threshold: 1.0, |
| 92 | }; |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 93 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 94 | chrome.storage.sync.get(null, function(items) { |
| 95 | options = items; |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 96 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 97 | mutationObserver = new MutationObserver(mutationCallback); |
| 98 | mutationObserver.observe( |
| 99 | document.querySelector('.scrollable-content'), observerOptions); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 100 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 101 | intersectionObserver = |
| 102 | new IntersectionObserver(intersectionCallback, intersectionOptions); |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 103 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 104 | if (options.fixedtoolbar) { |
| 105 | injectStyles( |
| 106 | 'ec-bulk-actions{position: sticky; top: 0; background: white; z-index: 96;}'); |
| 107 | } |
avm99963 | ae6a26d | 2020-04-12 14:03:51 +0200 | [diff] [blame] | 108 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 109 | if (options.increasecontrast) { |
| 110 | injectStyles('.thread-summary.read{background: #ecedee!important;}'); |
| 111 | } |
avm99963 | 0f9503f | 2020-07-27 13:56:52 +0200 | [diff] [blame] | 112 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 113 | if (options.stickysidebarheaders) { |
| 114 | injectStyles( |
| 115 | 'material-drawer .main-header{background: #fff; position: sticky; top: 0; z-index: 1;}'); |
| 116 | } |
| 117 | |
| 118 | if (options.profileindicator) { |
avm99963 | a560aba | 2020-08-31 13:32:29 +0200 | [diff] [blame] | 119 | injectScript( |
| 120 | chrome.runtime.getURL('injections/console_profileindicator_inject.js')); |
| 121 | injectStylesheet(chrome.runtime.getURL( |
| 122 | 'injections/console_profileindicator_inject.css')); |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 123 | |
| 124 | // In order to pass i18n strings to the injected script, which doesn't have |
| 125 | // access to the chrome.i18n API. |
| 126 | window.addEventListener('geti18nString', evt => { |
| 127 | var request = evt.detail; |
| 128 | var response = { |
| 129 | string: chrome.i18n.getMessage(request.msg), |
| 130 | requestId: request.id |
| 131 | }; |
avm99963 | a560aba | 2020-08-31 13:32:29 +0200 | [diff] [blame] | 132 | window.dispatchEvent( |
| 133 | new CustomEvent('sendi18nString', {detail: response})); |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 134 | }); |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 135 | } |
| 136 | }); |