From f9feee70d1edc690fe927365b78625a269dda8e2 Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 25 Aug 2023 17:42:43 +0700 Subject: [PATCH] refactor channel buttons --- extension/script.js | 80 +++++++++++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 32 deletions(-) diff --git a/extension/script.js b/extension/script.js index 9865f62..7c53f1e 100644 --- a/extension/script.js +++ b/extension/script.js @@ -115,7 +115,7 @@ const defaultIcon = ` { e.preventDefault(); - let currentLocation = window.location.href; - console.log('subscribe to: ' + currentLocation); - sendUrl(currentLocation, 'subscribe', subLink); + console.log(`subscribe to: ${channelHandle}`); + sendUrl(channelHandle, 'subscribe', subLink); }); - subLink.addEventListener('mouseover', e => { - let subText; - if (window.location.pathname === '/watch') { - let currentLocation = window.location.href; - subText = currentLocation; - } else { - subText = channelContainer.querySelector('#text').textContent; - } - - e.target.title = 'TA Subscribe: ' + subText; + subLink.addEventListener('mouseover', () => { + checkChannelSubscribed(channelHandle); }); Object.assign(subLink.style, { padding: '5px', @@ -158,6 +153,10 @@ function buildSubLink(channelContainer) { return subLink; } +function checkChannelSubscribed(channelHandle) { + console.log(`check channel subscribed: ${channelHandle}`); +} + function buildSpacer() { let spacer = document.createElement('span'); spacer.innerText = '|'; @@ -165,27 +164,28 @@ function buildSpacer() { return spacer; } -function buildDlLink(channelContainer) { +function buildDlLink() { let dlLink = document.createElement('span'); - dlLink.innerHTML = downloadIcon; + let currentLocation = window.location.href; + let urlObj = new URL(currentLocation); + if (urlObj.pathname.startsWith('/watch')) { + let videoId = urlObj.search.split('=')[1]; + dlLink.setAttribute('data-type', 'video'); + dlLink.setAttribute('data-id', videoId); + dlLink.title = `TA download video: ${videoId}`; + } else { + let toDownload = urlObj.pathname.slice(1); + dlLink.setAttribute('data-id', toDownload); + dlLink.setAttribute('data-type', 'channel'); + dlLink.title = `TA download channel ${toDownload}`; + } + dlLink.innerHTML = downloadIcon; dlLink.addEventListener('click', e => { e.preventDefault(); - let currentLocation = window.location.href; - console.log('download: ' + currentLocation); + console.log(`download: ${currentLocation}`); sendUrl(currentLocation, 'download', dlLink); }); - dlLink.addEventListener('mouseover', e => { - let subText; - let currentLocation = window.location.href; - if (window.location.pathname === '/watch') { - subText = currentLocation; - } else { - subText = channelContainer.querySelector('#text').textContent + ' ' + currentLocation; - } - - e.target.title = 'TA Download: ' + subText; - }); Object.assign(dlLink.style, { filter: 'invert()', width: '20px', @@ -196,16 +196,28 @@ function buildDlLink(channelContainer) { return dlLink; } +function getChannelHandle(channelContainer) { + const channelHandleContainer = document.querySelector('#channel-handle'); + let channelHandle = channelHandleContainer ? channelHandleContainer.innerText : null; + if (!channelHandle) { + let href = channelContainer.querySelector('.ytd-video-owner-renderer').href; + const urlObj = new URL(href); + channelHandle = urlObj.pathname.split('/')[1]; + } + return channelHandle; +} + function buildChannelButton(channelContainer) { + let channelHandle = getChannelHandle(channelContainer); let buttonDiv = buildButtonDiv(); - let subLink = buildSubLink(channelContainer); + let subLink = buildSubLink(channelHandle); buttonDiv.appendChild(subLink); let spacer = buildSpacer(); buttonDiv.appendChild(spacer); - let dlLink = buildDlLink(channelContainer); + let dlLink = buildDlLink(); buttonDiv.appendChild(dlLink); return buttonDiv; @@ -433,6 +445,10 @@ function cleanButtons() { button.parentElement.hasTA = false; button.remove(); }); + document.querySelectorAll('.ta-channel-button').forEach(button => { + button.parentElement.hasTA = false; + button.remove(); + }); } let oldHref = document.location.href;