☰

NHentai custom search

A fork of Dar9586's Nhentai Only english to include a changeable custom search.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         NHentai custom search
// @namespace    mr feelgood
// @version      1.3.2
// @description  A fork of Dar9586's Nhentai Only english to include a changeable custom search.
// @author       h8code
// @license      https://opensource.org/licenses/MIT
// @match        *://nhentai.net/*
// @run-at       document-end
// @grant        none
// @license      https://opensource.org/licenses/MIT
// ==/UserScript==

'use strict';

console.log("Tampermonkey script loaded");

const search = [
    {
        val: 'language:english',
        simplified: 'english'
    },
    {
       val: '-tag:"ai generated"',
       simplified: 'ai generated'
    },
    // EXTRA SEARCH TERMS
    // {
    //    val: "tag:some_tag",
    //    simplified: "some_tag",
    // },
];

function getSearchString() {
    let s = '';
    for (let v of search) {
        s += ' ';
        s += v.val;
    }
    return s.trim();
}

function hideSearch(){
    let textBox=document.getElementsByName('q')[0];
    for (let v of search) {
        console.log({textboxValue: textBox.value});
        if(textBox.value.includes(v.val))textBox.value=textBox.value.replace(v.val,"").trim();
    }
}

function setSearch(){
    let textBox=document.getElementsByName('q')[0];
    for (let v of search) {
        if(!textBox.value.includes(v.simplified))textBox.value+=` ${v.val}`;
    }
}

function redirectToSearch(list){
    let searchStr = encodeURIComponent(getSearchString());
    for (let item of list) {
        let old = item.href
        //                                 domain           type  name
        item.href=item.href.replace(/https:\/\/nhentai.net\/(.*)\/(.*)\//,'https://nhentai.net/search/?q=$1:"$2" '+searchStr);
        let newI = item.href;
        console.log({old, new: newI});
    }
}




(function() {
    'use strict';

    function runScript() {
        console.log("Running script on:", location.href);

            //remove english tag because it's ugly
            hideSearch();
            //redirect if going to main page
            if(window.location.pathname=="/")window.location.replace("https://nhentai.net/search/?q="+encodeURIComponent(getSearchString()))
            //append english to end of query
            document.getElementsByTagName('form')[0].onsubmit=setSearch
            //replace logo location
            document.getElementsByTagName('nav')[0].childNodes[0].href='/search/?q='+encodeURIComponent(getSearchString())
            //when using tab in navbar
            if(document.getElementById("tag-container")!=null)redirectToSearch(document.getElementById("tag-container").getElementsByTagName("a"))
            //on galleries
            if(document.getElementById("tags")!=null)redirectToSearch(document.getElementById("tags").getElementsByTagName("a"))

    }

    // Run once on load
    runScript();

    // Detect changes
    new MutationObserver(() => {
        console.log("Observed");
        console.log(location.href);

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