Nhentai Favorites Scraper

Agressively scrapes the magic numbers out of your Nhentai favorites list, returning the list as a text file. Disable to actually be able to use your favorites. My first introduction to JavaScript, so don't expect perfection.

Version au 26/09/2020. Voir la dernière version.

// ==UserScript==
// @name         Nhentai Favorites Scraper
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Agressively scrapes the magic numbers out of your Nhentai favorites list, returning the list as a text file.  Disable to actually be able to use your favorites.  My first introduction to JavaScript, so don't expect perfection.
// @author       zzzb123
// @match        *://nhentai.net/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

function download(filename, text) {
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);

  element.style.display = 'none';
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);
}

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

function genSetString(s){
    var ret = '';
    for(var a of s){
        ret += a + ',';
    }
    return ret;
}

function getLocalStorageSet(){
    var ret = new Set();
    try{
        for (var a of localStorage.getItem('parser-data').split(',')){
            ret.add(a);
        }
    }
    catch(e){}
    return ret;
}

(function(){
    var s = getLocalStorageSet();
    for(var fav of document.getElementsByClassName('gallery-favorite')){
        s.add(fav.dataset.id);
    }
    localStorage.setItem('parser-data',genSetString(s));
    sleep(500).then(() => {
        try{
            document.getElementsByClassName('next')[0].click();
        }
        catch(e){
            download('list.txt',localStorage.getItem('parser-data'));
        }
    });
})();