diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Sandboxed/SelectorsPollerScript.js b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Sandboxed/SelectorsPollerScript.js index ac407c53be1d..9b5295cb121b 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Sandboxed/SelectorsPollerScript.js +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Sandboxed/SelectorsPollerScript.js @@ -1021,12 +1021,24 @@ window.__firefox__.execute(function($) { styleElm.setAttribute('type', 'text/css') targetElm.appendChild(styleElm) CC.cosmeticStyleSheet = styleElm + // The previous `nextElementSibling` we moved our stylesheet below + var prevNextElementSibling = null; // Start a timer that moves the stylesheet down window.setInterval(() => { if (styleElm.nextElementSibling === null || styleElm.parentElement !== targetElm) { return } + if (styleElm.nextElementSibling !== null) { + // if we already moved below this element + if (prevNextElementSibling === styleElm.nextElementSibling) { + // Avoid a loop where we are repeatedly swapping places with another + // element. This can happen with `darkreader` (night mode) for + // example and cause unwanted animations to repeat. + return + } + prevNextElementSibling = styleElm.nextElementSibling; + } moveStyle() }, 1000) }