Skip to content

Commit

Permalink
Relocate the injected button to the h3 element
Browse files Browse the repository at this point in the history
  • Loading branch information
farcaller committed May 2, 2024
1 parent ca15cc9 commit 4725934
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions extension/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,14 @@ function ensureTALinks() {

let titleContainerNodes = getTitleContainers();
for (let titleContainer of titleContainerNodes) {
if (titleContainer.hasTA) continue;
let parent = getNearestH3(titleContainer);
if (!parent) continue;
if (parent.hasTA) continue;
let videoButton = buildVideoButton(titleContainer);
if (videoButton == null) continue;
processTitle(titleContainer);
titleContainer.appendChild(videoButton);
titleContainer.hasTA = true;
processTitle(parent);
parent.appendChild(videoButton);
parent.hasTA = true;
}
}
ensureTALinks = throttled(ensureTALinks, 700);
Expand Down Expand Up @@ -329,6 +331,8 @@ function getTitleContainers() {
}

function getVideoId(titleContainer) {
if (!titleContainer) return undefined;

let href = getNearestLink(titleContainer);
if (!href) return;

Expand Down Expand Up @@ -394,6 +398,16 @@ function getNearestLink(element) {
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) {
if (titleContainer.hasListener) return;
Object.assign(titleContainer.style, {
Expand Down Expand Up @@ -439,9 +453,9 @@ function checkVideoExists(taButton) {
console.error(e);
}

let videoId = taButton.dataset.id;
if (taButton.parentElement) {
videoId = getVideoId(taButton.parentElement);
let aElem = taButton?.parentElement?.querySelector('a');
let videoId = getVideoId(aElem);;
if (aElem) {
taButton.setAttribute('data-id', videoId);
taButton.setAttribute('data-type', 'video');
taButton.title = `TA download video: ${taButton.parentElement.innerText} [${videoId}]`;
Expand Down

0 comments on commit 4725934

Please sign in to comment.