Hanime Resumer and Hider

Store video timespamps locally when pausing, hide videos that have a timespamp

As of 2022-11-12. See the latest version.

// ==UserScript==
// @name         Hanime Resumer and Hider
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Store video timespamps locally when pausing, hide videos that have a timespamp
// @author       You
// @match        https://player.hanime.tv/*
// @match        https://hanime.tv/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=hanime.tv
// @grant        GM.setValue
// @grant        GM.getValue
// @license MIT
// ==/UserScript==

function l(...args){
    console.log('[Resumer]', ...args)
}

function key(){
    let key = document.URL.split(',')[2]
    return key
}

if(document.URL.includes('player.hanime')){
    //Resumer
    let once = false
    //Observe changes to the DOM
    const observer = new MutationObserver(async (mutationsList, observer) => {
        if(!once){
            //Get video element
            let video = document.querySelector('video')
            if(video){
                //Resume
                let previousTime = await GM.getValue(key())
                if(previousTime){
                    video.currentTime = previousTime
                    l(`resuming ${key()} ${video.currentTime}`)
                }
                //Save currentTime
                video.onpause = () => {
                    l(`saving ${key()} ${video.currentTime}`)
                    GM.setValue(key(), video.currentTime)
                }
                once = true
            }
        }
    })
    observer.observe(document, {subtree:true, childList:true, attributes:true})
}else{
    async function hide(a){
        let name = a.href.replace('https://hanime.tv/videos/hentai/', '')
        name = name.split('?')[0] //to remove ?playlist
        let previousTime = await GM.getValue(name)
        if(previousTime){
            l(name, previousTime)
            a.style.filter = 'brightness(0.1)'
        }
    }
    //Hider
    const observer = new MutationObserver((mutationsList, observer) => {
        for(let mutation of mutationsList){
            //home page
            if(mutation.target.className == 'lazy hvc__media__cover' && mutation.removedNodes.length == 0){
                hide(mutation.target.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement)
            }else if(mutation.target.className == 'lazy relative hvc2__lazy' && mutation.removedNodes.length == 0){
                hide(mutation.target.parentElement.parentElement)
            }else if(mutation.target.id == 'related_content' && mutation.removedNodes.length == 0){ //video page
                for(let a of mutation.target.querySelectorAll('.video__item a')){
                    hide(a)
                }
                break
            }
        }
        //playlists (inefficient)
        if(document.URL.includes('/playlists/')){
            for(let a of document.querySelectorAll('.video__item a')){
                hide(a)
            }
        }

    })
    observer.observe(document, {subtree:true, childList:true})
}