Skip to content

Commit

Permalink
fix: in-player adblocker inject timing issue
Browse files Browse the repository at this point in the history
- fix #1478
  • Loading branch information
JellyBrick committed Dec 10, 2023
1 parent d0ca10e commit abf2a52
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
22 changes: 9 additions & 13 deletions src/plugins/adblocker/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { contextBridge, webFrame } from 'electron';

import { blockers } from './types';
import { createPlugin } from '@/utils';
import {
Expand Down Expand Up @@ -107,31 +109,25 @@ export default createPlugin({
},
},
preload: {
script: 'window.JSON = window._proxyJson; window._proxyJson = undefined; window.Response = window._proxyResponse; window._proxyResponse = undefined; 0',
async start({ getConfig }) {
const config = await getConfig();

if (config.blocker === blockers.WithBlocklists) {
// Preload adblocker to inject scripts/styles
await injectCliqzPreload();
} else if (config.blocker === blockers.InPlayer && !isInjected()) {
inject(contextBridge);
await webFrame.executeJavaScript(this.script);
}
},
async onConfigChange(newConfig) {
if (newConfig.blocker === blockers.WithBlocklists) {
await injectCliqzPreload();
} else if (newConfig.blocker === blockers.InPlayer && !isInjected()) {
inject(contextBridge);
await webFrame.executeJavaScript(this.script);
}
},
},
renderer: {
async start({ getConfig }) {
const config = await getConfig();
if (config.blocker === blockers.InPlayer && !isInjected()) {
inject();
}
},
onConfigChange(newConfig) {
if (newConfig.blocker === blockers.InPlayer && !isInjected()) {
inject();
}
},
}
});
4 changes: 3 additions & 1 deletion src/plugins/adblocker/injectors/inject.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const inject: () => void;
import type { ContextBridge } from 'electron';

export const inject: (contextBridge: ContextBridge) => void;

export const isInjected: () => boolean;
34 changes: 29 additions & 5 deletions src/plugins/adblocker/injectors/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ let injected = false;

export const isInjected = () => injected;

export const inject = () => {
/**
* @param {Electron.ContextBridge} contextBridge
* @returns {*}
*/
export const inject = (contextBridge) => {
injected = true;
{
const pruner = function (o) {
Expand All @@ -28,17 +32,37 @@ export const inject = () => {
return o;
};

JSON.parse = new Proxy(JSON.parse, {
apply() {
return pruner(Reflect.apply(...arguments));
},
contextBridge.exposeInMainWorld('_proxyJson', {
parse: new Proxy(JSON.parse, {
apply() {
return pruner(Reflect.apply(...arguments));
},
}),
stringify: JSON.stringify,
[Symbol.toStringTag]: JSON[Symbol.toStringTag],
});

const withPrototype = (obj) => {
const protos = Object.getPrototypeOf(obj);
for (const [key, value] of Object.entries(protos)) {
if (Object.prototype.hasOwnProperty.call(obj, key)) continue;
if (typeof value === 'function') {
obj[key] = function (...args) {
return value.call(obj, ...args);
}
} else {
obj[key] = value;
}
}
return obj;
};

Response.prototype.json = new Proxy(Response.prototype.json, {
apply() {
return Reflect.apply(...arguments).then((o) => pruner(o));
},
});
contextBridge.exposeInMainWorld('_proxyResponse', withPrototype(Response));
}

(function () {
Expand Down

0 comments on commit abf2a52

Please sign in to comment.