Douban Book Redirect

Redirect to the first book when searching by ISBN on Douban

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Douban Book Redirect
// @namespace    http://GitHub.com/awyugan
// @version      0.2
// @description  Redirect to the first book when searching by ISBN on Douban
// @author       awyugan
// @match        https://search.douban.com/book/subject_search?*
// @match        https://www.douban.com/search?cat=1001*
// @grant        none
// @license        MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to check if the query is an ISBN
    function isISBN(query) {
        // Remove all non-digit characters
        let cleaned = query.replace(/[^0-9]/g, '');
        
        // Check if it's a valid ISBN-10 or ISBN-13
        return (cleaned.length === 10 || (cleaned.length === 13 && (cleaned.startsWith('978') || cleaned.startsWith('979'))));
    }

    // Wait for the page to fully load
    window.addEventListener('load', function() {
        // Extract the search text from the URL
        let urlParams = new URLSearchParams(window.location.search);
        let searchText = urlParams.get('search_text');

        if (searchText && isISBN(searchText)) {
            // Check if we're on the search page and if there are book results
            let firstBookLink = document.querySelector('.title-text'); // for the first URL
            if (!firstBookLink) {
                firstBookLink = document.querySelector('.result .title a'); // for the second URL
            }

            if (firstBookLink) {
                // Redirect to the first book's page
                window.location.href = firstBookLink.href;
            }
        }
    }, false);

})();