avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 1 | var intersectionObserver; |
| 2 | |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame^] | 3 | function injectStylesheet(stylesheetName) { |
| 4 | var link = document.createElement('link'); |
| 5 | link.setAttribute('rel', 'stylesheet'); |
| 6 | link.setAttribute('href', stylesheetName); |
| 7 | document.head.appendChild(link); |
| 8 | } |
| 9 | |
| 10 | function injectStyles(css) { |
| 11 | injectStylesheet('data:text/css;charset=UTF-8,' + encodeURIComponent(css)); |
| 12 | } |
| 13 | |
| 14 | function injectScript(scriptName) { |
| 15 | var script = document.createElement('script'); |
| 16 | script.src = scriptName; |
| 17 | document.head.appendChild(script); |
| 18 | } |
| 19 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 20 | function intersectionCallback(entries, observer) { |
| 21 | entries.forEach(entry => { |
| 22 | if (entry.isIntersecting) { |
| 23 | entry.target.click(); |
| 24 | } |
| 25 | }); |
| 26 | }; |
| 27 | |
| 28 | var intersectionOptions = { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 29 | threshold: 1.0, |
| 30 | }; |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 31 | |
| 32 | chrome.storage.sync.get(null, function(items) { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 33 | var path = document.location.pathname.split('/'); |
| 34 | if (path[path.length - 1] == 'new' || |
| 35 | (path.length > 1 && path[path.length - 1] == '' && |
| 36 | path[path.length - 2] == 'new')) { |
avm99963 | 7db655f | 2020-08-18 11:44:31 +0200 | [diff] [blame] | 37 | return; |
| 38 | } |
| 39 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 40 | var redirectLink = document.querySelector('.community-console'); |
avm99963 | 134f1ef | 2019-03-30 19:05:27 +0100 | [diff] [blame] | 41 | if (items.redirect && redirectLink !== null) { |
| 42 | window.location = redirectLink.href; |
| 43 | } else { |
avm99963 | 7db655f | 2020-08-18 11:44:31 +0200 | [diff] [blame] | 44 | var button = |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 45 | document.querySelector('.thread-all-replies__load-more-button'); |
avm99963 | 134f1ef | 2019-03-30 19:05:27 +0100 | [diff] [blame] | 46 | if (items.thread && button !== null) { |
avm99963 | 7db655f | 2020-08-18 11:44:31 +0200 | [diff] [blame] | 47 | intersectionObserver = |
| 48 | new IntersectionObserver(intersectionCallback, intersectionOptions); |
avm99963 | 134f1ef | 2019-03-30 19:05:27 +0100 | [diff] [blame] | 49 | intersectionObserver.observe(button); |
| 50 | } |
avm99963 | 7db655f | 2020-08-18 11:44:31 +0200 | [diff] [blame] | 51 | var allbutton = |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 52 | document.querySelector('.thread-all-replies__load-all-button'); |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 53 | if (items.threadall && button !== null) { |
avm99963 | 7db655f | 2020-08-18 11:44:31 +0200 | [diff] [blame] | 54 | intersectionObserver = |
| 55 | new IntersectionObserver(intersectionCallback, intersectionOptions); |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 56 | intersectionObserver.observe(allbutton); |
| 57 | } |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame^] | 58 | |
| 59 | if (items.profileindicator) { |
| 60 | injectScript( |
| 61 | chrome.runtime.getURL('injections/profileindicator_inject.js')); |
| 62 | injectStylesheet( |
| 63 | chrome.runtime.getURL('injections/profileindicator_inject.css')); |
| 64 | |
| 65 | // In order to pass i18n strings to the injected script, which doesn't |
| 66 | // have access to the chrome.i18n API. |
| 67 | window.addEventListener('geti18nString', evt => { |
| 68 | var request = evt.detail; |
| 69 | var response = { |
| 70 | string: chrome.i18n.getMessage(request.msg), |
| 71 | requestId: request.id |
| 72 | }; |
| 73 | window.dispatchEvent( |
| 74 | new CustomEvent('sendi18nString', {detail: response})); |
| 75 | }); |
| 76 | } |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 77 | } |
| 78 | }); |