Adrià Vilanova Martínez | 462280f | 2021-08-07 22:59:02 +0200 | [diff] [blame] | 1 | import {CCApi} from '../common/api.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 2 | import {parseUrl} from '../common/commonUtils.js'; |
| 3 | import {getAuthUser} from '../common/communityConsoleUtils.js'; |
avm99963 | f592396 | 2020-12-07 16:44:37 +0100 | [diff] [blame] | 4 | |
| 5 | function recursiveParentElement(el, tag) { |
| 6 | while (el !== document.documentElement) { |
| 7 | el = el.parentNode; |
| 8 | if (el.tagName == tag) return el; |
| 9 | } |
| 10 | return undefined; |
| 11 | } |
| 12 | |
| 13 | // Source: |
| 14 | // https://stackoverflow.com/questions/33063774/communication-from-an-injected-script-to-the-content-script-with-a-response |
| 15 | var contentScriptRequest = (function() { |
| 16 | var requestId = 0; |
| 17 | var prefix = 'TWPT-batchlock-generic'; |
| 18 | |
| 19 | function sendRequest(data) { |
| 20 | var id = requestId++; |
| 21 | |
| 22 | return new Promise(function(resolve, reject) { |
| 23 | var listener = function(evt) { |
| 24 | if (evt.source === window && evt.data && evt.data.prefix === prefix && |
| 25 | evt.data.requestId == id) { |
| 26 | // Deregister self |
| 27 | window.removeEventListener('message', listener); |
| 28 | resolve(evt.data.data); |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | window.addEventListener('message', listener); |
| 33 | |
| 34 | var payload = {data, id, prefix}; |
| 35 | |
| 36 | window.dispatchEvent( |
| 37 | new CustomEvent('TWPT_sendRequest', {detail: payload})); |
| 38 | }); |
| 39 | } |
| 40 | |
| 41 | return {sendRequest: sendRequest}; |
| 42 | })(); |
| 43 | |
| 44 | function enableEndButtons() { |
| 45 | var buttons = document.querySelectorAll( |
| 46 | '.pane[pane-id="default-1"] footer[data-footer-id="1"] material-button'); |
| 47 | buttons.forEach(btn => { |
| 48 | btn.classList.remove('is-disabled'); |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | function addLogEntry(success, action, url, threadId, errDetails = null) { |
| 53 | var p1 = contentScriptRequest.sendRequest({ |
| 54 | action: 'geti18nMessage', |
| 55 | msg: 'inject_lockdialog_log_entry_beginning', |
| 56 | placeholders: [threadId], |
| 57 | }); |
| 58 | |
| 59 | var p2 = contentScriptRequest.sendRequest({ |
| 60 | action: 'geti18nMessage', |
| 61 | msg: 'inject_lockdialog_log_entry_' + (success ? 'success' : 'error') + |
| 62 | '_' + action, |
| 63 | placeholders: [errDetails], |
| 64 | }); |
| 65 | |
| 66 | Promise.all([p1, p2]).then(strings => { |
| 67 | var log = document.getElementById('TWPT-lock-log'); |
| 68 | var logEntry = document.createElement('p'); |
| 69 | logEntry.classList.add( |
| 70 | 'TWPT-log-entry', 'TWPT-log-entry--' + (success ? 'success' : 'error')); |
| 71 | |
| 72 | var a = document.createElement('a'); |
| 73 | a.href = url; |
| 74 | a.target = '_blank'; |
| 75 | a.textContent = strings[0]; |
| 76 | |
| 77 | var end = document.createTextNode(': ' + strings[1]); |
| 78 | |
| 79 | logEntry.append(a, end); |
| 80 | log.append(logEntry); |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | function lockThreads(action) { |
| 85 | var modal = document.querySelector('.pane[pane-id="default-1"]'); |
| 86 | modal.querySelector('footer[data-footer-id="0"]').classList.add('is-hidden'); |
| 87 | modal.querySelector('footer[data-footer-id="1"]') |
| 88 | .classList.remove('is-hidden'); |
| 89 | |
| 90 | var checkboxes = document.querySelectorAll( |
| 91 | '.thread-group material-checkbox[aria-checked="true"]'); |
| 92 | |
| 93 | var p = document.createElement('p'); |
| 94 | p.style.textAlign = 'center'; |
| 95 | |
| 96 | var progress = document.createElement('progress'); |
| 97 | progress.max = checkboxes.length; |
| 98 | progress.value = 0; |
| 99 | |
| 100 | p.append(progress); |
| 101 | |
| 102 | var log = document.createElement('div'); |
| 103 | log.id = 'TWPT-lock-log'; |
| 104 | log.classList.add('TWPT-log'); |
| 105 | |
| 106 | modal.querySelector('main').textContent = ''; |
| 107 | modal.querySelector('main').append(p, log); |
| 108 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 109 | var authuser = getAuthUser(); |
avm99963 | 8d3b4ca | 2020-12-08 13:21:52 +0100 | [diff] [blame] | 110 | |
avm99963 | f592396 | 2020-12-07 16:44:37 +0100 | [diff] [blame] | 111 | checkboxes.forEach(checkbox => { |
| 112 | var url = recursiveParentElement(checkbox, 'A').href; |
| 113 | var thread = parseUrl(url); |
| 114 | if (thread === false) { |
| 115 | console.error('Fatal error: thread URL ' + url + ' could not be parsed.'); |
| 116 | return; |
| 117 | } |
Adrià Vilanova Martínez | 462280f | 2021-08-07 22:59:02 +0200 | [diff] [blame] | 118 | CCApi('SetThreadAttribute', { |
| 119 | 1: thread.forum, |
| 120 | 2: thread.thread, |
| 121 | 3: (action == 'lock' ? 1 : 2), |
| 122 | }, /* authenticated = */ true, authuser) |
| 123 | .then(() => { |
avm99963 | f592396 | 2020-12-07 16:44:37 +0100 | [diff] [blame] | 124 | addLogEntry(true, action, url, thread.thread); |
| 125 | }) |
| 126 | .catch(err => { |
| 127 | console.error( |
| 128 | 'An error occurred while locking thread ' + url + ': ' + err); |
| 129 | addLogEntry(false, action, url, thread.thread, err); |
| 130 | }) |
Adrià Vilanova Martínez | 462280f | 2021-08-07 22:59:02 +0200 | [diff] [blame] | 131 | .then(() => { |
avm99963 | f592396 | 2020-12-07 16:44:37 +0100 | [diff] [blame] | 132 | progress.value = parseInt(progress.value) + 1; |
| 133 | if (progress.value == progress.getAttribute('max')) |
| 134 | enableEndButtons(); |
| 135 | }); |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | window.addEventListener('message', e => { |
| 140 | if (e.source === window && e.data && e.data.prefix === 'TWPT-batchlock' && |
| 141 | e.data.action) { |
| 142 | switch (e.data.action) { |
| 143 | case 'lock': |
| 144 | case 'unlock': |
Adrià Vilanova Martínez | 462280f | 2021-08-07 22:59:02 +0200 | [diff] [blame] | 145 | console.debug('Performing action ' + e.data.action); |
avm99963 | f592396 | 2020-12-07 16:44:37 +0100 | [diff] [blame] | 146 | lockThreads(e.data.action); |
| 147 | break; |
| 148 | |
| 149 | default: |
| 150 | console.error( |
| 151 | 'Action \'' + e.data.action + |
| 152 | '\' unknown to TWPT-batchlock receiver.'); |
| 153 | } |
| 154 | } |
| 155 | }); |