Skip to content

Commit 5b9e947

Browse files
committed
feat(adblocker): improve In-Player adblocker
1 parent 1f1efac commit 5b9e947

File tree

1 file changed

+19
-200
lines changed

1 file changed

+19
-200
lines changed

src/plugins/adblocker/injectors/inject.js

+19-200
Original file line numberDiff line numberDiff line change
@@ -52,208 +52,27 @@ export const inject = (contextBridge) => {
5252
}));
5353
}
5454

55-
(function () {
56-
let cValue = 'undefined';
57-
const chain = 'playerResponse.adPlacements';
58-
const thisScript = document.currentScript;
59-
//
60-
switch (cValue) {
61-
case 'null': {
62-
cValue = null;
63-
break;
64-
}
65-
66-
case "''": {
67-
cValue = '';
68-
break;
69-
}
70-
71-
case 'true': {
72-
cValue = true;
73-
break;
74-
}
75-
76-
case 'false': {
77-
cValue = false;
78-
break;
79-
}
80-
81-
case 'undefined': {
82-
cValue = undefined;
83-
break;
84-
}
85-
86-
case 'noopFunc': {
87-
cValue = function () {};
88-
89-
break;
90-
}
91-
92-
case 'trueFunc': {
93-
cValue = function () {
94-
return true;
95-
};
96-
97-
break;
98-
}
99-
100-
case 'falseFunc': {
101-
cValue = function () {
102-
return false;
103-
};
104-
105-
break;
106-
}
107-
108-
default: {
109-
if (/^\d+$/.test(cValue)) {
110-
cValue = Number.parseFloat(cValue);
111-
//
112-
if (isNaN(cValue)) {
113-
return;
114-
}
115-
116-
if (Math.abs(cValue) > 0x7f_ff) {
117-
return;
118-
}
119-
} else {
120-
return;
121-
}
122-
}
55+
const chains = [
56+
{
57+
chain: 'playerResponse.adPlacements',
58+
cValue: 'undefined',
59+
},
60+
{
61+
chain: 'ytInitialPlayerResponse.playerAds',
62+
cValue: 'undefined',
63+
},
64+
{
65+
chain: 'ytInitialPlayerResponse.adPlacements',
66+
cValue: 'undefined',
67+
},
68+
{
69+
chain: 'ytInitialPlayerResponse.adSlots',
70+
cValue: 'undefined',
12371
}
72+
];
12473

125-
//
126-
let aborted = false;
127-
const mustAbort = function (v) {
128-
if (aborted) {
129-
return true;
130-
}
131-
132-
aborted =
133-
v !== undefined &&
134-
v !== null &&
135-
cValue !== undefined &&
136-
cValue !== null &&
137-
typeof v !== typeof cValue;
138-
return aborted;
139-
};
140-
141-
/*
142-
Support multiple trappers for the same property:
143-
https://github.com/uBlockOrigin/uBlock-issues/issues/156
144-
*/
145-
146-
const trapProp = function (owner, prop, configurable, handler) {
147-
if (handler.init(owner[prop]) === false) {
148-
return;
149-
}
150-
151-
//
152-
const odesc = Object.getOwnPropertyDescriptor(owner, prop);
153-
let previousGetter;
154-
let previousSetter;
155-
if (odesc instanceof Object) {
156-
if (odesc.configurable === false) {
157-
return;
158-
}
159-
160-
if (odesc.get instanceof Function) {
161-
previousGetter = odesc.get;
162-
}
163-
164-
if (odesc.set instanceof Function) {
165-
previousSetter = odesc.set;
166-
}
167-
}
168-
169-
//
170-
Object.defineProperty(owner, prop, {
171-
configurable,
172-
get() {
173-
if (previousGetter !== undefined) {
174-
previousGetter();
175-
}
176-
177-
//
178-
return handler.getter();
179-
},
180-
set(a) {
181-
if (previousSetter !== undefined) {
182-
previousSetter(a);
183-
}
184-
185-
//
186-
handler.setter(a);
187-
},
188-
});
189-
};
190-
191-
const trapChain = function (owner, chain) {
192-
const pos = chain.indexOf('.');
193-
if (pos === -1) {
194-
trapProp(owner, chain, false, {
195-
v: undefined,
196-
getter() {
197-
return document.currentScript === thisScript ? this.v : cValue;
198-
},
199-
setter(a) {
200-
if (mustAbort(a) === false) {
201-
return;
202-
}
203-
204-
cValue = a;
205-
},
206-
init(v) {
207-
if (mustAbort(v)) {
208-
return false;
209-
}
210-
211-
//
212-
this.v = v;
213-
return true;
214-
},
215-
});
216-
//
217-
return;
218-
}
219-
220-
//
221-
const prop = chain.slice(0, pos);
222-
const v = owner[prop];
223-
//
224-
chain = chain.slice(pos + 1);
225-
if (v instanceof Object || (typeof v === 'object' && v !== null)) {
226-
trapChain(v, chain);
227-
return;
228-
}
229-
230-
//
231-
trapProp(owner, prop, true, {
232-
v: undefined,
233-
getter() {
234-
return this.v;
235-
},
236-
setter(a) {
237-
this.v = a;
238-
if (a instanceof Object) {
239-
trapChain(a, chain);
240-
}
241-
},
242-
init(v) {
243-
this.v = v;
244-
return true;
245-
},
246-
});
247-
};
248-
249-
//
250-
trapChain(window, chain);
251-
})();
252-
253-
(function () {
254-
let cValue = 'undefined';
74+
chains.forEach(function ({ chain, cValue }) {
25575
const thisScript = document.currentScript;
256-
const chain = 'ytInitialPlayerResponse.adPlacements';
25776
//
25877
switch (cValue) {
25978
case 'null': {
@@ -446,5 +265,5 @@ export const inject = (contextBridge) => {
446265

447266
//
448267
trapChain(window, chain);
449-
})();
268+
});
450269
};

0 commit comments

Comments
 (0)