Greasy Fork

Greasy Fork is available in English.

国学大师页码链接工具 Guoxuedashi Page Linker 字典神器

为国学大师网查询到的页码添加超链接以打开在线资源或本地PDF文件对应页面;汉典增强,去广告 Generate page links for Guoxuedashi website based on page number in the table cell element; Zdic.net that comes with enhancements and ad-blocking

当前为 2023-08-20 提交的版本,查看 最新版本

// ==UserScript==
// @name         国学大师页码链接工具 Guoxuedashi Page Linker 字典神器
// @namespace    http://greasyfork.icu/zh-CN/scripts/463924
// @version      1.2.3
// @description  为国学大师网查询到的页码添加超链接以打开在线资源或本地PDF文件对应页面;汉典增强,去广告 Generate page links for Guoxuedashi website based on page number in the table cell element; Zdic.net that comes with enhancements and ad-blocking
// @author       微笑如此美妙
// @match        *.guoxuedashi.net/*
// @match        *dict.variants.moe.edu.tw/*
// @match        *humanum.arts.cuhk.edu.hk/Lexis/lexi-can/*
// @match        *www.zdic.net/*
// @match        *homeinmists.ilotus.org/hd/hydzd*
// @grant        none
// ==/UserScript==
// 本脚本需配合浏览器扩展方可点开本地文件链接,扩展下载链接:https://chrome.google.com/webstore/detail/enable-local-file-links/nikfmfgobenbhmocjaaboihbeocackld
// 扩展名称:Enable local file links;版本:v0.8.0;备用下载链接:https://crxdl.com/;文件大小为7k,下载完成后将crx文件拖动到浏览器管理扩展页(edge://extensions/)即可安装;确保管理扩展页的开发者模式打开,否则无法安装
// 汉语大字典目前可以直接访问,其他工具书PDF请重命名为拼音直接放置D盘即可调用,如字源为ziyuan.pdf、汉语大词典第1卷为hanyudacidian1.pdf,以此类推


