Skip to content

Commit 3fc281a

Browse files
committed
Improve google-ima shim
Related issue: uBlockOrigin/uAssets#31018
1 parent 5b95c37 commit 3fc281a

File tree

1 file changed

+16
-217
lines changed

1 file changed

+16
-217
lines changed

src/web_accessible_resources/google-ima.js

Lines changed: 16 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
* - Corrected typo in `UniversalAdIdInfo.getAdIdValue()` method name
1616
* - Corrected dispatch of LOAD event when preloading is enabled
1717
* - Corrected dispatch of CONTENT_PAUSE/RESUME_REQUESTED events
18+
* - Remove test for auto-play in requestAds(): always behave as if auto-play
19+
* is disabled
1820
*
1921
* Related issue:
2022
* - https://github.com/uBlockOrigin/uBlock-issues/issues/2158
2123
* - https://github.com/uBlockOrigin/uAssets/issues/30134
24+
* - https://github.com/uBlockOrigin/uAssets/issues/31018
2225
*
2326
**/
2427

@@ -39,204 +42,6 @@
3942

4043
if (!window.google || !window.google.ima || !window.google.ima.VERSION) {
4144
const VERSION = "3.517.2";
42-
43-
const CheckCanAutoplay = (function() {
44-
// Sourced from: https://searchfox.org/mozilla-central/source/dom/media/gtest/negative_duration.mp4
45-
const TEST_VIDEO = new Blob(
46-
[
47-
new Uint32Array([
48-
469762048,
49-
1887007846,
50-
1752392036,
51-
0,
52-
913273705,
53-
1717987696,
54-
828601953,
55-
-1878917120,
56-
1987014509,
57-
1811939328,
58-
1684567661,
59-
0,
60-
0,
61-
0,
62-
-402456576,
63-
0,
64-
256,
65-
1,
66-
0,
67-
0,
68-
256,
69-
0,
70-
0,
71-
0,
72-
256,
73-
0,
74-
0,
75-
0,
76-
64,
77-
0,
78-
0,
79-
0,
80-
0,
81-
0,
82-
0,
83-
33554432,
84-
-201261056,
85-
1801548404,
86-
1744830464,
87-
1684564852,
88-
251658241,
89-
0,
90-
0,
91-
0,
92-
0,
93-
16777216,
94-
0,
95-
-1,
96-
-1,
97-
0,
98-
0,
99-
0,
100-
0,
101-
256,
102-
0,
103-
0,
104-
0,
105-
256,
106-
0,
107-
0,
108-
0,
109-
64,
110-
5,
111-
53250,
112-
-2080309248,
113-
1634296941,
114-
738197504,
115-
1684563053,
116-
1,
117-
0,
118-
0,
119-
0,
120-
0,
121-
-2137614336,
122-
-1,
123-
-1,
124-
50261,
125-
754974720,
126-
1919706216,
127-
0,
128-
0,
129-
1701079414,
130-
0,
131-
0,
132-
0,
133-
1701079382,
134-
1851869295,
135-
1919249508,
136-
16777216,
137-
1852402979,
138-
102,
139-
1752004116,
140-
100,
141-
1,
142-
0,
143-
0,
144-
1852400676,
145-
102,
146-
1701995548,
147-
102,
148-
0,
149-
1,
150-
1819440396,
151-
32,
152-
1,
153-
1651799011,
154-
108,
155-
1937011607,
156-
100,
157-
0,
158-
1,
159-
1668702599,
160-
49,
161-
0,
162-
1,
163-
0,
164-
0,
165-
0,
166-
33555712,
167-
4718800,
168-
4718592,
169-
0,
170-
65536,
171-
0,
172-
0,
173-
0,
174-
0,
175-
0,
176-
0,
177-
0,
178-
0,
179-
16776984,
180-
1630601216,
181-
21193590,
182-
-14745500,
183-
1729626337,
184-
-1407254428,
185-
89161945,
186-
1049019,
187-
9453056,
188-
-251611125,
189-
27269507,
190-
-379058688,
191-
-1329024392,
192-
268435456,
193-
1937011827,
194-
0,
195-
0,
196-
268435456,
197-
1668510835,
198-
0,
199-
0,
200-
335544320,
201-
2054386803,
202-
0,
203-
0,
204-
0,
205-
268435456,
206-
1868788851,
207-
0,
208-
0,
209-
671088640,
210-
2019915373,
211-
536870912,
212-
2019914356,
213-
0,
214-
16777216,
215-
16777216,
216-
0,
217-
0,
218-
0,
219-
]),
220-
],
221-
{ type: "video/mp4" }
222-
);
223-
224-
let testVideo;
225-
226-
return function() {
227-
if (!testVideo) {
228-
testVideo = document.createElement("video");
229-
testVideo.style =
230-
"position:absolute; width:0; height:0; left:0; right:0; z-index:-1; border:0";
231-
testVideo.setAttribute("muted", "muted");
232-
testVideo.setAttribute("playsinline", "playsinline");
233-
testVideo.src = URL.createObjectURL(TEST_VIDEO);
234-
document.body.appendChild(testVideo);
235-
}
236-
return testVideo.play();
237-
};
238-
})();
239-
24045
const ima = {};
24146

24247
class AdDisplayContainer {
@@ -390,26 +195,20 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) {
390195
return VERSION;
391196
}
392197
requestAds(_r, _c) {
393-
// If autoplay is disabled and the page is trying to autoplay a tracking
394-
// ad, then IMA fails with an error, and the page is expected to request
395-
// ads again later when the user clicks to play.
396-
CheckCanAutoplay().then(
397-
() => {
398-
const { ADS_MANAGER_LOADED } = AdsManagerLoadedEvent.Type;
399-
this._dispatch(new ima.AdsManagerLoadedEvent(ADS_MANAGER_LOADED, _r, _c));
400-
},
401-
() => {
402-
const e = new ima.AdError(
403-
"adPlayError",
404-
1205,
405-
1205,
406-
"The browser prevented playback initiated without user interaction.",
407-
_r,
408-
_c
409-
);
410-
this._dispatch(new ima.AdErrorEvent(e));
411-
}
198+
requestAnimationFrame(() => {
199+
const { ADS_MANAGER_LOADED } = AdsManagerLoadedEvent.Type;
200+
const event = new ima.AdsManagerLoadedEvent(ADS_MANAGER_LOADED, _r, _c);
201+
this._dispatch(event);
202+
});
203+
const error = new ima.AdError(
204+
"adPlayError",
205+
1205, 1205,
206+
"The browser prevented playback initiated without user interaction.",
207+
_r, _c
412208
);
209+
requestAnimationFrame( () => {
210+
this._dispatch(new ima.AdErrorEvent(error));
211+
});
413212
}
414213
}
415214

0 commit comments

Comments
 (0)