refactor: 必要时取消定时器,提高性能

This commit is contained in:
itcatplayit
2023-05-21 10:08:31 +08:00
parent d6a41df2d0
commit e329eb4e80

View File

@@ -6,33 +6,30 @@ const getVideoElement = () => {
return videoWrapper.querySelector('video')
}
var danmukuBoxLoaded = false
setInterval(function () {
if (danmukuBoxLoaded) return
let danmukuBox = null
const timerIframe = setInterval(function () {
if (danmukuBox) return clearInterval(timerIframe)
var danmukuBox = document.getElementById('danmukuBox')
danmukuBox = document.getElementById('danmukuBox')
if (danmukuBox) {
danmukuBoxLoaded = true
setTimeout(function () {
var vKey = ''
for (const key in danmukuBox?.dataset) {
if (key.startsWith('v-')) {
vKey = key
break
}
var vKey = ''
for (const key in danmukuBox?.dataset) {
if (key.startsWith('v-')) {
vKey = key
break
}
}
const iframe = document.createElement('iframe')
iframe.id = IFRAME_ID
iframe.src = chrome.runtime.getURL('index.html')
iframe.style = 'border: none; width: 100%; height: 44px;'
iframe.allow = 'clipboard-read; clipboard-write;'
if (vKey) {
iframe.dataset[vKey] = danmukuBox?.dataset[vKey]
}
//insert before first child
danmukuBox?.insertBefore(iframe, danmukuBox?.firstChild)
}, 1500)
const iframe = document.createElement('iframe')
iframe.id = IFRAME_ID
iframe.src = chrome.runtime.getURL('index.html')
iframe.style = 'border: none; width: 100%; height: 44px;'
iframe.allow = 'clipboard-read; clipboard-write;'
if (vKey) {
iframe.dataset[vKey] = danmukuBox?.dataset[vKey]
}
//insert before first child
danmukuBox?.insertBefore(iframe, danmukuBox?.firstChild)
}
}, 1000)