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 { |
| 12 | "forum": forum_a[1], |
| 13 | "thread": thread_a[1] |
| 14 | }; |
| 15 | } |
| 16 | |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 17 | function mutationCallback(mutationList, observer) { |
| 18 | mutationList.forEach((mutation) => { |
| 19 | if (mutation.type == "childList") { |
| 20 | mutation.addedNodes.forEach(function (node) { |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 21 | if (typeof node.classList !== "undefined") { |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 22 | if (options.thread && node.classList.contains("load-more-bar")) { |
| 23 | intersectionObserver.observe(node.querySelector(".load-more-button")); |
| 24 | } |
| 25 | |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 26 | if (options.threadall && node.classList.contains("load-more-bar")) { |
| 27 | intersectionObserver.observe(node.querySelector(".load-all-button")); |
| 28 | } |
| 29 | |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 30 | if (options.history && ("parentNode" in node) && node.parentNode !== null && ("tagName" in node.parentNode) && node.parentNode.tagName == "EC-USER") { |
| 31 | var nameElement = node.querySelector(".name span"); |
| 32 | if (nameElement !== null) { |
avm99963 | 18d043a | 2020-05-29 12:20:47 +0200 | [diff] [blame] | 33 | var name = nameElement.innerHTML; |
avm99963 | 51c9585 | 2019-06-17 21:37:41 +0200 | [diff] [blame] | 34 | var query = encodeURIComponent("(creator:\""+name+"\" | replier:\""+name+"\") -forum:0"); |
| 35 | var urlpart = encodeURIComponent("query="+query); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 36 | var link = document.createElement("a"); |
avm99963 | 51c9585 | 2019-06-17 21:37:41 +0200 | [diff] [blame] | 37 | link.setAttribute("href", "https://support.google.com/s/community/search/"+urlpart); |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 38 | link.innerText = chrome.i18n.getMessage("inject_previousposts"); |
avm99963 | 7beddb3 | 2020-01-18 19:52:46 +0100 | [diff] [blame] | 39 | node.querySelector(".main-card-content").appendChild(document.createElement("br")); |
| 40 | node.querySelector(".main-card-content").appendChild(link); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | } |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 44 | }); |
| 45 | } |
| 46 | }); |
| 47 | } |
| 48 | |
avm99963 | adf9086 | 2020-04-12 13:27:45 +0200 | [diff] [blame] | 49 | function intersectionCallback(entries, observer) { |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 50 | entries.forEach(entry => { |
| 51 | if (entry.isIntersecting) { |
| 52 | entry.target.click(); |
| 53 | } |
| 54 | }); |
| 55 | }; |
| 56 | |
avm99963 | ae6a26d | 2020-04-12 14:03:51 +0200 | [diff] [blame] | 57 | function injectStyles(css) { |
| 58 | var link = document.createElement('link'); |
| 59 | link.setAttribute("rel", "stylesheet"); |
| 60 | link.setAttribute("href", "data:text/css;charset=UTF-8,"+encodeURIComponent(css)); |
| 61 | document.head.appendChild(link); |
| 62 | } |
| 63 | |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 64 | var observerOptions = { |
| 65 | childList: true, |
| 66 | attributes: true, |
| 67 | subtree: true |
| 68 | } |
| 69 | |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 70 | var intersectionOptions = { |
| 71 | root: document.querySelector('.scrollable-content'), |
| 72 | rootMargin: '0px', |
| 73 | threshold: 1.0 |
| 74 | } |
| 75 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 76 | chrome.storage.sync.get(null, function(items) { |
| 77 | options = items; |
| 78 | |
| 79 | mutationObserver = new MutationObserver(mutationCallback); |
| 80 | mutationObserver.observe(document.querySelector(".scrollable-content"), observerOptions); |
| 81 | |
| 82 | intersectionObserver = new IntersectionObserver(intersectionCallback, intersectionOptions); |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 83 | |
| 84 | if (options.fixedtoolbar) { |
avm99963 | ae6a26d | 2020-04-12 14:03:51 +0200 | [diff] [blame] | 85 | injectStyles("ec-bulk-actions{position: sticky; top: 0; background: white; z-index: 96;}"); |
| 86 | } |
| 87 | |
| 88 | if (options.increasecontrast) { |
| 89 | injectStyles(".thread-summary.read{background: #ecedee!important;}"); |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 90 | } |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 91 | }); |