blob: 6d7a1e58b74c3dbf7e32223b625ac1ccbefaf4d2 [file] [log] [blame]
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +02001import {CCApi} from '../common/api.js';
Adrià Vilanova Martínez54964a52022-10-26 23:53:29 +02002import {parseUrl, recursiveParentElement} from '../common/commonUtils.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02003import {getAuthUser} from '../common/communityConsoleUtils.js';
avm99963f5923962020-12-07 16:44:37 +01004
avm99963f5923962020-12-07 16:44:37 +01005// Source:
6// https://stackoverflow.com/questions/33063774/communication-from-an-injected-script-to-the-content-script-with-a-response
7var 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
36function 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
44function 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
76function 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ínez3465e772021-07-11 19:18:41 +0200101 var authuser = getAuthUser();
avm999638d3b4ca2020-12-08 13:21:52 +0100102
avm99963f5923962020-12-07 16:44:37 +0100103 checkboxes.forEach(checkbox => {
avm99963d08c37e2022-09-25 20:41:58 +0200104 var url = recursiveParentElement(checkbox, 'EC-THREAD-SUMMARY')
105 .querySelector('a.header-content')
106 .href;
avm99963f5923962020-12-07 16:44:37 +0100107 var thread = parseUrl(url);
108 if (thread === false) {
109 console.error('Fatal error: thread URL ' + url + ' could not be parsed.');
110 return;
111 }
avm99963d08c37e2022-09-25 20:41:58 +0200112 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ínez462280f2021-08-07 22:59:02 +0200119 .then(() => {
avm99963f5923962020-12-07 16:44:37 +0100120 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ínez462280f2021-08-07 22:59:02 +0200127 .then(() => {
avm99963f5923962020-12-07 16:44:37 +0100128 progress.value = parseInt(progress.value) + 1;
129 if (progress.value == progress.getAttribute('max'))
130 enableEndButtons();
131 });
132 });
133}
134
135window.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ínez462280f2021-08-07 22:59:02 +0200141 console.debug('Performing action ' + e.data.action);
avm99963f5923962020-12-07 16:44:37 +0100142 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});