FC2 BT Search Buttons / Mouse Forward Integration for manko.fun / solji.kim

Add inline FC2 search buttons (BTDig / BTSearch) and integrate mouse forward key behavior.

// ==UserScript==
// @name         FC2 BT Search Buttons / Mouse Forward Integration for manko.fun / solji.kim
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Add inline FC2 search buttons (BTDig / BTSearch) and integrate mouse forward key behavior. 
//               Includes customizable toggles for showing buttons and choosing which site opens via mouse forward.
// @author       VanillaMilk
// @license      MIT
// @match        https://manko.fun/*
// @match        https://solji.kim/*
// @run-at       document-end
// @grant        GM_registerMenuCommand
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(async function () {
  'use strict';

  // --- Load settings ---
  const showBTDig = await GM_getValue('showBTDig', true);
  const showBTSearch = await GM_getValue('showBTSearch', true);
  const enableMouseBTDig = await GM_getValue('enableMouseBTDig', true);
  const enableMouseBTSearch = await GM_getValue('enableMouseBTSearch', false);

  // --- Menu toggles ---
  GM_registerMenuCommand(
    (showBTDig ? '☑ ' : '☐ ') + 'Show BTDig button',
    async () => { await GM_setValue('showBTDig', !showBTDig); location.reload(); }
  );
  GM_registerMenuCommand(
    (showBTSearch ? '☑ ' : '☐ ') + 'Show BTSearch button',
    async () => { await GM_setValue('showBTSearch', !showBTSearch); location.reload(); }
  );
  GM_registerMenuCommand(
    (enableMouseBTDig ? '☑ ' : '☐ ') + 'Mouse forward opens BTDig',
    async () => {
      await GM_setValue('enableMouseBTDig', !enableMouseBTDig);
      if (!enableMouseBTDig) await GM_setValue('enableMouseBTSearch', false); // enforce exclusivity
      location.reload();
    }
  );
  GM_registerMenuCommand(
    (enableMouseBTSearch ? '☑ ' : '☐ ') + 'Mouse forward opens BTSearch',
    async () => {
      await GM_setValue('enableMouseBTSearch', !enableMouseBTSearch);
      if (!enableMouseBTSearch) await GM_setValue('enableMouseBTDig', false); // enforce exclusivity
      location.reload();
    }
  );

  let latestNum = null;

  // --- Extract FC2 number ---
  function scan() {
    const el = document.querySelector('a.text-\\[\\#ffc94d\\]');
    if (el) {
      const text = el.textContent.trim();
      const match = text.match(/FC2[- ]?PPV[- ]?(\d{5,})/i);
      if (match) {
        const num = match[1];
        latestNum = num;
        insertButtons(el, num);
        return true;
      }
    }
    return false;
  }

  // --- Insert search buttons ---
  function insertButtons(target, num) {
    if (target.__fc2_buttons__) return;
    target.__fc2_buttons__ = true;

    const box = document.createElement('span');
    box.style.display = 'inline-flex';
    box.style.alignItems = 'center';
    box.style.gap = '6px';
    box.style.marginLeft = '10px';

    const sites = [];
    if (showBTDig) sites.push({ name: 'BTDig', url: `https://en.btdig.com/search?q=${num}` });
    if (showBTSearch) sites.push({ name: 'BTSearch', url: `https://www.btsearch.love/search?keyword=${num}` });

    for (const s of sites) {
      const a = document.createElement('a');
      a.textContent = s.name;
      a.href = s.url;
      a.target = '_blank';
      a.style.padding = '3px 8px';
      a.style.border = '1px solid #ffc94d';
      a.style.borderRadius = '6px';
      a.style.background = '#353535';
      a.style.color = '#ffc94d';
      a.style.textDecoration = 'none';
      a.style.fontSize = '12px';
      a.style.fontFamily = 'sans-serif';
      a.style.cursor = 'pointer';
      a.addEventListener('mouseenter', () => {
        a.style.background = '#ffc94d';
        a.style.color = '#000';
      });
      a.addEventListener('mouseleave', () => {
        a.style.background = '#353535';
        a.style.color = '#ffc94d';
      });
      box.appendChild(a);
    }

    if (sites.length > 0) target.insertAdjacentElement('afterend', box);
  }

  // --- Mouse forward button behavior ---
  window.addEventListener('mouseup', (e) => {
    if (e.button === 4 && latestNum) {
      if (enableMouseBTSearch) {
        window.open(`https://www.btsearch.love/search?keyword=${latestNum}`, '_blank');
      } else if (enableMouseBTDig) {
        window.open(`https://en.btdig.com/search?q=${latestNum}`, '_blank');
      }
    }
  }, true);

  // --- Continuous scan until element found ---
  scan();
  const iv = setInterval(() => {
    if (scan()) clearInterval(iv);
  }, 1000);
  setTimeout(() => clearInterval(iv), 20000);
})();