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