diff --git a/extension/content-script.js b/extension/content-script.js --- a/extension/content-script.js +++ b/extension/content-script.js @@ -814,27 +814,36 @@ player.addEventListener("pause", player.replayAfterRemoval); `; - executeScript(`function() { + const handleCreateElement = ` + const tagName = arguments[0]; + if (typeof tagName === "string") { + if (tagName.toLowerCase() === "audio") { + const player = createdTag; + ${addPlayerToDomEvadingAutoPlayBlocking} + } else if (tagName.toLowerCase() === "video") { + (document.head || document.documentElement).appendChild(createdTag); + createdTag.parentNode.removeChild(createdTag); + } + } + `; + + if (IS_FIREFOX) { + var oldCreateElement = Document.prototype.createElement; + exportFunction(function() { + const createdTag = oldCreateElement.apply(this, arguments); + eval(handleCreateElement); + return createdTag; + }, document, {defineAs: "createElement"}); + } else { + 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") { - const player = createdTag; - ${addPlayerToDomEvadingAutoPlayBlocking} - } else if (tagName.toLowerCase() === "video") { - (document.head || document.documentElement).appendChild(createdTag); - createdTag.parentNode.removeChild(createdTag); - } - } - + const createdTag = oldCreateElement.apply(this, arguments); + ${handleCreateElement} return createdTag; }; - } - `); + `); + } // 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