Douban2ZJUlib

Finds and logs ISBN numbers from the current webpage

当前为 2023-12-07 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Douban2ZJUlib
// @namespace   https://book.douban.com/subject/36099425/
// @author       AlainAllen
// @description Finds and logs ISBN numbers from the current webpage
// @match     http://book.douban.com/subject/*
// @match     https://book.douban.com/subject/*
// @version     0.2
// @grant       GM_xmlhttpRequest
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to find the last ISBN in the element with ID 'info'
    function findLastISBNInElement(elementId) {
        var element = document.getElementById(elementId);
        if (element) {
            var isbnPattern = /(\d{0,3}-?\d{1,5}-?\d{1,7}-?\d{1,7}-?[\dX])/g; // Pattern to match ISBN
            var matches = element.innerText.match(isbnPattern);
            if (matches && matches.length > 0) {
                var lastIsbn = matches[matches.length - 1];
                console.log('ISBN on douban:', lastIsbn);
                return lastIsbn;
            } else {
                console.log('ISBN not found');
                return null;
            }
        } else {
            console.log('Element with ID ' + elementId + ' not found');
            return null;
        }
    }

    // Function to check the ISBN by forming a URL and loading it
    function checkISBN(isbn) {
        if (isbn) {
            var baseUrl = 'https://opac.zju.edu.cn/F/76UDUNXYY3Q54I9H9PHNMHPCSKVN5EA4ABM8PAG46YSP8HTFCN-12474?find_base=ZJU01&find_base=ZJU09&func=find-m&find_code=ISB&request='; // Base URL
            var checkUrl = baseUrl + isbn.replace(/-/g, ''); // Remove hyphens from ISBN

            GM_xmlhttpRequest({
                method: 'GET',
                url: checkUrl,
                onload: function(response) {
                    // Success checking part
                    var isbnRegex = /ISBN(?:-1[03])?:?\s*(\d{0,3}-?\d{1,5}-?\d{1,7}-?\d{1,7}-?[\dX])/g;
                    var allIsbnMatches = response.responseText.match(isbnRegex);
                    if (allIsbnMatches && allIsbnMatches.length > 0) {
                        // Get the last ISBN from the matches
                        var lastIsbn = allIsbnMatches[allIsbnMatches.length - 1];
                        console.log("Last ISBN found: " + lastIsbn);
                        // Append the checked URL to the 'info' part of Douban
                        appendLinkToInfo(checkUrl);
                    } else {
                        console.log("No ISBN found.");
                    }
                }
            });
        } else {
            console.log('No ISBN provided to check.');
        }
    }

    // Function to append the checked URL to the 'info' part of Douban
    function appendLinkToInfo(url) {
        var infoElement = document.getElementById('info');
        if (infoElement) {
            var link = document.createElement('a');
            var span = document.createElement('span');
            span.className = 'pl';
            span.textContent = '图书馆:';
            infoElement.appendChild(span);
            link.href = url;
            link.textContent = '馆藏链接';
            infoElement.appendChild(link);
        } else {
            console.log('Info element not found on Douban page.');
        }
    }

    // Example usage
    var lastIsbn = findLastISBNInElement('info');
    checkISBN(lastIsbn);
})();