Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 1 | import MWOptionsWatcherClient from '../../common/mainWorldOptionsWatcher/Client.js'; |
Adrià Vilanova Martínez | 8a17fa8 | 2023-02-04 19:19:27 +0100 | [diff] [blame] | 2 | import {convertJSONToResponse, convertJSONToResponseText, getResponseJSON} from '../utils.js'; |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 3 | |
Adrià Vilanova Martínez | 4fb615f | 2023-02-04 18:55:16 +0100 | [diff] [blame] | 4 | import createMessageRemoveParentRef from './createMessageRemoveParentRef.js'; |
Adrià Vilanova Martínez | 8a17fa8 | 2023-02-04 19:19:27 +0100 | [diff] [blame] | 5 | import flattenThread from './flattenThread.js'; |
| 6 | import loadMoreThread from './loadMoreThread.js'; |
Adrià Vilanova Martínez | a38faee | 2024-05-16 19:52:57 +0200 | [diff] [blame^] | 7 | import removeUserAbuseEventsInViewForum from './removeUserAbuseEventsInViewForum.js'; |
| 8 | import removeUserAbuseEventsInViewThread from './removeUserAbuseEventsInViewThread.js'; |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 9 | |
| 10 | export const responseModifiers = [ |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 11 | loadMoreThread, |
| 12 | flattenThread, |
Adrià Vilanova Martínez | 4fb615f | 2023-02-04 18:55:16 +0100 | [diff] [blame] | 13 | createMessageRemoveParentRef, |
Adrià Vilanova Martínez | a38faee | 2024-05-16 19:52:57 +0200 | [diff] [blame^] | 14 | removeUserAbuseEventsInViewForum, |
| 15 | removeUserAbuseEventsInViewThread, |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 16 | ]; |
| 17 | |
| 18 | // Content script target |
| 19 | export const kCSTarget = 'TWPT-XHRInterceptorOptionsWatcher-CS'; |
| 20 | // Main world (AKA regular web page) target |
| 21 | export const kMWTarget = 'TWPT-XHRInterceptorOptionsWatcher-MW'; |
| 22 | |
| 23 | export default class ResponseModifier { |
| 24 | constructor() { |
| 25 | this.optionsWatcher = new MWOptionsWatcherClient( |
| 26 | Array.from(this.watchingFeatures()), kCSTarget, kMWTarget); |
| 27 | } |
| 28 | |
| 29 | watchingFeatures(modifiers) { |
| 30 | if (!modifiers) modifiers = responseModifiers; |
| 31 | |
| 32 | const union = new Set(); |
| 33 | for (const m of modifiers) { |
| 34 | if (!m.featureGated) continue; |
| 35 | for (const feature of m.features) union.add(feature); |
| 36 | } |
| 37 | return union; |
| 38 | } |
| 39 | |
| 40 | async #getMatchingModifiers(request) { |
| 41 | // First filter modifiers which match the request URL regex. |
| 42 | const urlModifiers = responseModifiers.filter( |
| 43 | modifier => request.$TWPTRequestURL.match(modifier.urlRegex)); |
| 44 | |
| 45 | // Now filter modifiers which require a certain feature to be enabled |
| 46 | // (feature-gated modifiers). |
| 47 | const featuresAreEnabled = await this.optionsWatcher.areEnabled( |
| 48 | Array.from(this.watchingFeatures(urlModifiers))); |
| 49 | |
| 50 | // #!if !production |
| 51 | if (Object.keys(featuresAreEnabled).length > 0) { |
Adrià Vilanova Martínez | 5b98793 | 2023-11-16 02:18:44 +0100 | [diff] [blame] | 52 | console.debug( |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 53 | '[XHR Interceptor - Response Modifier] Requested features', |
| 54 | featuresAreEnabled, 'for request', request.$TWPTRequestURL); |
| 55 | } |
| 56 | // #!endif |
| 57 | |
| 58 | return urlModifiers.filter(modifier => { |
| 59 | return !modifier.featureGated || modifier.isEnabled(featuresAreEnabled); |
| 60 | }); |
| 61 | } |
| 62 | |
Adrià Vilanova Martínez | b47ec06 | 2023-01-15 17:43:26 +0100 | [diff] [blame] | 63 | async intercept(request) { |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 64 | const matchingModifiers = await this.#getMatchingModifiers(request); |
| 65 | |
| 66 | // If we didn't find any matching modifiers, return the response right away. |
Adrià Vilanova Martínez | b47ec06 | 2023-01-15 17:43:26 +0100 | [diff] [blame] | 67 | if (matchingModifiers.length === 0) return request.xhr.response; |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 68 | |
| 69 | // Otherwise, apply the modifiers sequentially and set the new response. |
| 70 | let json = getResponseJSON({ |
| 71 | responseType: request.xhr.responseType, |
| 72 | response: request.xhr.response, |
| 73 | $TWPTRequestURL: request.$TWPTRequestURL, |
| 74 | $isArrayProto: request.$isArrayProto, |
| 75 | }); |
| 76 | for (const modifier of matchingModifiers) { |
| 77 | json = await modifier.interceptor(request, json); |
| 78 | } |
Adrià Vilanova Martínez | 8a17fa8 | 2023-02-04 19:19:27 +0100 | [diff] [blame] | 79 | request.$newResponse = convertJSONToResponse(request, json); |
| 80 | request.$newResponseText = convertJSONToResponseText(request, json); |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 81 | request.$responseModified = true; |
| 82 | } |
| 83 | } |