if (window.location.href.includes(".guoxuedashi.net/")) {
    // 国语辞典、异体字典与粤语字库等链接的放置及关键词复制
    (function() {
        'use strict';

        let keyword = document.querySelector(".info_tree h1")?.innerText;
        let res;
        if (window.location.href.startsWith("http://www.guoxuedashi.net/zidian/") && !window.location.href.startsWith("http://www.guoxuedashi.net/zidian/_")) {
            res = document.querySelector(".tit03");
        } else if (window.location.href.startsWith("http://www.guoxuedashi.net/zidian/_") || window.location.href.startsWith("http://www.guoxuedashi.net/hydcd/") || window.location.href.startsWith("http://www.guoxuedashi.net/chengyu/")) {
            res = document.querySelector(".info_tree");
        } else if (window.location.href.includes("m.guoxuedashi.net/")) {
            res = document.querySelector(".panel-heading.m-path");
            keyword = document.querySelector(".breadcrumb li.active").innerText;
        }
        let link1 = `https://dict.revised.moe.edu.tw/search.jsp?md=1&word=${keyword}`;
        let link2 = `https://dict.variants.moe.edu.tw/variants/rbt/query_by_standard.rbt?rbtType=AJAX_INVOKE&componentId=rci_35808733_4&searchedText=${keyword}`;
        let link3 = `https://humanum.arts.cuhk.edu.hk/Lexis/lexi-can/`;
        let link4 = `http://www.czyzd.com/search?keyword=${keyword}`;
        let link5 = `https://www.syndict.com/w2p.php?word=${keyword}&item=hak`;
        let link6 = `https://www.zdic.net/hans/${keyword}`;
        // res.innerHTML += `   <a href="${link1}" target="_blank">📙国语辞典</a> ∣<a href="${link2}" target="_blank">📕异体字典</a> ∣<a href="${link3}" target="_blank">📒粤语字库</a> ∣<a href="${link4}" target="_blank">📘潮州字典</a> ∣<a href="${link5}" target="_blank">📓客典</a> ∣<a href="${link6}" target="_blank">📚汉典</a>`;
        if (window.location.href.includes("m.guoxuedashi.net/")) {
            res.innerHTML += `
                <div class="links" style="background-color: #f8f8f8; padding: 12px; text-align: center;">
                    <div style="display: inline-block; width: 18%;">
                        <a href="${link1}" target="_blank">国语辞典</a>
                    </div>
                    <div style="display: inline-block; width: 18%;">
                        <a href="${link2}" target="_blank">异体字典</a>
                    </div>
                    <div style="display: inline-block; width: 18%;">
                        <a href="${link3}" target="_blank">粤语字库</a>
                    </div>
                    <div style="display: inline-block; width: 18%;">
                        <a href="${link4}" target="_blank">潮州字典</a>
                    </div>
                    <div style="display: inline-block; width: 10%;">
                        <a href="${link5}" target="_blank">客典</a>
                    </div>
                    <div style="display: inline-block; width: 10%;">
                        <a href="${link6}" target="_blank">汉典</a>
                    </div>
                </div>
            `;
        } else {
            res.innerHTML += `  ➟ <div class="links" style="display: inline-block;"><a href="${link1}" target="_blank">国语辞典</a> ∣ <a href="${link2}" target="_blank">异体字典</a> ∣ <a href="${link3}" target="_blank">粤语字库</a> ∣ <a href="${link4}" target="_blank">潮州字典</a> ∣ <a href="${link5}" target="_blank">客典</a> ∣ <a href="${link6}" target="_blank">汉典</a></div>`;
        }
        res.querySelectorAll(".links a")[2].addEventListener("click", function() {
            // 关键词复制
            let textArea = document.createElement("textarea");
            textArea.value = keyword;
            document.body.appendChild(textArea);
            textArea.select();
            document.execCommand("copy");
            console.log(document.execCommand("copy"));
            document.body.removeChild(textArea);
        });
    })();


    //《汉语大字典》《中华字海》《字源》《汉语大词典》
    (function() {
        'use strict';

        const tds = document.querySelectorAll("#shupage td:first-child");
        for (const td of tds) {
            let content = td.innerText.trim();
            if (content === "汉语大字典") {
                let pageNumber = parseInt(td.nextElementSibling.textContent.match(/\d+/)[0]) + 0;
                let link = `https://homeinmists.ilotus.org/hd/hydzd3.php?st=term&kw=${pageNumber}`;
                td.nextElementSibling.innerHTML = `<a href="${link}" target="_blank">${td.nextElementSibling.innerHTML}</a>`;
            } else if (content === "中华字海") {
                let pageNumber = parseInt(td.nextElementSibling.textContent.match(/\d+/)[0]) + 84;
                let link = `file:///D:/zhonghuazihai.pdf#page=${pageNumber}`;
                td.nextElementSibling.innerHTML = `<a href="${link}" target="_blank">${td.nextElementSibling.innerHTML}</a>`;
            } else if (content === "字 源") {
                let matchedNumbers = td.nextElementSibling.textContent.match(/\d+/g);
                if (matchedNumbers && matchedNumbers.length >= 1) {
                    for (let i = 0; i < matchedNumbers.length; i++) {
                        let pageNumber = parseInt(matchedNumbers[i]);
                        if (pageNumber <= 467) {
                            pageNumber += 12;
                        } else if (pageNumber <= 870) {
                            pageNumber += 13;
                        } else if (pageNumber > 870) {
                            pageNumber += 15;
                        }
                        matchedNumbers[i] = pageNumber;
                    }
                }
                let originTextArray = td.nextElementSibling.innerHTML.split(/[\u3000\s]+/); // 按全角空格分隔源文本
                let link0 = `file:///D:/ziyuan.pdf#page=${matchedNumbers[0]}`;
                let link1 = `file:///D:/ziyuan.pdf#page=${matchedNumbers[1]}`;
                td.nextElementSibling.innerHTML = `<a href="${link0}" target="_blank">${originTextArray[0]}</a> <a href="${link1}" target="_blank">${originTextArray[1]}</a>`;
            } else if (content === "汉语大词典") {
                const pageOffsets = { // 各卷页码偏移表
                    "第1卷": 29,
                    "第2卷": -1744 + 29,
                    "第3卷": -3406 + 29,
                    "第4卷": -5068 + 29,
                    "第5卷": -6455 + 29,
                    "第6卷": -7983 + 29,
                    "第7卷": -9602 + 29,
                    "第8卷": -11156 + 29,
                    "第9卷": -12517 + 29,
                    "第10卷": -13967 + 29,
                    "第11卷": -15359 + 29,
                    "第12卷": -16801 + 29
                };
                const volume = td.nextElementSibling.nextElementSibling.innerText.match(/第(\d+)卷/)[0];
                const volumeNumber = volume.match(/\d+/)[0];
                const pageNumber = parseInt(td.nextElementSibling.textContent.match(/\d+/)[0]) + pageOffsets[volume];
                const link = `file:///D:/hanyudacidian${volumeNumber}.pdf#page=${pageNumber}`;
                td.nextElementSibling.innerHTML = `<a href="${link}" target="_blank">${td.nextElementSibling.innerHTML}</a>`;
            }
        }
    })();


    //《王力古汉语字典》异步加载故每隔1秒(1000毫秒)自动执行直至加载完成并完成修改
    (function() {
        'use strict';

        let hasModified = false; // 添加一个标记,标记是否已经执行过修改操作
        const intervalId = setInterval(function() {
            const tds = document.querySelectorAll("#shupage td:first-child");
            for (const td of tds) {
                let content = td.innerText.trim();
                if (content === "王力古汉语字典" && !hasModified) { // 添加判断语句判断是否已经修改过
                    let pageNumber = parseInt(td.nextElementSibling.textContent.match(/\d+/)[0]) + 113;
                    let link = `file:///D:/wangliguhanyuzidian.pdf#page=${pageNumber}`;
                    td.nextElementSibling.innerHTML = `<a href="${link}" target="_blank">${td.nextElementSibling.innerHTML}</a>`;
                    hasModified = true; // 修改标记为已执行过修改操作
                    clearInterval(intervalId); // 停止定时器
                }
            }
        }, 1000);
    })();
}


