Douban2ZJUlib

Finds and logs ISBN numbers from the current webpage

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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.4
// @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/Q84DGEB5U3IFM7NN9FLYXVXPJHUL6LSU4E8AYD81KQRRJ5LNRF-32238?find_base=ZJU01&find_base=ZJU09&func=find-m&find_code=ISB&request=';
            var checkUrl = baseUrl + isbn.replace(/-/g, '') + '&local_base=ZJU01'; // Remove hyphens from ISBN

            GM_xmlhttpRequest({
                method: 'GET',
                url: checkUrl,
                onload: function(response) {
                    // Check the status of the response
                    if (response.status === 200) {
                        console.log("OPAC webpage loaded successfully.");

                        // 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 on the OPAC page.");
                        }
                    } else {
                        console.log("Failed to load the OPAC webpage. Status code: " + response.status);
                    }
                },
                onerror: function(error) {
                    console.log("Error loading the OPAC webpage:", error);
                }
            });
        } 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);
})();