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 | 7beddb3 | 2020-01-18 19:52:46 +0100 | [diff] [blame] | 17 | function recursiveParentElement(el, tag) { |
| 18 | while (el !== document.documentElement) { |
| 19 | el = el.parentNode; |
| 20 | if (el.tagName == tag) return el; |
| 21 | } |
| 22 | |
| 23 | return undefined; |
| 24 | } |
| 25 | |
avm99963 | 3b9ff84 | 2019-06-04 04:01:31 +0200 | [diff] [blame] | 26 | function duplicateThreads() { |
| 27 | var modal = document.querySelector(".pane[pane-id=\"default-1\"]"); |
| 28 | var duplicateinto = parseUrl(modal.querySelector("#infinitegforums_duplicateinto").value); |
| 29 | if (duplicateinto === false) { |
| 30 | return; |
| 31 | } |
| 32 | modal.querySelector("footer").style.display = "none"; |
| 33 | var checkboxes = document.querySelectorAll(".thread-group material-checkbox[aria-checked=\"true\"]"); |
| 34 | modal.querySelector("main").innerHTML = '<p style="text-align: center;">'+chrome.i18n.getMessage("inject_duplicate_progress")+':<br><progress id="infinitegforums_progress" max="'+checkboxes.length+'" value="0"></progress></p>'; |
| 35 | checkboxes.forEach(checkbox => { |
avm99963 | 7beddb3 | 2020-01-18 19:52:46 +0100 | [diff] [blame] | 36 | var thread = parseUrl(recursiveParentElement(checkbox, "A").href); |
avm99963 | 3b9ff84 | 2019-06-04 04:01:31 +0200 | [diff] [blame] | 37 | var script = document.createElement('script'); |
| 38 | script.textContent = 'fetch("https://support.google.com/s/community/api/MarkDuplicateThread", {"credentials":"include","headers":{"content-type":"text/plain; charset=utf-8"},"body":\'{\"1\":\"'+thread.forum+'\",\"2\":\"'+thread.thread+'\",\"3\":{\"2\":{\"1\":\"'+duplicateinto.forum+'\",\"2\":\"'+duplicateinto.thread+'\"}}}\',"method":"POST","mode":"cors"}).then(_ => { var progress = document.querySelector("#infinitegforums_progress"); progress.value = parseInt(progress.value) + 1; if (progress.value == progress.getAttribute("max")) { location.reload(); } });'; |
| 39 | document.head.appendChild(script); |
| 40 | script.remove(); |
| 41 | }); |
| 42 | } |
| 43 | |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 44 | function mutationCallback(mutationList, observer) { |
| 45 | mutationList.forEach((mutation) => { |
| 46 | if (mutation.type == "childList") { |
| 47 | mutation.addedNodes.forEach(function (node) { |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 48 | if (typeof node.classList !== "undefined") { |
| 49 | if (options.list && node.classList.contains("view-more-button-container")) { |
| 50 | intersectionObserver.observe(node.querySelector(".view-more-button")); |
| 51 | } |
Nico Sinisterra | 4ae1c57 | 2019-03-27 12:57:13 -0300 | [diff] [blame] | 52 | |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 53 | if (options.thread && node.classList.contains("load-more-bar")) { |
| 54 | intersectionObserver.observe(node.querySelector(".load-more-button")); |
| 55 | } |
| 56 | |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 57 | if (options.threadall && node.classList.contains("load-more-bar")) { |
| 58 | intersectionObserver.observe(node.querySelector(".load-all-button")); |
| 59 | } |
| 60 | |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 61 | if (options.history && ("parentNode" in node) && node.parentNode !== null && ("tagName" in node.parentNode) && node.parentNode.tagName == "EC-USER") { |
| 62 | var nameElement = node.querySelector(".name span"); |
| 63 | if (nameElement !== null) { |
avm99963 | 51c9585 | 2019-06-17 21:37:41 +0200 | [diff] [blame] | 64 | var name = nameElement.innerText; |
| 65 | var query = encodeURIComponent("(creator:\""+name+"\" | replier:\""+name+"\") -forum:0"); |
| 66 | var urlpart = encodeURIComponent("query="+query); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 67 | var link = document.createElement("a"); |
avm99963 | 51c9585 | 2019-06-17 21:37:41 +0200 | [diff] [blame] | 68 | link.setAttribute("href", "https://support.google.com/s/community/search/"+urlpart); |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 69 | link.innerText = chrome.i18n.getMessage("inject_previousposts"); |
avm99963 | 7beddb3 | 2020-01-18 19:52:46 +0100 | [diff] [blame] | 70 | node.querySelector(".main-card-content").appendChild(document.createElement("br")); |
| 71 | node.querySelector(".main-card-content").appendChild(link); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 72 | } |
| 73 | } |
avm99963 | af7860e | 2019-06-04 03:33:26 +0200 | [diff] [blame] | 74 | |
| 75 | if (options.batchduplicate && ("tagName" in node) && node.tagName == "MATERIAL-BUTTON" && node.getAttribute("debugid") !== null && (node.getAttribute("debugid") == "mark-read-button" || node.getAttribute("debugid") == "mark-unread-button") && ("parentNode" in node) && document.querySelector("[debugid=\"batchduplicate\"]") === null && node.parentNode !== null && ("parentNode" in node.parentNode) && node.parentNode.parentNode !== null && ("tagName" in node.parentNode.parentNode) && node.parentNode.parentNode.tagName == "EC-BULK-ACTIONS") { |
| 76 | var clone = node.cloneNode(true); |
| 77 | clone.setAttribute("debugid", "batchduplicate"); |
| 78 | clone.setAttribute("title", chrome.i18n.getMessage("inject_duplicatebtn")); |
| 79 | clone.querySelector("material-icon").setAttribute("icon", "control_point_duplicate"); |
| 80 | clone.querySelector("i.material-icon-i").innerText = "control_point_duplicate"; |
| 81 | node.parentNode.prepend(clone); |
| 82 | clone.addEventListener("click", function() { |
| 83 | var modal = document.querySelector(".pane[pane-id=\"default-1\"]"); |
| 84 | modal.classList.add("visible"); |
| 85 | modal.style.display = "block"; |
| 86 | modal.innerHTML = '<material-dialog role="dialog" aria-modal="true" style="display: block!important; width: 600px; max-width: 100%; margin: 8px auto; padding: 16px 0; background: white; box-shadow: 0 24px 38px 3px rgba(0,0,0,.14), 0 9px 46px 8px rgba(0,0,0,.12), 0 11px 15px -7px rgba(0,0,0,.2);"><header role="presentation" style="padding: 24px 24px 0; width: 100%; box-sizing: border-box;"><div style="color: #202124; font-family: \'Google Sans\',sans-serif; font-size: 18px; font-weight: 400; line-height: 24px; margin-bottom: 4px; text-align: center;">'+chrome.i18n.getMessage("inject_duplicatebtn")+'</div></header><main role="presentation" style="font-size: 13px; font-weight: 400; color: rgba(0,0,0,.87); overflow: auto; padding: 0 24px;"><p><label for="duplicate_thread">'+chrome.i18n.getMessage("inject_duplicateinto")+' </label><input type="url" id="infinitegforums_duplicateinto" placeholder="'+chrome.i18n.getMessage("inject_duplicateinto_placeholder")+'" style="width: 400px;"></p></main><footer role="presentation" style="padding: 0 24px;"><material-button role="button" style="display: inline-block; float: right; color: #1a73e8; height: 36px; min-width: 64px; margin: 0 4px; cursor: pointer;" id="infinitegforums_duplicate"><div class="content" style="line-height: 36px; text-align: center;">'+chrome.i18n.getMessage("inject_duplicate_confirm")+'</div></material-button><material-button role="button" style="display: inline-block; float: right; color: #1a73e8; height: 36px; min-width: 64px; margin: 0 4px; cursor: pointer;" id="infinitegforums_cancel"><div class="content" style="line-height: 36px; text-align: center;">'+chrome.i18n.getMessage("inject_duplicate_cancel")+'</div></material-button><div style="clear: both;"></div></footer></material-dialog>'; |
avm99963 | 3b9ff84 | 2019-06-04 04:01:31 +0200 | [diff] [blame] | 87 | modal.querySelector("#infinitegforums_duplicateinto").focus(); |
avm99963 | af7860e | 2019-06-04 03:33:26 +0200 | [diff] [blame] | 88 | modal.querySelector("#infinitegforums_cancel").addEventListener("click", function() { |
| 89 | var modal = document.querySelector(".pane[pane-id=\"default-1\"]"); |
| 90 | modal.classList.remove("visible"); |
| 91 | modal.style.display = "none"; |
| 92 | modal.innerHTML = ""; |
| 93 | }); |
avm99963 | 3b9ff84 | 2019-06-04 04:01:31 +0200 | [diff] [blame] | 94 | modal.querySelector("#infinitegforums_duplicateinto").addEventListener("keypress", function() { |
| 95 | if (event.keyCode == 13) { |
| 96 | duplicateThreads(); |
avm99963 | af7860e | 2019-06-04 03:33:26 +0200 | [diff] [blame] | 97 | } |
avm99963 | af7860e | 2019-06-04 03:33:26 +0200 | [diff] [blame] | 98 | }); |
avm99963 | 3b9ff84 | 2019-06-04 04:01:31 +0200 | [diff] [blame] | 99 | modal.querySelector("#infinitegforums_duplicate").addEventListener("click", duplicateThreads); |
avm99963 | af7860e | 2019-06-04 03:33:26 +0200 | [diff] [blame] | 100 | }); |
| 101 | } |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 102 | } |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 103 | }); |
| 104 | } |
| 105 | }); |
| 106 | } |
| 107 | |
| 108 | function intersectionCallback(entries, observer) { |
| 109 | entries.forEach(entry => { |
| 110 | if (entry.isIntersecting) { |
| 111 | entry.target.click(); |
| 112 | } |
| 113 | }); |
| 114 | }; |
| 115 | |
| 116 | var observerOptions = { |
| 117 | childList: true, |
| 118 | attributes: true, |
| 119 | subtree: true |
| 120 | } |
| 121 | |
avm99963 | 847ee63 | 2019-03-27 00:57:44 +0100 | [diff] [blame] | 122 | var intersectionOptions = { |
| 123 | root: document.querySelector('.scrollable-content'), |
| 124 | rootMargin: '0px', |
| 125 | threshold: 1.0 |
| 126 | } |
| 127 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 128 | chrome.storage.sync.get(null, function(items) { |
| 129 | options = items; |
| 130 | |
| 131 | mutationObserver = new MutationObserver(mutationCallback); |
| 132 | mutationObserver.observe(document.querySelector(".scrollable-content"), observerOptions); |
| 133 | |
| 134 | intersectionObserver = new IntersectionObserver(intersectionCallback, intersectionOptions); |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 135 | |
| 136 | if (options.fixedtoolbar) { |
| 137 | var link = document.createElement('link'); |
| 138 | link.setAttribute("rel", "stylesheet"); |
avm99963 | 8a3a615 | 2019-03-31 21:12:02 +0200 | [diff] [blame] | 139 | link.setAttribute("href", "data:text/css;charset=UTF-8,ec-bulk-actions{position: sticky; top: 0; background: white; z-index: 96;}"); |
avm99963 | b21f625 | 2019-03-30 22:06:21 +0100 | [diff] [blame] | 140 | document.head.appendChild(link); |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 141 | } |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 142 | }); |