Skip to content

Commit

Permalink
fix: iframe issue (#65)
Browse files Browse the repository at this point in the history
* fix: iframe issue

* Version bump
  • Loading branch information
cyfung1031 authored Dec 4, 2023
1 parent 50bae2a commit 219e546
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions YoutubeGeniusLyrics.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// @author cuzi
// @icon https://raw.githubusercontent.com/hfg-gmuend/openmoji/master/color/72x72/E044.png
// @supportURL https://github.com/cvzi/Youtube-Genius-Lyrics-userscript/issues
// @version 10.9.46
// @version 10.9.47
// @require https://greasyfork.org/scripts/406698-geniuslyrics/code/GeniusLyrics.js
// @grant GM.xmlHttpRequest
// @grant GM.setValue
Expand Down Expand Up @@ -57,6 +57,7 @@ const Promise = (async () => { })().constructor // YouTube polyfill to Promise i
let lyricsDisplayState = 'hidden'
let disableShowLyricsButton = false // hide if the page is confirmed as non-video page
let isYouTubeLive = false
let iframeBlankURL = null

function addCss () {
let style = document.querySelector('style#youtube_genius_lyrics_style')
Expand Down Expand Up @@ -704,23 +705,23 @@ const { requestAnimationFrame, setTimeout, setInterval, clearTimeout, clearInter
let frame = document.getElementById(frameId)
if (!frame) {
frame = document.createElement('iframe')
frame.id = 'vanillajs-iframe-v1'
frame.id = frameId
const blobURL = typeof webkitCancelAnimationFrame === 'function' ? (frame.src = URL.createObjectURL(new Blob([], { type: 'text/html' }))) : null // avoid Brave Crash
frame.sandbox = 'allow-same-origin' // script cannot be run inside iframe but API can be obtained from iframe
let n = document.createElement('noscript') // wrap into NOSCRPIT to avoid reflow (layouting)
n.appendChild(frame)
const root = document.documentElement
if (root) {
root.appendChild(n)
if (blobURL) Promise.resolve().then(() => URL.revokeObjectURL(blobURL))
removeIframeFn = (setTimeout) => {
const removeIframeOnDocumentReady = (e) => {
e && win.removeEventListener('DOMContentLoaded', removeIframeOnDocumentReady, false)
win = null
setTimeout(() => {
n.remove()
n = null
}, 200)
e = n
n = win = removeIframeFn = 0
setTimeout ? setTimeout(() => e.remove(), 200) : e.remove()
}
if (document.readyState !== 'loading') {
if (!setTimeout || document.readyState !== 'loading') {
removeIframeOnDocumentReady()
} else {
win.addEventListener('DOMContentLoaded', removeIframeOnDocumentReady, false)
Expand All @@ -731,11 +732,15 @@ const { requestAnimationFrame, setTimeout, setInterval, clearTimeout, clearInter

const fc = frame ? frame.contentWindow : null
if (fc) {
const { requestAnimationFrame, setTimeout, setInterval, clearTimeout, clearInterval } = fc
const res = { requestAnimationFrame, setTimeout, setInterval, clearTimeout, clearInterval }
for (const k in res) res[k] = res[k].bind(win) // necessary
if (removeIframeFn) Promise.resolve(res.setTimeout).then(removeIframeFn)
return res
try {
const { requestAnimationFrame, setTimeout, setInterval, clearTimeout, clearInterval } = fc
const res = { requestAnimationFrame, setTimeout, setInterval, clearTimeout, clearInterval }
for (const k in res) res[k] = res[k].bind(win) // necessary
if (removeIframeFn) Promise.resolve(res.setTimeout).then(removeIframeFn)
return res
} catch (e) {
if (removeIframeFn) removeIframeFn()
}
}
} catch (e) {
console.warn(e)
Expand Down Expand Up @@ -1892,6 +1897,9 @@ function setupLyricsDisplayDOM (song, searchresultsLengths) { // eslint-disable-
}

const iframe = document.createElement('iframe')
if (typeof webkitCancelAnimationFrame === 'function' && ((window || 0).navigator || 0).brave) {
iframe.src = (iframeBlankURL || (iframeBlankURL = URL.createObjectURL(new Blob([], { type: 'text/html' })))) // Brave
}
iframe.id = 'lyricsiframe'
iframe.style.opacity = 0.1

Expand Down

0 comments on commit 219e546

Please sign in to comment.