trigger button clear when url changes

This commit is contained in:
Simon 2023-08-24 18:52:39 +07:00
parent f0ec9e23a7
commit 10ecdaee23
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
1 changed files with 17 additions and 0 deletions

View File

@ -283,11 +283,13 @@ function processTitle(titleContainer) {
titleContainer.classList.add('title-container');
titleContainer.addEventListener('mouseover', () => {
const taButton = titleContainer.querySelector('.ta-button');
if (!taButton) return;
taButton.style.opacity = 1;
});
titleContainer.addEventListener('mouseout', () => {
const taButton = titleContainer.querySelector('.ta-button');
if (!taButton) return;
taButton.style.opacity = 0;
});
}
@ -300,6 +302,7 @@ function buildVideoButton(titleContainer) {
let params = new URLSearchParams(href);
let videoId = params.get('/watch?v');
if (!videoId) return;
dlButton.setAttribute('data-id', videoId);
dlButton.setAttribute('data-type', 'video');
@ -402,6 +405,15 @@ function sendUrl(url, action, button) {
sending.then(handleResponse, handleError);
}
function cleanButtons() {
console.log('trigger clean buttons');
document.querySelectorAll('.ta-button').forEach(button => {
button.parentElement.hasTA = false;
button.remove();
});
}
let oldHref = document.location.href;
let throttleBlock;
const throttle = (callback, time) => {
if (throttleBlock) return;
@ -413,6 +425,11 @@ const throttle = (callback, time) => {
};
let observer = new MutationObserver(list => {
const currentHref = document.location.href;
if (currentHref !== oldHref) {
cleanButtons();
oldHref = currentHref;
}
if (list.some(i => i.type === 'childList' && i.addedNodes.length > 0)) {
throttle(ensureTALinks, 700);
}