From f8d69f58830e178b3eb34e386e6adaee6298ec3f Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 9 Nov 2023 18:21:43 +0700 Subject: [PATCH] implement extract video id on hover --- extension/script.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/extension/script.js b/extension/script.js index 2011ed8..42d1e61 100644 --- a/extension/script.js +++ b/extension/script.js @@ -326,12 +326,9 @@ function getTitleContainers() { return elements; } -function buildVideoButton(titleContainer) { +function getVideoId(titleContainer) { let href = getNearestLink(titleContainer); if (!href) return; - const dlButton = document.createElement('a'); - dlButton.classList.add('ta-button'); - dlButton.href = '#'; let videoId; if (href.startsWith('/watch?v')) { @@ -340,11 +337,16 @@ function buildVideoButton(titleContainer) { } else if (href.startsWith('/shorts/')) { videoId = href.split('/')[2]; } + return videoId; +} + +function buildVideoButton(titleContainer) { + let videoId = getVideoId(titleContainer); if (!videoId) return; - dlButton.setAttribute('data-id', videoId); - dlButton.setAttribute('data-type', 'video'); - dlButton.title = `TA download video: ${titleContainer.innerText} [${videoId}]`; + const dlButton = document.createElement('a'); + dlButton.classList.add('ta-button'); + dlButton.href = '#'; Object.assign(dlButton.style, { display: 'flex', @@ -435,7 +437,12 @@ function checkVideoExists(taButton) { console.error(e); } - let videoId = taButton.dataset.id; + if (!taButton.parentElement) return; + let videoId = getVideoId(taButton.parentElement); + taButton.setAttribute('data-id', videoId); + taButton.setAttribute('data-type', 'video'); + taButton.title = `TA download video: ${taButton.parentElement.innerText} [${videoId}]`; + let message = { type: 'videoExists', videoId }; let sending = sendMessage(message); sending.then(handleResponse, handleError);