EH – SearchNav Click UI

Make SearchNav more mouse-friendly; also easier method to search by date

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

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

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         EH – SearchNav Click UI
// @namespace    fabulous.cupcake.jp.net
// @version      2022.11.05.1
// @description  Make SearchNav more mouse-friendly; also easier method to search by date
// @author       FabulousCupcake (the one who made it), JoGaTo (the uploader)
// @license      MIT
// @runat        document-start
// @include      /https?:\/\/(e-|ex)hentai\.org\/.*/
// @grant        unsafeWindow
// ==/UserScript==

const stylesheet = `
#ujumpbox {
  position: relative;
}
#ujumpbox .preset-inputs {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 0.5em;
  position: absolute;
  top: 30px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 99999;
}
#ujumpbox .row {
  display: flex;
  flex-direction: row;
  justify-content: center;
}
#ujumpbox .preset-inputs input {
  width: 33px;
}
/* Date Input Width Fix */
.jumpbox { width: auto !important; }
`;

const hasSearchNav = () => {
  return !!document.querySelector(".searchnav");
}

const addStylesheet = () => {
  const stylesheetEl = document.createElement("style");
  stylesheetEl.innerHTML = stylesheet;
  document.body.appendChild(stylesheetEl);
}

const addElements = () => {
  const hook = document.querySelector("#ujumpbox");
  const el = `
<div class="preset-inputs stuffbox">
  <div class="row" style="margin-bottom: 0.25em;">
    <button class="date-input">Use Date Input</button>
  </div>
  <div class="row">
    <input type="button" value="1d"/>
    <input type="button" value="3d"/>
    <input type="button" value="1w"/>
    <input type="button" value="2w"/>
  </div>
  <div class="row">
    <input type="button" value="1m"/>
    <input type="button" value="6m"/>
    <input type="button" value="1y"/>
    <input type="button" value="2y"/>
  </div>
</div>
`;
  
  hook.insertAdjacentHTML("beforeend", el);
}

const addEventListeners = () => {
  // Preset Button Events
  const presetInputButtons = document.querySelectorAll("#ujumpbox .preset-inputs input");
  const handler = e => {
    const mainInput = document.querySelector("#ujump");
    const value = e.target.getAttribute("value");

    mainInput.value = value;
    mainInput.dispatchEvent(new Event('change'));
  }

  Array.from(presetInputButtons).forEach(el => el.addEventListener("click", handler));

  // Date Input Event
  const dateButton = document.querySelector("#ujumpbox .date-input");
  dateButton.addEventListener("click", () => {
    const mainInput = document.querySelector("#ujump");
    mainInput.setAttribute("type", "date");

    // Disable preset inputs
    Array.from(presetInputButtons).forEach(el => el.setAttribute("disabled", ""));
  });
}

const addSearchJumpButtonCallback = callbackFn => {
  const origFn = unsafeWindow.enable_jump_mode;
  unsafeWindow.enable_jump_mode = function() {
    origFn.apply(this, arguments);
    callbackFn();
  }
}

const main = () => {
  if (!hasSearchNav()) return;
  addStylesheet();

  addSearchJumpButtonCallback(() => {
    addElements();
    addEventListeners();
  });
}

main();