Greasy Fork is available in English.

Youtube - Auto Like

Auto likes a video near the end, the goal being to keep track of watched videos since youtube history is limited

  1. // ==UserScript==
  2. // @name Youtube - Auto Like
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.9
  5. // @description Auto likes a video near the end, the goal being to keep track of watched videos since youtube history is limited
  6. // @author You
  7. // @match https://www.youtube.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13.  
  14. function l(...args){
  15. console.log('[Auto Like]', ...args)
  16. }
  17.  
  18. function getLikeButton(){
  19. return document.querySelector('like-button-view-model button')
  20. }
  21.  
  22. function isLiked(){
  23. return getLikeButton().getAttribute("aria-pressed") === 'true'
  24. }
  25.  
  26. function like(){
  27. if(!isLiked()) getLikeButton().click()
  28. }
  29.  
  30. function listen(video){
  31. l('listening', video)
  32. video.addEventListener('timeupdate', () => {
  33. if(video.currentTime/video.duration > 0.9){
  34. like()
  35. }
  36. })
  37. }
  38.  
  39. function findVideo(onVideoFound){
  40. const observer = new MutationObserver((mutations, observer) => {
  41. // Keep trying to find video
  42. let video = document.querySelector('video.video-stream')
  43. if(video){
  44. onVideoFound(video)
  45. observer.disconnect()
  46. }
  47. })
  48. observer.observe(document, {childList:true, subtree:true})
  49. }
  50.  
  51. findVideo(video => {
  52. listen(video)
  53. })