Nhentai Favorites Scraper

Scrapes the magic numbers out of your Nhentai favorites list, returning the list as a text file. My first introduction to JavaScript, so don't expect perfection.

< Feedback on Nhentai Favorites Scraper

Review: Bad - script does not work

§
Posted: 2024-02-26

Make some changes (ugly code but it works). Separator of csv is '$'

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

//this is the one value you may want to tinker around with, Nhentai will complain if it is too low, but it will be slow if it is too high\
//if your internet is not NASA level fast, 0 should work just fine, this is mostly as a 'just in case' so you dont have to mod the code too much
var sleep_interval = 0;


function download(filename, text, columns) {
  var csvContent = '';
  if (columns) {
    csvContent += columns.join('$') + '\n';
  }
  csvContent += text;
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(csvContent));
  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){
    return Array.from(s).join('\n');
}

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

function check_state(){
    var state = localStorage.getItem('parser-state');
    if(state == null){
        state = '0';
        localStorage.setItem('parser-state',state);
    }
    return state;
}

function activate_and_start(){
    localStorage.removeItem('parser-data');
    localStorage.setItem('parser-state','1');
    location.href = '/favorites/';
}

function disable_parser(){
    localStorage.setItem('parser-state','0');
}

function start(sleepfor){
    var button = document.createElement('button');
    button.innerHTML = 'save list';
    button.onclick = activate_and_start;
    document.getElementsByClassName('menu right')[0].children[0].children[0].replaceWith(button); //injects the parser enable button into the page in place of the favorites button
    if(check_state() == '0'){
        return;
    }
    button.innerHTML = 'cancel';
    button.onclick = disable_parser;
    var s = getLocalStorageSet();
    for(var fav of document.getElementsByClassName('gallery-favorite')){
        var dataId = fav.dataset.id;
        var caption = fav.querySelector('.caption').textContent.trim().replace(/\s+/g, ' ');
        var firstBracket = caption.indexOf('[');
        var secondBracket = caption.indexOf(']');
        var tag = '';
        var index = caption.indexOf('[');
        if (index != -1) {
            tag = caption.substring(index + 1, caption.indexOf(']', index));
        } else {
            tag = 'None';
        }
        s.add(dataId + '$' + tag + '$' + caption);
    }
    localStorage.setItem('parser-data',genSetString(s));
    sleep(sleepfor).then(() => {
        try{
            document.getElementsByClassName('next')[0].click();
        }
        catch(e){
            disable_parser();
            button.innerHTML = 'DONE';
            var dataString = localStorage.getItem('parser-data').trim(); // remove trailing line breaks
            var columns = ['id', 'author', 'fullname'];
            download('list.csv', dataString, columns);
        }
    });
}

start(sleep_interval);

Post reply

Sign in to post a reply.