Patch google-ima shim script for proper integration into uBO

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/2158

Additionally, added firing of CONTENT_RESUME_REQUESTED event in
start() method.
This commit is contained in:
Raymond Hill 2022-09-11 11:03:47 -04:00
parent b6b6e49d57
commit 067e128163
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 97 additions and 63 deletions

View File

@ -36,6 +36,7 @@
<div class="li"><span><a href="https://github.com/Swatinem/diff" target="_blank">An implementation of Myers' diff algorithm</a> by <a href="https://github.com/Swatinem">Arpad Borsos</a></span></div> <div class="li"><span><a href="https://github.com/Swatinem/diff" target="_blank">An implementation of Myers' diff algorithm</a> by <a href="https://github.com/Swatinem">Arpad Borsos</a></span></div>
<div class="li"><span><a href="https://github.com/foo123/RegexAnalyzer" target="_blank">Regular Expression Analyzer</a> by <a href="https://github.com/foo123">Nikos M.</a></span></div> <div class="li"><span><a href="https://github.com/foo123/RegexAnalyzer" target="_blank">Regular Expression Analyzer</a> by <a href="https://github.com/foo123">Nikos M.</a></span></div>
<div class="li"><span><a href="https://github.com/hsluv/hsluv" target="_blank">HSLuv - Human-friendly HSL</a> by <a href="https://github.com/boronine">Alexei Boronine</a></span></div> <div class="li"><span><a href="https://github.com/hsluv/hsluv" target="_blank">HSLuv - Human-friendly HSL</a> by <a href="https://github.com/boronine">Alexei Boronine</a></span></div>
<div class="li"><span><a href="https://searchfox.org/mozilla-central/rev/d317e93d9a59c9e4c06ada85fbff9f6a1ceaaad1/browser/extensions/webcompat/shims/google-ima.js" target="_blank">google-ima.js</a> by <a href="https://www.mozilla.org/">Mozilla</a></span></div>
</div> </div>
<div class="li"><span data-i18n="aboutCDNs"></span></div> <div class="li"><span data-i18n="aboutCDNs"></span></div>
<div class="liul"> <div class="liul">

View File

