Comick Google Search

Returns google links to Comick

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Comick Google Search
// @namespace    LookAtYrOwnName
// @version      1.2
// @description  Returns google links to Comick
// @match        https://comick.dev/*
// @run-at       document-end
// ==/UserScript==

(function () {
    function cleanH1Text(text) {
    text = text.trim();
    return text.replace(/((?:chapter|ch)\.?\s*\d+(?:\.\d+)?).*/i, "$1");
    }

    function getChapterNumber() {
        const h1 = document.querySelector("h1.text-xl.font-medium.text-blue-800.dark\\:text-blue-200");
        if (!h1) return null;

        const cleaned = cleanH1Text(h1.textContent.trim());
        const m = cleaned.match(/(?:chapter|ch)\.?\s*(\d+)/i);
        return m ? m[1] : null;
    }

    function extractChapterFromText(text) {
        const m = text.match(/ch\.\s*(\d+)/i);
        return m ? m[1] : null;
    }

    function removeExistingChapter(text) {
        return text.replace(/ch\.\s*\d+/i, "").trim();
    }

    function createSearchText(text, chapter) {
        const cleaned = removeExistingChapter(text);
        return cleaned + " Chapter " + chapter;
    }

    function processElement(el, chapterFromPage) {
        if (el.dataset.gsearchApplied === "true") return;

        const original = el.textContent.trim();
        if (!original) return;

        const chapter = extractChapterFromText(original) || chapterFromPage;
        if (!chapter) return;

        const finalText = createSearchText(original, chapter);
        const query = encodeURIComponent(finalText);
        const url = "https://www.google.com/search?q=" + query;

        const a = document.createElement("a");
        a.href = url;
        a.textContent = finalText;
        a.style.color = "inherit";
        a.style.textDecoration = "underline";

        el.textContent = "";
        el.appendChild(a);

        el.dataset.gsearchApplied = "true";
    }

    function scan() {
        const chapterFromPage = getChapterNumber();
        if (!chapterFromPage) return;

        document.querySelectorAll("div[data-keyword-text]").forEach(el => {
            processElement(el, chapterFromPage);
        });
    }

    scan();

    new MutationObserver(() => scan())
        .observe(document.body, { childList: true, subtree: true });
})();