diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /browser/extensions/webcompat/shims/adsafeprotected-ima.js | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'browser/extensions/webcompat/shims/adsafeprotected-ima.js')
-rw-r--r-- | browser/extensions/webcompat/shims/adsafeprotected-ima.js | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/browser/extensions/webcompat/shims/adsafeprotected-ima.js b/browser/extensions/webcompat/shims/adsafeprotected-ima.js new file mode 100644 index 0000000000..de273de7d8 --- /dev/null +++ b/browser/extensions/webcompat/shims/adsafeprotected-ima.js @@ -0,0 +1,69 @@ +/* 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 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +/** + * Bug 1508639 - Shim Ad Safe Protected's Google IMA adapter + */ + +if (!window.googleImaVansAdapter) { + const shimId = "AdSafeProtectedGoogleIMAAdapter"; + + const sendMessageToAddon = (function() { + const pendingMessages = new Map(); + const channel = new MessageChannel(); + channel.port1.onerror = console.error; + channel.port1.onmessage = event => { + const { messageId, response } = event.data; + const resolve = pendingMessages.get(messageId); + if (resolve) { + pendingMessages.delete(messageId); + resolve(response); + } + }; + function reconnect() { + const detail = { + pendingMessages: [...pendingMessages.values()], + port: channel.port2, + shimId, + }; + window.dispatchEvent(new CustomEvent("ShimConnects", { detail })); + } + window.addEventListener("ShimHelperReady", reconnect); + reconnect(); + return function(message) { + const messageId = + Math.random() + .toString(36) + .substring(2) + Date.now().toString(36); + return new Promise(resolve => { + const payload = { + message, + messageId, + shimId, + }; + pendingMessages.set(messageId, resolve); + channel.port1.postMessage(payload); + }); + }; + })(); + + window.googleImaVansAdapter = { + init: () => {}, + dispose: () => {}, + }; + + // Treat it as an opt-in when the user clicks on a video + // TODO: Improve this! It races to tell the bg script to unblock the ad from + // https://pubads.g.doubleclick.net/gampad/ads before the page loads them. + async function click(e) { + if (e.isTrusted && e.target.closest("#video-player")) { + document.documentElement.removeEventListener("click", click, true); + await sendMessageToAddon("optIn"); + // TODO: reload ima3.js? + } + } + document.documentElement.addEventListener("click", click, true); +} |