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