起点显示优书网评分

在起点书籍页面显示优书网评分

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        起点显示优书网评分
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  在起点书籍页面显示优书网评分
// @author       chen310
// @match        https://www.qidian.com/book/*
// @icon         https://qdfepccdn.qidian.com/www.qidian.com/favicon/qd_icon.ico
// @grant        GM_xmlhttpRequest
// @connect      api.yousuu.com
// @run-at document-body
// ==/UserScript==

(function () {
    "use strict";
    const bookElement = document.getElementById("bookName");
    const bookName = document.querySelector('meta[property="og:novel:book_name"]').getAttribute("content");
    const writer = document.querySelector('meta[property="og:novel:author"]').getAttribute("content");
    const apiUrl = `https://api.yousuu.com/api/search?type=title&value=${encodeURIComponent(bookName)}`;

    GM_xmlhttpRequest({
        method: "GET",
        url: apiUrl,
        onload: function (response) {
            if (response.status === 200) {
                try {
                    const jsonData = JSON.parse(response.responseText);
                    const book = jsonData.data.books.find(b => b.title === bookName && b.author === writer);
                    if (book) {
                        const element = document.createElement("a");
                        element.target = "_blank";
                        element.style.color = "#5790df";
                        element.style.fontSize = "16px";
                        element.href = "https://www.yousuu.com/book/" + book.bookId;
                        element.textContent = " 优书网: " + book.score / 10;
                        bookElement.style.display = "inline";
                        bookElement.parentNode.insertBefore(element, bookElement.nextSibling);
                    } else {
                        console.log("找不到这本书");
                    }
                } catch (error) {
                    console.error("解析 JSON 数据时出错:", error);
                }
            } else {
                console.error("请求失败,状态码:" + response.status);
            }
        },
        onerror: function (error) {
            console.error("请求错误:", error);
        }
    });

})();