From e329eb4e80a8e7318d296df82e20f561dada82d0 Mon Sep 17 00:00:00 2001 From: itcatplayit Date: Sun, 21 May 2023 10:08:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20=E5=BF=85=E8=A6=81=E6=97=B6?= =?UTF-8?q?=E5=8F=96=E6=B6=88=E5=AE=9A=E6=97=B6=E5=99=A8=EF=BC=8C=E6=8F=90?= =?UTF-8?q?=E9=AB=98=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chrome/content-script.cjs | 43 ++++++++++++++++------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/src/chrome/content-script.cjs b/src/chrome/content-script.cjs index 35cb903..237d60e 100644 --- a/src/chrome/content-script.cjs +++ b/src/chrome/content-script.cjs @@ -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) From c62f04806d44e7597bb98c841258979fcb1f9e6a Mon Sep 17 00:00:00 2001 From: itcatplayit Date: Sun, 21 May 2023 10:10:46 +0800 Subject: [PATCH 2/2] fix: Cannot read properties of null (reading 'style') bug --- src/chrome/content-script.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chrome/content-script.cjs b/src/chrome/content-script.cjs index 237d60e..79b4167 100644 --- a/src/chrome/content-script.cjs +++ b/src/chrome/content-script.cjs @@ -234,7 +234,7 @@ window.addEventListener("message", (event) => { container.append(div) } } - text.style.display = trans ? 'block' : 'none' + text && (text.style.display = trans ? 'block' : 'none') } }, false);