Skip to content

Commit abf2a52

Browse files
committed
fix: in-player adblocker inject timing issue
- fix #1478
1 parent d0ca10e commit abf2a52

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

src/plugins/adblocker/index.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { contextBridge, webFrame } from 'electron';
2+
13
import { blockers } from './types';
24
import { createPlugin } from '@/utils';
35
import {
@@ -107,31 +109,25 @@ export default createPlugin({
107109
},
108110
},
109111
preload: {
112+
script: 'window.JSON = window._proxyJson; window._proxyJson = undefined; window.Response = window._proxyResponse; window._proxyResponse = undefined; 0',
110113
async start({ getConfig }) {
111114
const config = await getConfig();
112115

113116
if (config.blocker === blockers.WithBlocklists) {
114117
// Preload adblocker to inject scripts/styles
115118
await injectCliqzPreload();
119+
} else if (config.blocker === blockers.InPlayer && !isInjected()) {
120+
inject(contextBridge);
121+
await webFrame.executeJavaScript(this.script);
116122
}
117123
},
118124
async onConfigChange(newConfig) {
119125
if (newConfig.blocker === blockers.WithBlocklists) {
120126
await injectCliqzPreload();
127+
} else if (newConfig.blocker === blockers.InPlayer && !isInjected()) {
128+
inject(contextBridge);
129+
await webFrame.executeJavaScript(this.script);
121130
}
122131
},
123132
},
124-
renderer: {
125-
async start({ getConfig }) {
126-
const config = await getConfig();
127-
if (config.blocker === blockers.InPlayer && !isInjected()) {
128-
inject();
129-
}
130-
},
131-
onConfigChange(newConfig) {
132-
if (newConfig.blocker === blockers.InPlayer && !isInjected()) {
133-
inject();
134-
}
135-
},
136-
}
137133
});
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
export const inject: () => void;
1+
import type { ContextBridge } from 'electron';
2+
3+
export const inject: (contextBridge: ContextBridge) => void;
24

35
export const isInjected: () => boolean;

src/plugins/adblocker/injectors/inject.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ let injected = false;
1212

1313
export const isInjected = () => injected;
1414

15-
export const inject = () => {
15+
/**
16+
* @param {Electron.ContextBridge} contextBridge
17+
* @returns {*}
18+
*/
19+
export const inject = (contextBridge) => {
1620
injected = true;
1721
{
1822
const pruner = function (o) {
@@ -28,17 +32,37 @@ export const inject = () => {
2832
return o;
2933
};
3034

31-
JSON.parse = new Proxy(JSON.parse, {
32-
apply() {
33-
return pruner(Reflect.apply(...arguments));
34-
},
35+
contextBridge.exposeInMainWorld('_proxyJson', {
36+
parse: new Proxy(JSON.parse, {
37+
apply() {
38+
return pruner(Reflect.apply(...arguments));
39+
},
40+
}),
41+
stringify: JSON.stringify,
42+
[Symbol.toStringTag]: JSON[Symbol.toStringTag],
3543
});
3644

45+
const withPrototype = (obj) => {
46+
const protos = Object.getPrototypeOf(obj);
47+
for (const [key, value] of Object.entries(protos)) {
48+
if (Object.prototype.hasOwnProperty.call(obj, key)) continue;
49+
if (typeof value === 'function') {
50+
obj[key] = function (...args) {
51+
return value.call(obj, ...args);
52+
}
53+
} else {
54+
obj[key] = value;
55+
}
56+
}
57+
return obj;
58+
};
59+
3760
Response.prototype.json = new Proxy(Response.prototype.json, {
3861
apply() {
3962
return Reflect.apply(...arguments).then((o) => pruner(o));
4063
},
4164
});
65+
contextBridge.exposeInMainWorld('_proxyResponse', withPrototype(Response));
4266
}
4367

4468
(function () {

0 commit comments

Comments
 (0)