豆瓣电影图书主页一键搜索

豆瓣电影、图书主页添加资源网站的搜索链接,图书主页添加复制信息按钮,一键复制作者/译者/出版社等信息,可直接粘贴到excel

当前为 2024-09-12 提交的版本,查看 最新版本

// ==UserScript==
// @name         豆瓣电影图书主页一键搜索
// @namespace    doubanBookSearchAndInfoExport
// @description  豆瓣电影、图书主页添加资源网站的搜索链接,图书主页添加复制信息按钮,一键复制作者/译者/出版社等信息,可直接粘贴到excel
// @author       ersan23
// @version      3.2
// @supportURL   https://gf.qytechs.cn/zh-CN/scripts/507896
// @match        *://movie.douban.com/subject/*
// @match        *://book.douban.com/subject/*
// ==/UserScript==

let fetch_anchor = function (anchor) {
    return anchor[0].nextSibling.nodeValue.trim();
};


function showLinks () {

    let title = document.title.replace(/\([^\)]*\)/g, '');

    let nameText = '';

    //这段是用Kimi.ai写的,改了三天,豆瓣的作者和译者信息实在太乱了啊啊啊啊啊啊,每个上传者都有自己的写法,烦死了。谢谢Kimi~ ❤️
    function extractNames(selector) {
        let namesSet = new Set(); // 使用Set来存储唯一的名字
        let accumulatedText = ''; // 用于累积每个<a>标签的文本内容
        $(selector).each(function() {
            // 获取当前<span>元素后的所有<a>元素
            $(this).nextAll('a').each(function(index) {
                // 获取每个<a>标签的文本内容
                let text = $(this).text().trim();
                // 如果不是第一个<a>标签,前面加上斜杠
                if (index > 0) {
                    accumulatedText += '/';
                }
                accumulatedText += text; // 将文本添加到累积字符串中
            });
        });
        console.log("accumulatedText:" + accumulatedText);
        // 移除所有括号及括号内容,并按逗号(中英文)和斜杠分割名字
        let extractedNames = accumulatedText.replace(/[()\(\)\【\】\[\]“”‘’“”‘’]+.*?[)\(\)\】\]””’””’]+/g, '').split(/[,\/]+/);
        //console.log("extractedNames:" + extractedNames);
        extractedNames.forEach(function(extractedName) {
            let cleanedName = extractedName.replace(/[()\(\)\【\】\[\]“”‘’“”‘’]+.*?[)\(\)\】\]””’””’]+/g, '').trim();
            // 移除人名后面的空格和非人名内容
            cleanedName = cleanedName.replace(/(?:\s+[^ ]+)+$/, '').trim();
            // 将“•”替换为“·”
            cleanedName = cleanedName.replace(/•/g, '·');
            namesSet.add(cleanedName); // 将名字添加到Set中,自动去重
        });
        // 使用“ / ”连接所有唯一的名字
        return Array.from(namesSet).join(' / ');
    }

    // 使用extractNames函数
    let writerSelector = '#info span.pl:contains("作者")';
    let writer = extractNames(writerSelector);

    let translatorSelector = '#info span.pl:contains("译者")';
    let translator = extractNames(translatorSelector);



    let is_series = $('#info span.pl:contains("丛书")').length > 0;
    let series = is_series ? ' ' + $('#info span.pl:contains("丛书")')[0].nextSibling.nextSibling.textContent : '';
    series = series.replace(/•/g, '·').replace(/·/g, '·');

    let is_producer = $('#info span.pl:contains("出品方")').length > 0;
    let producer = is_producer ? ' ' + $('#info span.pl:contains("出品方")')[0].nextSibling.nextSibling.textContent: '';

    let is_publisher = $('#info span.pl:contains("出版社")').length > 0;
    let publisher = is_publisher ? ' ' + $('#info span.pl:contains("出版社")')[0].nextSibling.nextSibling.textContent: '';

    let is_publishYear = $('#info span.pl:contains("出版年")').length > 0;
    let publishYear = is_publishYear ? ' ' + $('#info span.pl:contains("出版年")')[0].nextSibling.textContent : '';

    let is_page = $('#info span.pl:contains("页数")').length > 0;
    let page = is_page ? ' ' + $('#info span.pl:contains("页数")')[0].nextSibling.textContent.match(/\d+/) : '';

    let is_price = $('#info span.pl:contains("定价")').length > 0;
    let price = is_price ? ' ' + $('#info span.pl:contains("定价")')[0].nextSibling.textContent.match(/[\d.]+/) : '';

    let isbn_anchor = $('#info span.pl:contains("ISBN")');
    let isbn = isbn_anchor[0] ? fetch_anchor(isbn_anchor) : '';

    let is_readDate = $('#interest_sect_level span.mr10:contains("我读过这本书")').length > 0;
    let readDate = is_readDate ? ' ' + $('#interest_sect_level span.mr10:contains("我读过这本书")').next('.color_gray').text().trim() : '';


    let QMark = '"';


    //复制的信息顺序:作者|译者|丛书|出品方|出版社|出版年|页数|定价|阅读日期|评论
    //做这个功能是为了一键复制信息粘贴到excel,可以根据自己的excel修改顺序和信息
    var emptyWord;
    var info_list = [writer, translator, series, producer, publisher, publishYear, page, price, emptyWord, emptyWord, readDate,].map(function(item) {
        return (item || '').toString().trim();
    });
    var info_export = info_list.join('\t');

    var Movie_links = [
        { html: "BiliBili", href: "https://search.bilibili.com/pgc?keyword=" + title },
        { html: "皮皮虾", href: "https://ppxzy.net/?s=" + title },
        { html: "搜片", href: "https://soupian.pro/search?key=" + title },
        { html: "中国高清网", href: "https://gaoqing.la/?s=" + title },

    ];
    var Book_read_links = [
        //{ html: "", href: "" + title + " " + writer },
        { html: "Anna", href: "https://annas-archive.li/search?q=" + title + writer + "&ext=pdf&ext=epub&ext=mobi" },
        { html: "Z-Lib", href: "https://zh.z-library.rs/s/" + title + "?extensions[]=EPUB&extensions[]=PDF"},
        //Kox.moe 漫画网站,主推中文漫画/绘本
        { html: "Kox.moe", href: "https://kox.moe/list.php?s=" + title },
        //OceanofPDF 外语pdf电子书网站
        { html: "OceanofPDF", href: "https://oceanofpdf.com/?s=" + title },
        //HathiTrust 外语电子书网站,资源较少
        { html: "HathiTrust", href: "https://catalog.hathitrust.org/Search/Home?lookfor=" + title + "&searchtype=title&ft=ft&setft=true"},
        //Gallica 法语电子书网站
        { html: "Gallica", href: "https://gallica.bnf.fr/services/engine/search/sru?operation=searchRetrieve&version=1.2&query=(gallica all " + QMark + title + "" + writer + QMark + ")"},
        { html: "顺图(书名)", href: "https://cloud.sdlib.com.cn/pages/shop/searchLoan/index?type=0&value=" + title },
        { html: "顺图(ISBN)", href: "https://cloud.sdlib.com.cn/pages/shop/searchLoan/index?type=2&value=" + isbn },
        { html: "全国新书目", href: "https://cnpub.com.cn/search.html#/quick?keyword=&checkedTypeList=%5B%22cip%22%5D&checkedClass=%5B%5D&filterbox=%5B%7B%22key%22%3A%22isbn%22,%22value%22%3A%22" + isbn + "%22,%22link%22%3A%22and%22,%22value1%22%3A%22%22,%22rule%22%3A%22like%22%7D%5D&priceStart=&priceEnd=&publishingStart=&publishingEnd=&createdateStart=&createdateEnd=&auditdateStart=&auditdateEnd=&zz=%5B%5D&ztc=%5B%5D&xglb=%5B%5D&xglbWord=%5B%5D&xglbAwardDate=%5B%5D&ca_textlangue=%5B%5D&jd_cate=%5B%5D&firstauthor=%5B%5D&ca_keyword=%5B%5D&searchType=cip&filter=%28%20%28%20isbn%20like%20%27" + isbn + "%27%20%29%20%29&type=cip,&gh=1&hs=1&pageNum=1&pageSize=10&firstbookname=&firstauthor1=&publisher1=&subjecttype1=&subjecttype2=&subjecttype1_1=&subjecttype2_1=&cate=%5B%5D&cate1=&jd_cate1=&zzz=%20&cbn=&publisher=%5B%5D&download=0&ids=&cbnStart=&cbnEnd=&noneStr=7e82a440e3fe44dfe4ac4f9ca1db226b&isbnl=&ca_isbookseries=0&stat=0&sort=&jd_cate1_2=&jd_cate1_1=&search=&jd_cate_index=%5B%5D"},

    ];
    var Book_buy_links = [
        { html: "京东", href: "https://search.jd.com/Search?keyword=" + title + publisher },
        { html: "淘宝", href: "https://s.taobao.com/search?page=1&sort=price-asc&q=图书 " + title + publisher },
        { html: "当当", href: "https://search.dangdang.com/?key=" + title + publisher + "&show=big" },
        { html: "闲鱼", href: "https://www.goofish.com/search?q=" + title + writer },
        //{ html: "拼多多", href: "https://mobile.yangkeduo.com/search_result.html?search_key=" + title + " " + writer },
        { html: "孔夫子", href: "https://search.kongfz.com/product_result/?order=100&ajaxdata=4&quality=90h&quaselect=2&key=" + title + publisher},

    ];

    var copyButton = $('<button>点击复制</button>');
    //$('body').append(copyButton);


    var link = $("<div>").append(
        $("<span>").attr("class", "pl").html("下载链接:")
    );

    switch(location.host){
        case "book.douban.com":
            appendLinks(Book_read_links, link)
            link.append('<br>')
                .append('<span class="pl">买买买:</span>')
            appendLinks(Book_buy_links, link)
            link.append('<br>')
                .append('<span class="pl">复制信息:</span>')
                .append(copyButton)
            copyInfo(copyButton, info_export)

            break;
        case "movie.douban.com":
            appendLinks(Movie_links, link)
            break;
    }

    $('#info').append(link);

    function appendLinks(items, appendTo){
        items.forEach(function(item, i){
            $("<a>")
                .html(item.html)
                .attr({
                href: item.href,
                target: "_blank"
            })
                .appendTo(appendTo);

            if(i != items.length -1){
                appendTo.append(" | ");
            }
        });
    }

    function addStyles() {
        var styleExists = document.querySelector('style.toast-style');
        if (!styleExists) {
            var style = document.createElement('style');
            style.className = 'toast-style'; // 给style标签添加一个类名以便识别
            style.type = 'text/css';
            style.innerHTML = `
            .toast {
                    position: fixed;
                    top: 50%;
                    left: 50%;
                    transform: translate(-50%, -50%);
                    background-color: #333;
                    color: #fff;
                    padding: 10px;
                    border-radius: 5px;
                    opacity: 0.9;
                    z-index: 1000; /* 确保提示显示在最上层 */
                    width: auto; /* 自动宽度 */
                    text-align: center; /* 文本居中 */
                    white-space: nowrap; /* 防止文本换行 */
                    box-shadow: 0 4px 6px rgba(0,0,0,0.1); /* 添加阴影效果 */
                }
        `;
            document.head.appendChild(style);
        }
    }

    function copyInfo(nameButton, infoCont){
        addStyles(); // 确保样式被添加
        nameButton.click(function() {
            navigator.clipboard.writeText(infoCont).then(function() {
                // 创建提示元素
                var toast = document.createElement('div');
                toast.className = 'toast';
                toast.textContent = '文本已复制到剪贴板';
                document.body.appendChild(toast);

                // 设置定时器,自动移除提示
                setTimeout(function() {
                    toast.remove();
                }, 2000);

            }).catch(function(err) {
                console.error('无法复制', err);
            });
        });
    }
}

showLinks()

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址