Hitomi - Search & UI Tweaks

Search Filters & UI Manipulations

Versión del día 25/01/2019. Echa un vistazo a la versión más reciente.

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Necesitará instalar una extensión como Tampermonkey para instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name         Hitomi - Search & UI Tweaks
// @namespace    brazenvoid
// @version      2.5.0
// @author       brazenvoid
// @license      GPL-3.0-only
// @description  Search Filters & UI Manipulations
// @include      https://hitomi.la/*
// @run-at       document-idle
// ==/UserScript==

// Define languages to keep and tags to exclude here

let settings = {
    allowedGallleryTypes: [
        'artist cg',
        'doujinshi',
        'game cg',
        'manga',
    ],
	allowedLanguages: [
  	    "japanese",
  	    "english"
	],
	excludedTags: [
  	    "female:loli",
  	    "sample",
  	    "male:yaoi"
	],
    removeRelatedGalleries: false,
}

// Translate gallery types to css selectors

let allowedGallerySelectors = [], allowedGalleryTypeSelector

for (let allowedGalleryType of settings.allowedGallleryTypes) {
    switch (allowedGalleryType) {
        case 'anime':
            allowedGalleryTypeSelector = 'anime'
            break
        case 'artist cg':
            allowedGalleryTypeSelector = 'acg'
            break
        case 'doujinshi':
            allowedGalleryTypeSelector = 'dj'
            break
        case 'game cg':
            allowedGalleryTypeSelector = 'cg'
            break
        case 'manga':
            allowedGalleryTypeSelector = 'manga'
            break
        default:
            continue;
    }
    allowedGallerySelectors.push(allowedGalleryTypeSelector)
}

// Formatting filters

let formatFilters = function (filters, prefix, suffix) {

  for (let index = 0; index < filters.length; index++) {

  	filters[index] = '[href="'+ prefix + encodeURIComponent(filters[index]) + suffix +'.html"]'
	}
	return filters.join(", ")
}

let languageFiltersSelector = formatFilters(settings.allowedLanguages, "/index-", "-1")
let tagFiltersSelector = formatFilters(settings.excludedTags, "/tag/", "-all-1")

// Filteration logic

let validateLanguage = function (gallery) {
  
  let validationCheck = true
  
  if (settings.allowedLanguages.length > 0) {
    
    let languageTD = gallery.querySelector('tr:nth-child(3) > td:nth-child(2)')
    if (languageTD.querySelector('a') !== null) {
      validationCheck = languageTD.querySelectorAll(languageFiltersSelector).length > 0
    }
  }
  return validationCheck
}

let validateTags = function (gallery) {
  
  let validationCheck = true
  
  if (settings.excludedTags.length > 0) {
    validationCheck = gallery.querySelectorAll(tagFiltersSelector).length === 0
  }
  return validationCheck
}

let validateType = function (gallery) {
  return allowedGallerySelectors.includes(gallery.className)
}

let complianceCallback = function(target) {
  
  let galleries = target.querySelectorAll('.anime, .manga, .dj, .acg, .cg'), removeGallery = false, languageTD, validationCheck
  
  for (let gallery of galleries) {

    validationCheck = validateType(gallery) && validateLanguage(gallery) && validateTags(gallery);
    
    if (!validationCheck) {
      gallery.remove()
    }
  }
}

// Script Run

let galleriesList = document.querySelector('.gallery-content')
let isGalleryPage = document.getElementById('dl-button') !== null;

if (isGalleryPage && settings.removeRelatedGalleries) {

    galleriesList.remove();

} else {

    let observerConfig = {
      attributes: false,
      childList: true,
      subtree: false
    }
    complianceCallback(galleriesList)

    // Adding observer to check compliance of galleries

    let observer = new MutationObserver(function(mutations, observer) {
      for(let mutation of mutations) {
        complianceCallback(mutation.target)
      }
    })
    observer.observe(galleriesList, observerConfig)
}