Douban2ZJUlib

Finds and logs ISBN numbers from the current webpage

  1. // ==UserScript==
  2. // @name Douban2ZJUlib
  3. // @namespace https://book.douban.com/subject/36099425/
  4. // @author AlainAllen
  5. // @description Finds and logs ISBN numbers from the current webpage
  6. // @match http://book.douban.com/subject/*
  7. // @match https://book.douban.com/subject/*
  8. // @version 0.4
  9. // @grant GM_xmlhttpRequest
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Function to find the last ISBN in the element with ID 'info'
  17. function findLastISBNInElement(elementId) {
  18. var element = document.getElementById(elementId);
  19. if (element) {
  20. var isbnPattern = /(\d{0,3}-?\d{1,5}-?\d{1,7}-?\d{1,7}-?[\dX])/g; // Pattern to match ISBN
  21. var matches = element.innerText.match(isbnPattern);
  22. if (matches && matches.length > 0) {
  23. var lastIsbn = matches[matches.length - 1];
  24. console.log('ISBN on douban:', lastIsbn);
  25. return lastIsbn;
  26. } else {
  27. console.log('ISBN not found');
  28. return null;
  29. }
  30. } else {
  31. console.log('Element with ID ' + elementId + ' not found');
  32. return null;
  33. }
  34. }
  35.  
  36. // Function to check the ISBN by forming a URL and loading it
  37. function checkISBN(isbn) {
  38. if (isbn) {
  39. var baseUrl = 'https://opac.zju.edu.cn/F/Q84DGEB5U3IFM7NN9FLYXVXPJHUL6LSU4E8AYD81KQRRJ5LNRF-32238?find_base=ZJU01&find_base=ZJU09&func=find-m&find_code=ISB&request=';
  40. var checkUrl = baseUrl + isbn.replace(/-/g, '') + '&local_base=ZJU01'; // Remove hyphens from ISBN
  41.  
  42. GM_xmlhttpRequest({
  43. method: 'GET',
  44. url: checkUrl,
  45. onload: function(response) {
  46. // Check the status of the response
  47. if (response.status === 200) {
  48. console.log("OPAC webpage loaded successfully.");
  49.  
  50. // Success checking part
  51. var isbnRegex = /ISBN(?:-1[03])?:?\s*(\d{0,3}-?\d{1,5}-?\d{1,7}-?\d{1,7}-?[\dX])/g;
  52. var allIsbnMatches = response.responseText.match(isbnRegex);
  53. if (allIsbnMatches && allIsbnMatches.length > 0) {
  54. // Get the last ISBN from the matches
  55. var lastIsbn = allIsbnMatches[allIsbnMatches.length - 1];
  56. console.log("Last ISBN found: " + lastIsbn);
  57. // Append the checked URL to the 'info' part of Douban
  58. appendLinkToInfo(checkUrl);
  59. } else {
  60. console.log("No ISBN found on the OPAC page.");
  61. }
  62. } else {
  63. console.log("Failed to load the OPAC webpage. Status code: " + response.status);
  64. }
  65. },
  66. onerror: function(error) {
  67. console.log("Error loading the OPAC webpage:", error);
  68. }
  69. });
  70. } else {
  71. console.log('No ISBN provided to check.');
  72. }
  73. }
  74.  
  75.  
  76. // Function to append the checked URL to the 'info' part of Douban
  77. function appendLinkToInfo(url) {
  78. var infoElement = document.getElementById('info');
  79. if (infoElement) {
  80. var link = document.createElement('a');
  81. var span = document.createElement('span');
  82. span.className = 'pl';
  83. span.textContent = '图书馆: ';
  84. infoElement.appendChild(span);
  85. link.href = url;
  86. link.textContent = '馆藏链接';
  87. infoElement.appendChild(link);
  88. } else {
  89. console.log('Info element not found on Douban page.');
  90. }
  91. }
  92.  
  93. // Example usage
  94. var lastIsbn = findLastISBNInElement('info');
  95. checkISBN(lastIsbn);
  96. })();