@ -104,6 +104,8 @@ const redirectableResources = new Map([
[ 'google-analytics_inpage_linkid.js', { [ 'google-analytics_inpage_linkid.js', {
alias: 'google-analytics.com/inpage_linkid.js', alias: 'google-analytics.com/inpage_linkid.js',
} ], } ],
[ 'google-ima.js', {
} ],
[ 'googlesyndication_adsbygoogle.js', { [ 'googlesyndication_adsbygoogle.js', {
alias: 'googlesyndication.com/adsbygoogle.js', alias: 'googlesyndication.com/adsbygoogle.js',
data: 'text', data: 'text',

View File

@ -1,3 +1,20 @@
/**
*
* Source below is based on Mozilla source code:
* https://searchfox.org/mozilla-central/rev/d317e93d9a59c9e4c06ada85fbff9f6a1ceaaad1/browser/extensions/webcompat/shims/google-ima.js
*
* Modifications to the original code below this comment:
* - Avoid JS syntax not supported by older browser versions
* - Add missing shim event
* - Modified to avoid jshint warnings as per uBO's config
*
* Related issue:
* - https://github.com/uBlockOrigin/uBlock-issues/issues/2158
*
**/
'use strict';
/* This Source Code Form is subject to the terms of the Mozilla Public /* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
@ -11,7 +28,7 @@
* site breakage, such as black bxoes where videos ought to be placed. * site breakage, such as black bxoes where videos ought to be placed.
*/ */
if (!window.google?.ima?.VERSION) { if (!window.google || !window.google.ima || !window.google.ima.VERSION) {
const VERSION = "3.517.2"; const VERSION = "3.517.2";
const CheckCanAutoplay = (function() { const CheckCanAutoplay = (function() {
@ -195,7 +212,7 @@ if (!window.google?.ima?.VERSION) {
{ type: "video/mp4" } { type: "video/mp4" }
); );
let testVideo = undefined; let testVideo;
return function() { return function() {
if (!testVideo) { if (!testVideo) {
@ -219,68 +236,70 @@ if (!window.google?.ima?.VERSION) {
} }
class ImaSdkSettings { class ImaSdkSettings {
#c = true; constructor() {
#f = {}; this.c = true;
#i = false; this.f = {};
#l = ""; this.i = false;
#p = ""; this.l = "";
#r = 0; this.p = "";
#t = ""; this.r = 0;
#v = ""; this.t = "";
this.v = "";
}
getCompanionBackfill() {} getCompanionBackfill() {}
getDisableCustomPlaybackForIOS10Plus() { getDisableCustomPlaybackForIOS10Plus() {
return this.#i; return this.i;
} }
getFeatureFlags() { getFeatureFlags() {
return this.#f; return this.f;
} }
getLocale() { getLocale() {
return this.#l; return this.l;
} }
getNumRedirects() { getNumRedirects() {
return this.#r; return this.r;
} }
getPlayerType() { getPlayerType() {
return this.#t; return this.t;
} }
getPlayerVersion() { getPlayerVersion() {
return this.#v; return this.v;
} }
getPpid() { getPpid() {
return this.#p; return this.p;
} }
isCookiesEnabled() { isCookiesEnabled() {
return this.#c; return this.c;
} }
setAutoPlayAdBreaks() {} setAutoPlayAdBreaks() {}
setCompanionBackfill() {} setCompanionBackfill() {}
setCookiesEnabled(c) { setCookiesEnabled(c) {
this.#c = !!c; this.c = !!c;
} }
setDisableCustomPlaybackForIOS10Plus(i) { setDisableCustomPlaybackForIOS10Plus(i) {
this.#i = !!i; this.i = !!i;
} }
setFeatureFlags(f) { setFeatureFlags(f) {
this.#f = f; this.f = f;
} }
setLocale(l) { setLocale(l) {
this.#l = l; this.l = l;
} }
setNumRedirects(r) { setNumRedirects(r) {
this.#r = r; this.r = r;
} }
setPlayerType(t) { setPlayerType(t) {
this.#t = t; this.t = t;
} }
setPlayerVersion(v) { setPlayerVersion(v) {
this.#v = v; this.v = v;
} }
setPpid(p) { setPpid(p) {
this.#p = p; this.p = p;
} }
setSessionId(s) {} setSessionId(/*s*/) {}
setVpaidAllowed(a) {} setVpaidAllowed(/*a*/) {}
setVpaidMode(m) {} setVpaidMode(/*m*/) {}
} }
ImaSdkSettings.CompanionBackfillMode = { ImaSdkSettings.CompanionBackfillMode = {
ALWAYS: "always", ALWAYS: "always",
@ -293,10 +312,12 @@ if (!window.google?.ima?.VERSION) {
}; };
class EventHandler { class EventHandler {
#listeners = new Map(); constructor() {
this.listeners = new Map();
}
_dispatch(e) { _dispatch(e) {
const listeners = this.#listeners.get(e.type) || []; const listeners = this.listeners.get(e.type) || [];
for (const listener of Array.from(listeners)) { for (const listener of Array.from(listeners)) {
try { try {
listener(e); listener(e);
@ -307,28 +328,33 @@ if (!window.google?.ima?.VERSION) {
} }
addEventListener(t, c) { addEventListener(t, c) {
if (!this.#listeners.has(t)) { if (!this.listeners.has(t)) {
this.#listeners.set(t, new Set()); this.listeners.set(t, new Set());
} }
this.#listeners.get(t).add(c); this.listeners.get(t).add(c);
} }
removeEventListener(t, c) { removeEventListener(t, c) {
this.#listeners.get(t)?.delete(c); const typeSet = this.listeners.get(t);
if (!typeSet) { return; }
typeSet.delete(c);
} }
} }
class AdsLoader extends EventHandler { class AdsLoader extends EventHandler {
#settings = new ImaSdkSettings(); constructor() {
super();
this.settings = new ImaSdkSettings();
}
contentComplete() {} contentComplete() {}
destroy() {} destroy() {}
getSettings() { getSettings() {
return this.#settings; return this.settings;
} }
getVersion() { getVersion() {
return VERSION; return VERSION;
} }
requestAds(r, c) { requestAds(/*r, c*/) {
// If autoplay is disabled and the page is trying to autoplay a tracking // If autoplay is disabled and the page is trying to autoplay a tracking
// ad, then IMA fails with an error, and the page is expected to request // ad, then IMA fails with an error, and the page is expected to request
// ads again later when the user clicks to play. // ads again later when the user clicks to play.
@ -351,7 +377,10 @@ if (!window.google?.ima?.VERSION) {
} }
class AdsManager extends EventHandler { class AdsManager extends EventHandler {
#volume = 1; constructor() {
super();
this.volume = 1;
}
collapse() {} collapse() {}
configureAdsManager() {} configureAdsManager() {}
destroy() {} destroy() {}
@ -374,9 +403,9 @@ if (!window.google?.ima?.VERSION) {
return 0; return 0;
} }
getVolume() { getVolume() {
return this.#volume; return this.volume;
} }
init(w, h, m, e) {} init(/*w, h, m, e*/) {}
isCustomClickTrackingUsed() { isCustomClickTrackingUsed() {
return false; return false;
} }
@ -385,10 +414,10 @@ if (!window.google?.ima?.VERSION) {
} }
pause() {} pause() {}
requestNextAdBreak() {} requestNextAdBreak() {}
resize(w, h, m) {} resize(/*w, h, m*/) {}
resume() {} resume() {}
setVolume(v) { setVolume(v) {
this.#volume = v; this.volume = v;
} }
skip() {} skip() {}
start() { start() {
@ -396,6 +425,7 @@ if (!window.google?.ima?.VERSION) {
for (const type of [ for (const type of [
AdEvent.Type.LOADED, AdEvent.Type.LOADED,
AdEvent.Type.STARTED, AdEvent.Type.STARTED,
AdEvent.Type.CONTENT_RESUME_REQUESTED,
AdEvent.Type.AD_BUFFERING, AdEvent.Type.AD_BUFFERING,
AdEvent.Type.FIRST_QUARTILE, AdEvent.Type.FIRST_QUARTILE,
AdEvent.Type.MIDPOINT, AdEvent.Type.MIDPOINT,
@ -412,7 +442,7 @@ if (!window.google?.ima?.VERSION) {
}); });
} }
stop() {} stop() {}
updateAdsRenderingSettings(s) {} updateAdsRenderingSettings(/*s*/) {}
} }
class AdsRenderingSettings {} class AdsRenderingSettings {}
@ -445,7 +475,9 @@ if (!window.google?.ima?.VERSION) {
} }
class Ad { class Ad {
_pi = new AdPodInfo(); constructor() {
this._pi = new AdPodInfo();
}
getAdId() { getAdId() {
return ""; return "";
} }
@ -566,31 +598,27 @@ if (!window.google?.ima?.VERSION) {
} }
class AdError { class AdError {
#errorCode = -1;
#message = "";
#type = "";
#vastErrorCode = -1;
constructor(type, code, vast, message) { constructor(type, code, vast, message) {
this.#errorCode = code; this.errorCode = code;
this.#message = message; this.message = message;
this.#type = type; this.type = type;
this.#vastErrorCode = vast; this.vastErrorCode = vast;
} }
getErrorCode() { getErrorCode() {
return this.#errorCode; return this.errorCode;
} }
getInnerError() {} getInnerError() {}
getMessage() { getMessage() {
return this.#message; return this.message;
} }
getType() { getType() {
return this.#type; return this.type;
} }
getVastErrorCode() { getVastErrorCode() {
return this.#vastErrorCode; return this.vastErrorCode;
} }
toString() { toString() {
return `AdError ${this.#errorCode}: ${this.#message}`; return `AdError ${this.errorCode}: ${this.message}`;
} }
} }
AdError.ErrorCode = {}; AdError.ErrorCode = {};
@ -599,7 +627,11 @@ if (!window.google?.ima?.VERSION) {
const isEngadget = () => { const isEngadget = () => {
try { try {
for (const ctx of Object.values(window.vidible._getContexts())) { for (const ctx of Object.values(window.vidible._getContexts())) {
if (ctx.getPlayer()?.div?.innerHTML.includes("www.engadget.com")) { const player = ctx.getPlayer();
if (!player) { continue;}
const div = player.div;
if (!div) { continue; }
if (div.innerHTML.includes("www.engadget.com")) {
return true; return true;
} }
} }
@ -656,13 +688,12 @@ if (!window.google?.ima?.VERSION) {
}; };
class AdErrorEvent { class AdErrorEvent {
type = "adError";
#error = "";
constructor(error) { constructor(error) {
this.#error = error; this.type = "adError";
this.error = error;
} }
getError() { getError() {
return this.#error; return this.error;
} }
getUserRequestContext() { getUserRequestContext() {
return {}; return {};