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( |
| 56 | '(creator:"' + name + '" | replier:"' + name + '") forum:'+forumId); |
| 57 | var query2 = encodeURIComponent( |
| 58 | '(creator:"' + name + '" | replier:"' + name + '") forum:any'); |
| 59 | addProfileHistoryLink(node, 'forum', query1); |
| 60 | addProfileHistoryLink(node, 'all', query2); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | } |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 64 | }); |
| 65 | } |
| 66 | }); |
| 67 | } |
| 68 | |
avm99963 | adf9086 | 2020-04-12 13:27:45 +0200 | [diff] [blame] | 69 | function intersectionCallback(entries, observer) { |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 70 | entries.forEach(entry => { |
| 71 | if (entry.isIntersecting) { |
| 72 | entry.target.click(); |
| 73 | } |
| 74 | }); |
| 75 | }; |
| 76 | |
| 77 | var observerOptions = { |
| 78 | childList: true, |
| 79 | attributes: true, |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 80 | subtree: true, |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 81 | } |
| 82 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 83 | var intersectionOptions = { |
| 84 | root: document.querySelector('.scrollable-content'), |
| 85 | rootMargin: '0px', |
| 86 | threshold: 1.0, |
| 87 | }; |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 88 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 89 | chrome.storage.sync.get(null, function(items) { |
| 90 | options = items; |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 91 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 92 | mutationObserver = new MutationObserver(mutationCallback); |
| 93 | mutationObserver.observe( |
| 94 | document.querySelector('.scrollable-content'), observerOptions); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 95 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 96 | intersectionObserver = |
| 97 | new IntersectionObserver(intersectionCallback, intersectionOptions); |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 98 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 99 | if (options.fixedtoolbar) { |
| 100 | injectStyles( |
| 101 | 'ec-bulk-actions{position: sticky; top: 0; background: white; z-index: 96;}'); |
| 102 | } |
avm99963 | ae6a26d | 2020-04-12 14:03:51 +0200 | [diff] [blame] | 103 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 104 | if (options.increasecontrast) { |
| 105 | injectStyles('.thread-summary.read{background: #ecedee!important;}'); |
| 106 | } |
avm99963 | 0f9503f | 2020-07-27 13:56:52 +0200 | [diff] [blame] | 107 | |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 108 | if (options.stickysidebarheaders) { |
| 109 | injectStyles( |
| 110 | 'material-drawer .main-header{background: #fff; position: sticky; top: 0; z-index: 1;}'); |
| 111 | } |
| 112 | |
| 113 | if (options.profileindicator) { |
avm99963 | a560aba | 2020-08-31 13:32:29 +0200 | [diff] [blame] | 114 | injectScript( |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 115 | chrome.runtime.getURL('injections/profileindicator_inject.js')); |
| 116 | injectStylesheet( |
| 117 | chrome.runtime.getURL('injections/profileindicator_inject.css')); |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 118 | |
| 119 | // In order to pass i18n strings to the injected script, which doesn't have |
| 120 | // access to the chrome.i18n API. |
| 121 | window.addEventListener('geti18nString', evt => { |
| 122 | var request = evt.detail; |
| 123 | var response = { |
| 124 | string: chrome.i18n.getMessage(request.msg), |
| 125 | requestId: request.id |
| 126 | }; |
avm99963 | a560aba | 2020-08-31 13:32:29 +0200 | [diff] [blame] | 127 | window.dispatchEvent( |
| 128 | new CustomEvent('sendi18nString', {detail: response})); |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 129 | }); |
avm99963 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 130 | } |
| 131 | }); |