From 7656c41dbc6d049a6711f0110101bbaa44cb8e33 Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Sun, 4 Aug 2024 14:36:10 +0900 Subject: [PATCH] fix(adblocker/inplayer): fix Response.prototype.json fix #2310 --- src/plugins/adblocker/index.ts | 17 ++++++++++++++--- src/plugins/adblocker/injectors/inject.js | 14 ++------------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/plugins/adblocker/index.ts b/src/plugins/adblocker/index.ts index 9deb74c8..5b2fd3ab 100644 --- a/src/plugins/adblocker/index.ts +++ b/src/plugins/adblocker/index.ts @@ -10,11 +10,11 @@ import { import injectCliqzPreload from './injectors/inject-cliqz-preload'; import { inject, isInjected } from './injectors/inject'; +import { loadAdSpeedup } from './adSpeedup'; import { t } from '@/i18n'; import type { BrowserWindow } from 'electron'; -import { loadAdSpeedup } from './adSpeedup'; interface AdblockerConfig { /** @@ -74,7 +74,7 @@ export default createPlugin({ ]; }, renderer: { - async onPlayerApiReady(_, {getConfig}) { + async onPlayerApiReady(_, { getConfig }) { const config = await getConfig(); if (config.blocker === blockers.AdSpeedup) { await loadAdSpeedup(); @@ -118,7 +118,18 @@ export default createPlugin({ }, }, preload: { - script: 'window.JSON.parse = window._proxyJsonParse; window._proxyJsonParse = undefined; window.Response.prototype.json = window._proxyResponseJson; window._proxyResponseJson = undefined; 0', + script: `const _prunerFn = window._pruner; + window._pruner = undefined; + JSON.parse = new Proxy(JSON.parse, { + apply() { + return _prunerFn(Reflect.apply(...arguments)); + }, + }); + Response.prototype.json = new Proxy(Response.prototype.json, { + apply() { + return Reflect.apply(...arguments).then((o) => _prunerFn(o)); + }, + }); 0`, async start({ getConfig }) { const config = await getConfig(); diff --git a/src/plugins/adblocker/injectors/inject.js b/src/plugins/adblocker/injectors/inject.js index f5c8a420..6e6219fe 100644 --- a/src/plugins/adblocker/injectors/inject.js +++ b/src/plugins/adblocker/injectors/inject.js @@ -37,19 +37,9 @@ export const inject = (contextBridge) => { // return o; - }; + } - contextBridge.exposeInMainWorld('_proxyJsonParse', new Proxy(JSON.parse, { - apply() { - return pruner(Reflect.apply(...arguments)); - }, - })); - - contextBridge.exposeInMainWorld('_proxyResponseJson', new Proxy(Response.prototype.json, { - apply() { - return Reflect.apply(...arguments).then((o) => pruner(o)); - }, - })); + contextBridge.exposeInMainWorld('_pruner', pruner); } const chains = [