Auto likes a video after watching it, the goal being to keep track of watched videos since youtube history is limited
当前为
// ==UserScript==
// @name Youtube - Auto Like
// @namespace http://tampermonkey.net/
// @version 0.7
// @description Auto likes a video after watching it, the goal being to keep track of watched videos since youtube history is limited
// @author You
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// @license MIT
// ==/UserScript==
function l(...args){
// console.log('[Auto Like]', ...args)
}
function getLikeButton(){
return document.querySelector('#segmented-like-button button')
}
function isLiked(){
return getLikeButton().getAttribute("aria-pressed") === 'true'
}
function like(){
if(!isLiked()) getLikeButton().click()
}
function listen(video){
l('listening', video)
video.addEventListener('timeupdate', () => {
l(video, video.currentTime)
if(video.currentTime/video.duration > 0.9){
like()
}
})
}
function findVideo(onVideoFound){
let videos = [] // for some reason there is more than 1 video found
const observer = new MutationObserver((mutations, observer) => {
// Keep trying to find video
let video = document.querySelector('video')
if(video){
let alreadyFound = videos.includes(video)
if(!alreadyFound){
videos.push(video)
l('videos found:', videos)
onVideoFound(video)
}
}
})
observer.observe(document, {childList:true, subtree:true})
}
findVideo(video => {
listen(video)
})