mirror of
https://github.com/tubearchivist/browser-extension.git
synced 2024-11-25 13:10:14 +00:00
Relocate the injected button to the h3 element (#37)
This commit is contained in:
parent
ca15cc9c0b
commit
c82e493628
@ -135,12 +135,14 @@ function ensureTALinks() {
|
|||||||
|
|
||||||
let titleContainerNodes = getTitleContainers();
|
let titleContainerNodes = getTitleContainers();
|
||||||
for (let titleContainer of titleContainerNodes) {
|
for (let titleContainer of titleContainerNodes) {
|
||||||
if (titleContainer.hasTA) continue;
|
let parent = getNearestH3(titleContainer);
|
||||||
|
if (!parent) continue;
|
||||||
|
if (parent.hasTA) continue;
|
||||||
let videoButton = buildVideoButton(titleContainer);
|
let videoButton = buildVideoButton(titleContainer);
|
||||||
if (videoButton == null) continue;
|
if (videoButton == null) continue;
|
||||||
processTitle(titleContainer);
|
processTitle(parent);
|
||||||
titleContainer.appendChild(videoButton);
|
parent.appendChild(videoButton);
|
||||||
titleContainer.hasTA = true;
|
parent.hasTA = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ensureTALinks = throttled(ensureTALinks, 700);
|
ensureTALinks = throttled(ensureTALinks, 700);
|
||||||
@ -329,6 +331,8 @@ function getTitleContainers() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getVideoId(titleContainer) {
|
function getVideoId(titleContainer) {
|
||||||
|
if (!titleContainer) return undefined;
|
||||||
|
|
||||||
let href = getNearestLink(titleContainer);
|
let href = getNearestLink(titleContainer);
|
||||||
if (!href) return;
|
if (!href) return;
|
||||||
|
|
||||||
@ -394,6 +398,16 @@ function getNearestLink(element) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getNearestH3(element) {
|
||||||
|
for (let i = 0; i < 5 && element && element !== document; i++) {
|
||||||
|
if (element.tagName === 'H3') {
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
element = element.parentNode;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
function processTitle(titleContainer) {
|
function processTitle(titleContainer) {
|
||||||
if (titleContainer.hasListener) return;
|
if (titleContainer.hasListener) return;
|
||||||
Object.assign(titleContainer.style, {
|
Object.assign(titleContainer.style, {
|
||||||
@ -439,9 +453,9 @@ function checkVideoExists(taButton) {
|
|||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
let videoId = taButton.dataset.id;
|
let aElem = taButton?.parentElement?.querySelector('a');
|
||||||
if (taButton.parentElement) {
|
let videoId = getVideoId(aElem);;
|
||||||
videoId = getVideoId(taButton.parentElement);
|
if (aElem) {
|
||||||
taButton.setAttribute('data-id', videoId);
|
taButton.setAttribute('data-id', videoId);
|
||||||
taButton.setAttribute('data-type', 'video');
|
taButton.setAttribute('data-type', 'video');
|
||||||
taButton.title = `TA download video: ${taButton.parentElement.innerText} [${videoId}]`;
|
taButton.title = `TA download video: ${taButton.parentElement.innerText} [${videoId}]`;
|
||||||
|
Loading…
Reference in New Issue
Block a user