Douban Book Redirect

Redirect to the first book when searching by ISBN on Douban

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);

})();