|  | // ==UserScript==
 | 
|  | // @name         Nyaa Nibl Search Buttons
 | 
|  | // @namespace    your-namespace
 | 
|  | // @version      1.0
 | 
|  | // @description  Adds a search button with all title variants (including cleaned #desktop-title) and buttons for each title
 | 
|  | // @match        https://kuroiru.co/*
 | 
|  | // @grant        none
 | 
|  | // @run-at       document-start
 | 
|  | // @noframes
 | 
|  | // ==/UserScript==
 | 
|  | 
 | 
|  | (function() {
 | 
|  |   'use strict';
 | 
|  | 
 | 
|  |   // --- Shared CSS (applied once globally) ---
 | 
|  |   const css = `
 | 
|  |     .nyaa-btn {
 | 
|  |       display: block;
 | 
|  |       margin-top: 4px;
 | 
|  |       padding: 4px 8px;
 | 
|  |       background: #222;
 | 
|  |       color: #aaa;
 | 
|  |       border-radius: 4px;
 | 
|  |       text-decoration: none;
 | 
|  |       cursor: pointer;
 | 
|  |       font-size: 90%;
 | 
|  |       transition: all 0.2s ease;
 | 
|  |     }
 | 
|  |     .nyaa-btn:hover {
 | 
|  |       background: #555;
 | 
|  |       color: #fff;
 | 
|  |     }
 | 
|  |     .nyaa-btn-main {
 | 
|  |       font-size: 100%;
 | 
|  |       font-weight: bold;
 | 
|  |     }
 | 
|  |   `;
 | 
|  |   const styleEl = document.createElement('style');
 | 
|  |   styleEl.textContent = css;
 | 
|  |   document.documentElement.appendChild(styleEl);
 | 
|  | 
 | 
|  |   // --- Title cleaning ---
 | 
|  |   function cleanTitle(title) {
 | 
|  |     return title
 | 
|  |       .replace(/\b(Season|S)\s*\d+\b/gi, '')
 | 
|  |       .replace(/\b(Part|Pt\.?)\s*\d+\b/gi, '')
 | 
|  |       .replace(/\b(OVA|OAD|ONA|Special|SP)\b/gi, '')
 | 
|  |       .replace(/[\[\]\(\)]/g, '')
 | 
|  |       .replace(/\s*[-\|~]\s*/g, ' ')
 | 
|  |       .replace(/\s+/g, ' ')
 | 
|  |       .replace(/^[\s,.:;!?'"-]+|[\s,.:;!?'"-]+$/g, '')
 | 
|  |       .trim();
 | 
|  |   }
 | 
|  | 
 | 
|  |   // --- Button insertion logic ---
 | 
|  |   function insertNyaaButtons() {
 | 
|  |     const titleDiv = document.querySelector('#prompt-title-alt');
 | 
|  |     const desktopTitleDiv = document.querySelector('#desktop-title');
 | 
|  |     if (!titleDiv) return;
 | 
|  | 
 | 
|  |     // Avoid duplicates
 | 
|  |     if (document.querySelector('.nyaa-btn-main')) return;
 | 
|  | 
 | 
|  |     // Gather titles
 | 
|  |     const titles = [];
 | 
|  |     if (titleDiv.textContent.trim()) {
 | 
|  |       titles.push(
 | 
|  |         ...titleDiv.textContent
 | 
|  |           .split(',')
 | 
|  |           .map(t => t.trim())
 | 
|  |           .filter(Boolean)
 | 
|  |       );
 | 
|  |     }
 | 
|  |     if (desktopTitleDiv && desktopTitleDiv.textContent.trim()) {
 | 
|  |       const cleaned = cleanTitle(desktopTitleDiv.textContent);
 | 
|  |       if (cleaned) titles.push(cleaned);
 | 
|  |     }
 | 
|  | 
 | 
|  |     // Deduplicate (case-insensitive)
 | 
|  |     const uniqueTitles = [...new Set(titles.map(t => t.toLowerCase()))]
 | 
|  |       .map(lower => titles.find(t => t.toLowerCase() === lower));
 | 
|  | 
 | 
|  |     if (uniqueTitles.length === 0) return;
 | 
|  | 
 | 
|  |     // === Main Combined Button ===
 | 
|  |     const queryAll = uniqueTitles.map(t => `"${t}"`).join('|');
 | 
|  |     const encodedAll = encodeURIComponent(queryAll);
 | 
|  |     const searchUrlAll = `https://nyaa.si/?f=0&c=1_2&q=${encodedAll}`;
 | 
|  | 
 | 
|  |     const mainBtn = document.createElement('a');
 | 
|  |     mainBtn.href = searchUrlAll;
 | 
|  |     mainBtn.target = '_blank';
 | 
|  |     mainBtn.textContent = 'Nyaa (all)';
 | 
|  |     mainBtn.className = 'nyaa-btn nyaa-btn-main';
 | 
|  |     titleDiv.insertAdjacentElement('afterend', mainBtn);
 | 
|  | 
 | 
|  |     // === Individual Buttons ===
 | 
|  |     uniqueTitles.forEach(t => {
 | 
|  |       const encodedTitle = encodeURIComponent(t);
 | 
|  |       const url = `https://nibl.co.uk/search?query=${encodedTitle}`;
 | 
|  | 
 | 
|  |       const btn = document.createElement('a');
 | 
|  |       btn.href = url;
 | 
|  |       btn.target = '_blank';
 | 
|  |       btn.textContent = `Nibl: ${t}`;
 | 
|  |       btn.className = 'nyaa-btn';
 | 
|  |       mainBtn.insertAdjacentElement('afterend', btn);
 | 
|  |     });
 | 
|  |   }
 | 
|  | 
 | 
|  |   // --- Run once + observe DOM changes ---
 | 
|  |   insertNyaaButtons();
 | 
|  |   const observer = new MutationObserver(() => insertNyaaButtons());
 | 
|  |   observer.observe(document.body, { childList: true, subtree: true });
 | 
|  | })();
 |