// 异体字字典自动搜索
if (window.location.href.startsWith("https://dict.variants.moe.edu.tw/")) {
    (function() {
        'use strict';

        if (window.location.href.startsWith("https://dict.variants.moe.edu.tw/variants/rbt/query_by_standard.rbt?rbtType=AJAX_INVOKE&componentId=rci_35808733_4&searchedText=")) {
            window.location.href = `https://dict.variants.moe.edu.tw/variants/rbt/query_by_standard_tiles.rbt?command=backstep`;
        }
        if (window.location.href.startsWith("https://dict.variants.moe.edu.tw/variants/rbt/query_by_standard_tiles.rbt?command=backstep")) {
            setTimeout (function() {
                var searchBtn = document.querySelector("img[src='images/btn/btn_standard-search.gif']"); // 获取搜索按钮
                searchBtn.click();
            }, 0);
            setTimeout (function() {
                var wordList = document.querySelector("a[id]"); // 获取搜索结果
                wordList.click();
            }, 2000);
        }
    })();
}


// 粤语审音配词字库自动粘贴搜索
if (window.location.href.startsWith("https://humanum.arts.cuhk.edu.hk/Lexis/lexi-can/")) {
    window.onload = function() {
        'use strict';

        // 获取搜索框和搜索按钮的元素
        var searchInput,searchBtn;
        searchInput = document.querySelector("body > form:nth-child(1) > input[type=text]:nth-child(5)");
        searchBtn = document.querySelector("body > form:nth-child(1) > input[type=submit]:nth-child(6)");
        if (searchInput) {
            searchInput.focus();
            navigator.clipboard.readText().then(value => {
                searchInput.value=value;
                if (searchBtn) {
                    searchBtn.click();
                }
            });
        }
    };
}


