fix checkVideoExists, check siblings in getNearestLink

This commit is contained in:
Simon 2024-05-13 21:53:45 +02:00
parent 9ba90e4e45
commit 7b40dc44c2
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4

View File

@ -389,6 +389,24 @@ function buildVideoButton(titleContainer) {
} }
function getNearestLink(element) { function getNearestLink(element) {
// Check siblings
let sibling = element;
while (sibling) {
sibling = sibling.previousElementSibling;
if (sibling && sibling.tagName === 'A' && sibling.getAttribute('href') !== '#') {
return sibling.getAttribute('href');
}
}
sibling = element;
while (sibling) {
sibling = sibling.nextElementSibling;
if (sibling && sibling.tagName === 'A' && sibling.getAttribute('href') !== '#') {
return sibling.getAttribute('href');
}
}
// Check parent elements
for (let i = 0; i < 5 && element && element !== document; i++) { for (let i = 0; i < 5 && element && element !== document; i++) {
if (element.tagName === 'A' && element.getAttribute('href') !== '#') { if (element.tagName === 'A' && element.getAttribute('href') !== '#') {
return element.getAttribute('href'); return element.getAttribute('href');
@ -453,12 +471,14 @@ function checkVideoExists(taButton) {
console.error(e); console.error(e);
} }
let aElem = taButton?.parentElement?.querySelector('a'); let videoId = taButton.dataset.id;
let videoId = getVideoId(aElem);; if (!videoId) {
if (aElem) { videoId = getVideoId(taButton);
taButton.setAttribute('data-id', videoId); if (videoId) {
taButton.setAttribute('data-type', 'video'); taButton.setAttribute('data-id', videoId);
taButton.title = `TA download video: ${taButton.parentElement.innerText} [${videoId}]`; taButton.setAttribute('data-type', 'video');
taButton.title = `TA download video: ${taButton.parentElement.innerText} [${videoId}]`;
}
} }
let message = { type: 'videoExists', videoId }; let message = { type: 'videoExists', videoId };