https://bugs.kde.org/show_bug.cgi?id=411742

--- Comment #11 from Frederick Zhang <frederick...@tsundere.moe> ---
I'm using Firefox 71.0b8 (64-bit). Nothing unusual observed from web console,
browser console or extension debugging console.

I ran
> curl -L https://phabricator.kde.org/D24870\?download\=true | git apply
...to get apply the patch so I think it should be the latest one?

Here's my git diff just in case:

diff --git a/extension/content-script.js b/extension/content-script.js
index 68dd9912..a8aa7faa 100644
--- a/extension/content-script.js
+++ b/extension/content-script.js
@@ -780,17 +780,47 @@ function loadMediaSessionsShim() {
         // HACK We cannot really pass variables from the page's scope to our
content-script's scope
         // so we just blatantly insert the <audio> tag in the DOM and pick it
up through our regular
         // mechanism. Let's see how this goes :D

+        // HACK When removing a media object from DOM it is paused, so what we
do here is once the
+        // player loaded some data we add it (doesn't work earlier since it
cannot pause when
+        // there's nothing loaded to pause) to the DOM and before we remove
it, we note down that
+        // we will now get a paused event because of that. When we get it, we
just play() the player
+        // so it continues playing :-)
+        const addPlayerToDomEvadingAutoPlayBlocking = `
+            player.registerInDom = () => {
+                player.pausedBecauseOfDomRemoval = true;
+                player.removeEventListener("play", player.registerInDom);
+
+                (document.head ||
document.documentElement).appendChild(player);
+                player.parentNode.removeChild(player);
+            };
+
+            player.replayAfterRemoval = () => {
+                if (player.pausedBecauseOfDomRemoval === true) {
+                    delete player.pausedBecauseOfDomRemoval;
+                    player.removeEventListener("pause",
player.replyAfterRemoval);
+
+                    player.play();
+                }
+            };
+
+            player.addEventListener("play", player.registerInDom);
+            player.addEventListener("pause", player.replayAfterRemoval);
+        `;
+
         executeScript(`function() {
                 var oldCreateElement = Document.prototype.createElement;
                 Document.prototype.createElement = function() {
                     var createdTag = oldCreateElement.apply(this, arguments);

                     var tagName = arguments[0];

                     if (typeof tagName === "string") {
-                        if (tagName.toLowerCase() === "audio" ||
tagName.toLowerCase() === "video") {
+                        if (tagName.toLowerCase() === "audio") {
+                            const player = createdTag;
+                            ${addPlayerToDomEvadingAutoPlayBlocking}
+                        } else if (tagName.toLowerCase() === "video") {
                             (document.head ||
document.documentElement).appendChild(createdTag);
                             createdTag.parentNode.removeChild(createdTag);
                         }
                     }
@@ -802,13 +832,9 @@ function loadMediaSessionsShim() {

         // We also briefly add items created as new Audio() to the DOM so we
can control it
         // similar to the document.createElement hack above since we cannot
share variables
         // between the actual website and the background script despite them
sharing the same DOM
-        // HACK When removing a media object from DOM it is paused, so what we
do here is once the
-        // player loaded some data we add it (doesn't work earlier since it
cannot pause when
-        // there's nothing loaded to pause) to the DOM and before we remove
it, we note down that
-        // we will now get a paused event because of that. When we get it, we
just play() the player
-        // so it continues playing :-)
+
         if (IS_FIREFOX) {
             // Firefox enforces Content-Security-Policy also for scripts
injected by the content-script
             // This causes our executeScript calls to fail for pages like
Nextcloud
             // It also doesn't seem to have the aggressive autoplay prevention
Chrome has,
@@ -825,34 +851,12 @@ function loadMediaSessionsShim() {
             }, window, {defineAs: "Audio"});
         } else {
             executeScript(`function() {
                 var oldAudio = window.Audio;
-                window.Audio = function () {
-                    var createdAudio = new
(Function.prototype.bind.apply(oldAudio, arguments));
-
-                    createdAudio.registerInDom = function() {
-                        (document.head ||
document.documentElement).appendChild(createdAudio);
-                        createdAudio.pausedBecauseOfDomRemoval = true;
-                        createdAudio.parentNode.removeChild(createdAudio);
-
-                        createdAudio.removeEventListener("loadeddata",
createdAudio.registerInDom);
-                    };
-
-                    createdAudio.replayAfterRemoval = function() {
-                        if (createdAudio.pausedBecauseOfDomRemoval) {
-                            if (createdAudio.paused) {
-                                createdAudio.play();
-                            }
-                            delete createdAudio.pausedBecauseOfDomRemoval;
-
-                            createdAudio.removeEventListener("pause",
createdAudio.replayAfterRemoval);
-                        }
-                    };
-
-                    createdAudio.addEventListener("loadeddata",
createdAudio.registerInDom);
-                    createdAudio.addEventListener("pause",
createdAudio.replayAfterRemoval);
-
-                    return createdAudio;
+                window.Audio = function (...args) {
+                    const player = new oldAudio(...args);
+                    ${addPlayerToDomEvadingAutoPlayBlocking}
+                    return player;
                 };
             }`);
         }
     }

-- 
You are receiving this mail because:
You are watching all bug changes.

Reply via email to