// 汉典
if (window.location.href.startsWith("https://www.zdic.net/")) {
    (function() {
        'use strict';

        // 去广告
        const intervalLength = 100; // 间隔时长
        const totalTime = 5000; // 总时长5秒
        const intervalId = setInterval(function() { // 间隔定时器
            let elToRemove = [
                document.querySelector("body > main > div.res_c_left.res_s.res_t > div.gc_lslot_a.sticky.res_h.res_s"), // 左(小视图)
                document.querySelector("body > main > div.res_c_left.res_s.res_t > div.gc_lslot_b.sticky.res_d.res_t.res_s"), // 左(大视图)
                document.querySelector("body > main > div.zdict > div.topslot_container"), // 上
                document.querySelector("#gg_rslot"), // 右
                document.querySelector("#gg_rslot2"), // 右下
                document.getElementsByClassName("adsbygoogle adsbygoogle-noablate")[1], // 下
                document.querySelector("#xxjs > div.content.definitions.xnr > div.mpuslot_b-container"), // 详细解释
                document.querySelector("#gg_cslot_1"), // 说文解字
                document.querySelector("#gg_cslot_2"), // 末尾
                document.querySelector("#gg_lslot_a"), // ts页左
                document.querySelector("body > main > div.browse_wrapper > div.topslot_container"), // ts页上
                document.querySelector("#gg_bslot_a"), // ts页下
                // document.querySelector("#gg_bslot_b"), // ts页方形广告,若去除留空则不美观
                document.querySelector("#ad_topslot"), // aboutus页上
                document.querySelector("#ad_leftslot_a"), // aboutus页左
                document.querySelector("#ad_btmslot_a") // aboutus末尾
            ];
            elToRemove.forEach(function(el) {
                if (el) {
                    el.remove(); // 去除广告
                }
            });
        }, intervalLength);
        setTimeout(function() { // 延时定时器
            clearInterval(intervalId);
        }, totalTime);
        // 定义keyword和各个链接
        var keyword = document.querySelector("div.nr-box-header > h2 > span.orth").textContent.trim();
        const noGuoyuDict = (!document.querySelector("a[class='tab'][data-type-block-tab='國語辭典']") && !document.querySelector("a[class='tab'][data-type-block-tab='國語詞典']"));
        const fanti = [...document.querySelectorAll("span + a")].find(a => a.previousElementSibling?.textContent === "繁体")?.textContent;
        if (noGuoyuDict && fanti) { keyword = fanti; } // 若无国语辞典又有繁体,则繁体为关键词
        let link0 = `http://www.guoxuedashi.net/zidian/so.php?sokeyzi=${keyword}&submit=&kz=1`;
        let link1 = `https://dict.revised.moe.edu.tw/search.jsp?md=1&word=${keyword}`;
        let link2 = `https://dict.variants.moe.edu.tw/variants/rbt/query_by_standard.rbt?rbtType=AJAX_INVOKE&componentId=rci_35808733_4&searchedText=${keyword}`;
        let link3 = `https://humanum.arts.cuhk.edu.hk/Lexis/lexi-can/`;
        let link4 = `http://www.czyzd.com/search?keyword=${keyword}`;
        let link5 = `https://www.syndict.com/w2p.php?word=${keyword}&item=hak`;
        // 选择导航栏,创建并添加多链接元素
        const tabsNavigation = document.querySelector("body > main > div.zdict > div.res_c_center > div > div.navigation > div");
        const links = document.createElement("links"); // 创建多链接的元素
        links.innerHTML = `<a class="tab" href="${link0}" target="_blank">📚国学大师</a>` +
           (noGuoyuDict ? `<a class="tab" href="${link1}" target="_blank">📙国语辞典</a>` : "") +
                          `<a class="tab" href="${link2}" target="_blank">📕异体字典</a>
                           <a class="tab" href="${link3}" target="_blank">📒粤语字库</a>
                           <a class="tab" href="${link4}" target="_blank">📘潮州字典</a>
                           <a class="tab" href="${link5}" target="_blank">📓客典  </a>`; // 条件 ? 返回值1 : 返回值2
        tabsNavigation.appendChild(links); // 添加创建好的元素
        // 关键词复制
        const yueTab = noGuoyuDict ?
              document.querySelector("links > a:nth-child(4)") :
        document.querySelector("links > a:nth-child(3)");
        yueTab.addEventListener("click", function() {
            let textArea = document.createElement("textarea");
            textArea.value = keyword;
            document.body.appendChild(textArea);
            textArea.select();
            document.execCommand("copy");
            console.log(document.execCommand("copy"));
            document.body.removeChild(textArea);
        });
        // 国语辞典优化
        const header = document.querySelector("#gyjs > div.nr-box-header"); // 修改背景色和字体色
        header.style.backgroundColor = "rgb(148, 63, 22)";
        header.style.color = "rgb(250, 250, 250)";
        const entry = document.querySelector("#gyjs > div.nr-box-header > h2"); // 修改对齐方式
        entry.style.display = "flex";
        entry.style.alignItems = "flex-start";
        const dictname = document.querySelector("#gyjs > div.nr-box-header > h2 > span.span.dictname"); // 去除dictname
        dictname.remove();
        const img = ` <a href="${link1}" target="_blank" style="border-bottom: none;">
                           <img src="https://dict.revised.moe.edu.tw/img/logo1.png" style="width: 300px; max-width: 100%;" alt="國語辭典">
                       </a>`; // 创建并添加logo
        entry.innerHTML += img;
    })();
}


// 汉语大字典按钮校正
if (window.location.href.startsWith("https://homeinmists.ilotus.org/hd/hydzd")) {
    (function() {
        'use strict';

        const mappings = [
            { selector: ".fa.fa-caret-left", symbol: "◀" },
            { selector: ".fa.fa-caret-right", symbol: "▶" },
            { selector: ".fa.fa-step-backward", symbol: "|◀" },
            { selector: ".fa.fa-step-forward", symbol: "▶|" },
            { selector: ".fas.fa-chevron-up", symbol: "▴", opacity: "0.68" }
        ];
        mappings.forEach(({ selector, symbol, opacity }) => {
            document.querySelectorAll(selector).forEach((element) => {
                if (opacity) {
                    element.parentNode.style.opacity = opacity;
                }
                element.parentNode.innerHTML = symbol;
            });
        });
    })();
}