☰

Steam → F95Zone Search

Adds a dedicated F95Zone search button to Steam Store, Community Hub, and SteamDB pages

您需要先安裝使用者腳本管理器擴展,如 Tampermonkey、Greasemonkey 或 Violentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 Tampermonkey 或 Violentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 Tampermonkey或 Violentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 Tampermonkey或 Userscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Steam → F95Zone Search
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Adds a dedicated F95Zone search button to Steam Store, Community Hub, and SteamDB pages
// @author       vexxowo
// @match        https://store.steampowered.com/app/*
// @match        https://steamcommunity.com/app/*
// @match        https://steamdb.info/app/*
// @icon         https://f95zone.to/favicon.ico
// @license      MIT
// @grant        GM_xmlhttpRequest
// @connect      f95zone.to
// ==/UserScript==

(function () {
    'use strict';

    function resolveSearchUrl(title, btn) {
        const fallback = `https://f95zone.to/search/?q=${encodeURIComponent(title)}&c[title_only]=1&o=relevance`;
        btn.href = fallback;

        GM_xmlhttpRequest({
            method: 'GET',
            url: 'https://f95zone.to/',
            onload: function (r1) {
                const tokenMatch = r1.responseText.match(/data-csrf="([^"]+)"/);
                if (!tokenMatch) return;

                GM_xmlhttpRequest({
                    method: 'POST',
                    url: 'https://f95zone.to/search/search',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    data: [
                        `keywords=${encodeURIComponent(title)}`,
                        `c[title_only]=1`,
                        `order=relevance`,
                        `_xfToken=${encodeURIComponent(tokenMatch[1])}`
                    ].join('&'),
                    onload: function (r2) {
                        if (r2.finalUrl && /\/search\/\d+\//.test(r2.finalUrl)) {
                            btn.href = r2.finalUrl;
                        }
                    },
                    onerror: function () { /* keep fallback */ }
                });
            },
            onerror: function () { /* keep fallback */ }
        });
    }

    // ── Favicon loader (bypasses CSP via GM_xmlhttpRequest) ─────────────────

    function fetchFavicon(img) {
        GM_xmlhttpRequest({
            method: 'GET',
            url: 'https://f95zone.to/favicon.ico',
            responseType: 'arraybuffer',
            onload: function (r) {
                const bytes = new Uint8Array(r.response);
                let binary = '';
                bytes.forEach(b => binary += String.fromCharCode(b));
                img.src = 'data:image/x-icon;base64,' + btoa(binary);
            }
        });
    }

    // ── Steam store & community hub ──────────────────────────────────────────

    function injectSteam() {
        const targetLink = [...document.querySelectorAll('a')].find(a => {
            const text = a.textContent.trim();
            return text === 'Store Page' || text === 'Community Hub';
        });
        const titleEl = document.querySelector('#appHubAppName, .apphub_AppName, .persona_name.text_white a');

        if (!targetLink || !titleEl) return false;
        const h = targetLink.offsetHeight;
        if (h === 0) return false;
        const navBar = targetLink.parentElement;
        if (navBar.querySelector('.f95zone-btn')) return true;

        const rawTitle = titleEl.textContent.trim();
        const title = location.hostname === 'store.steampowered.com'
            ? rawTitle.replace(/\s*\[.*?\]\s*/g, '').trim()
            : rawTitle;

        const btn = document.createElement('a');
        btn.target    = '_blank';
        btn.rel       = 'noopener noreferrer';
        btn.title     = 'View on F95Zone';
        btn.className = 'f95zone-btn ' + targetLink.className;
        btn.style.cssText = `
            height: ${h}px !important;
            width: 46px !important;
            line-height: ${h}px !important;
            padding: 0 !important;
            margin-right: 4px !important;
            display: inline-block !important;
            vertical-align: top !important;
            text-align: center !important;
            box-sizing: border-box !important;
        `;

        const img = document.createElement('img');
        img.src    = 'https://f95zone.to/favicon.ico';
        img.width  = 16;
        img.height = 16;
        img.style.cssText = 'vertical-align: middle; pointer-events: none;';
        btn.appendChild(img);
        navBar.insertBefore(btn, targetLink);

        resolveSearchUrl(title, btn);
        return true;
    }

    // ── SteamDB ──────────────────────────────────────────────────────────────

    function injectSteamDB() {
        // Title is in the page heading
        const titleEl = document.querySelector('h1[itemprop="name"], .pagehead h1, #main h1');
        if (!titleEl) return false;

        // "Store" button links to store.steampowered.com — insert our button before it
        const storeBtn = [...document.querySelectorAll('a')].find(
            a => a.textContent.trim() === 'Store' && a.href.includes('store.steampowered.com')
        );
        if (!storeBtn) return false;
        if (storeBtn.offsetHeight === 0) return false;

        const container = storeBtn.parentElement;
        if (container.querySelector('.f95zone-btn')) return true;

        const title = titleEl.textContent.trim();

        const btn = document.createElement('a');
        btn.target    = '_blank';
        btn.rel       = 'noopener noreferrer';
        btn.title     = 'View on F95Zone';
        btn.className = 'f95zone-btn ' + storeBtn.className;

        // Match SteamDB's button layout: favicon + label text
        btn.style.cssText = storeBtn.style.cssText; // inherit any inline styles

        const img = document.createElement('img');
        // Use GM_xmlhttpRequest to fetch favicon as base64 — bypasses SteamDB's CSP
        img.width  = 16;
        img.height = 16;
        img.style.cssText = 'vertical-align: middle; pointer-events: none; margin-right: 4px;';
        fetchFavicon(img);
        btn.appendChild(img);

        const label = document.createElement('span');
        label.textContent = 'F95';
        btn.appendChild(label);

        container.insertBefore(btn, storeBtn);

        resolveSearchUrl(title, btn);
        return true;
    }

    // ── Dispatch ─────────────────────────────────────────────────────────────

    const injectFn = location.hostname === 'steamdb.info' ? injectSteamDB : injectSteam;

    const interval = setInterval(() => {
        if (injectFn()) clearInterval(interval);
    }, 500);
    setTimeout(() => clearInterval(interval), 10000);
})();