mv video dl button in title container

This commit is contained in:
Simon 2023-08-24 11:49:00 +07:00
parent 8cad2bcc22
commit f0ec9e23a7
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4

View File

@ -214,69 +214,11 @@ function getChannelContainers() {
return nodes; return nodes;
} }
function getThubnailContainers() { function getTitleContainers() {
let nodes = document.querySelectorAll('#thumbnail'); let nodes = document.querySelectorAll('#video-title');
return nodes; return nodes;
} }
function buildVideoButton(thumbContainer) {
let thumbLink = thumbContainer?.href;
if (!thumbLink) return;
if (thumbLink.includes('list=')) return;
let ggp = thumbContainer?.parentElement?.parentElement;
if (ggp?.id !== 'dismissible') return;
let dlButton = document.createElement('a');
dlButton.setAttribute('id', 'ta-video-button');
dlButton.href = '#';
dlButton.addEventListener('click', e => {
e.preventDefault();
let videoLink = thumbContainer.href;
console.log('download: ' + videoLink);
sendUrl(videoLink, 'download', dlButton);
});
ggp.addEventListener('mouseover', () => {
Object.assign(dlButton.style, {
opacity: 1,
});
let videoTitle = thumbContainer.href;
dlButton.title = 'TA download: ' + videoTitle;
});
ggp.addEventListener('mouseout', () => {
Object.assign(dlButton.style, {
opacity: 0,
});
});
Object.assign(dlButton.style, {
display: 'flex',
position: 'absolute',
top: '5px',
left: '5px',
alignItems: 'center',
backgroundColor: '#00202f',
color: '#fff',
fontSize: '1.4rem',
textDecoration: 'none',
borderRadius: '8px',
cursor: 'pointer',
opacity: 0,
});
let dlIcon = document.createElement('span');
dlIcon.innerHTML = downloadIcon;
Object.assign(dlIcon.style, {
filter: 'invert()',
width: '18px',
padding: '7px 8px',
});
dlButton.appendChild(dlIcon);
return dlButton;
}
// fix positioning of #owner div to fit new button // fix positioning of #owner div to fit new button
function adjustOwner(channelContainer) { function adjustOwner(channelContainer) {
let sponsorButton = channelContainer.querySelector('#sponsor-button'); let sponsorButton = channelContainer.querySelector('#sponsor-button');
@ -311,17 +253,96 @@ function ensureTALinks() {
channelContainer.hasTA = true; channelContainer.hasTA = true;
} }
let thumbContainerNodes = getThubnailContainers(); let titleContainerNodes = getTitleContainers();
for (let titleContainer of titleContainerNodes) {
for (let thumbContainer of thumbContainerNodes) { if (titleContainer.hasTA) continue;
if (thumbContainer.hasTA) continue; let videoButton = buildVideoButton(titleContainer);
let videoButton = buildVideoButton(thumbContainer);
if (videoButton == null) continue; if (videoButton == null) continue;
thumbContainer.parentElement.appendChild(videoButton); processTitle(titleContainer);
thumbContainer.hasTA = true; titleContainer.appendChild(videoButton);
titleContainer.hasTA = true;
} }
} }
function getNearestLink(element) {
for (let i = 0; i < 5 && element && element !== document; i++) {
if (element.tagName === 'A' && element.getAttribute('href') !== '#') {
return element.getAttribute('href');
}
element = element.parentNode;
}
return null;
}
function processTitle(titleContainer) {
Object.assign(titleContainer.style, {
display: 'flex',
gap: '15px',
});
titleContainer.classList.add('title-container');
titleContainer.addEventListener('mouseover', () => {
const taButton = titleContainer.querySelector('.ta-button');
taButton.style.opacity = 1;
});
titleContainer.addEventListener('mouseout', () => {
const taButton = titleContainer.querySelector('.ta-button');
taButton.style.opacity = 0;
});
}
function buildVideoButton(titleContainer) {
let href = getNearestLink(titleContainer);
const dlButton = document.createElement('a');
dlButton.classList.add('ta-button');
dlButton.href = '#';
let params = new URLSearchParams(href);
let videoId = params.get('/watch?v');
dlButton.setAttribute('data-id', videoId);
dlButton.setAttribute('data-type', 'video');
dlButton.title = `TA download video ${videoId}`;
Object.assign(dlButton.style, {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#00202f',
color: '#fff',
fontSize: '1.4rem',
textDecoration: 'none',
borderRadius: '8px',
cursor: 'pointer',
height: 'fit-content',
opacity: 0,
});
let dlIcon = document.createElement('span');
dlIcon.innerHTML = downloadIcon;
Object.assign(dlIcon.style, {
filter: 'invert()',
width: '18px',
height: '18px',
padding: '7px 8px',
});
dlButton.appendChild(dlIcon);
dlButton.addEventListener('click', e => {
e.preventDefault();
sendButton(dlButton);
e.stopPropagation();
});
return dlButton;
}
function sendButton(button) {
console.log(button);
}
function buttonError(button) { function buttonError(button) {
let buttonSpan = button.querySelector('span'); let buttonSpan = button.querySelector('span');
if (buttonSpan === null) { if (buttonSpan === null) {