图书馆SS显示

查询全国图书馆参考咨询联盟中图书的SS, 并生成文件名

  1. // ==UserScript==
  2. // @name 图书馆SS显示
  3. // @namespace book.ucdrs
  4. // @version 0.4.0
  5. // @description 查询全国图书馆参考咨询联盟中图书的SS, 并生成文件名
  6. // @author wenmin92
  7. // @match *://book.ucdrs.superlib.net/search*
  8. // @match *://book.ucdrs.superlib.net/views/specific/*
  9. // @require https://code.jquery.com/jquery-3.6.1.slim.min.js
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13.  
  14. function insertBefore(parent, prefix, content, nodeType = 'dd') {
  15. const html = `<${nodeType} class="custom-field">
  16. <span style="user-select:none">${prefix}</span><span style="user-select:all">${content}</span>
  17. </${nodeType}>`
  18. $(parent).prepend(html);
  19. }
  20.  
  21. function insertAfter(parent, prefix, content, nodeType = 'dd') {
  22. const html = `<${nodeType} class="custom-field">
  23. <span style="user-select:none">${prefix}</span><span style="user-select:all">${content}</span>
  24. </${nodeType}>`
  25. $(parent).append(html);
  26. }
  27.  
  28. function makeFileName() {
  29. let name = document.querySelector('#paper_one > input[name=title]').value;
  30. if (/第.版/.test(name)) {
  31. name = name.replace(/\s*(第.版)\s*/, ' ($1)');
  32. }
  33. const content = document.querySelector('#paper_one > input[name=content]').value;
  34. let author = content.match(/【作 者】(.*)\n/)?.[1].replace(/;|,/g, ', ').replace(/((.{1,3}))/g, '[$1]').replace(/((.{3,}?))/g, '') ?? 'null';
  35. let publisher = content.match(/【出版项】(.*)\n/)?.[1].replace(/^.*?:/, '').match(/(.*?),/)[1] ?? 'null';
  36. let publishDate = content.match(/【出版项】(.*)\n/)?.[1].replace(/^.*?:/, '').match(/,([\d.]+)/)[1] ?? 'null';
  37. let pages = content.match(/【形态项】(\d+)/)?.[1] ?? 'null';
  38. return `${name}, ${author}, ${publisher}, ${publishDate}, ${pages}P`;
  39. }
  40.  
  41. function insertCleanISBN(parent) {
  42. const content = document.querySelector('#paper_one > input[name=content]').value;
  43. const cleanISBN = content.match(/【ISBN号】([-\dxX]*)\n/)[1].replace(/-/g, '');
  44. const elISBN = Array.from(parent.children).find(it => it.textContent.includes('【ISBN号】'));
  45. elISBN?.append(` (${cleanISBN})`);
  46. }
  47.  
  48. function doSpecific() {
  49. const el = document.querySelector('script[language]:not([src])');
  50. const bookInfo = document.querySelector('.tubox dl');
  51.  
  52. if (el) {
  53. insertBefore(bookInfo, '【文件名】', makeFileName());
  54. const ss = el.innerText.match(/ssn=(\d{3,})/)?.[1];
  55. let dx = ''
  56. if (ss) {
  57. insertBefore(bookInfo, '【SS】', ss);
  58. } else if(dx = location.href.match(/dxNumber=(\d+)/)?.[1]) {
  59. insertBefore(bookInfo, '【DX】', dx);
  60. }
  61. insertCleanISBN(bookInfo);
  62. }
  63. }
  64.  
  65. function doSearch() {
  66. document.querySelectorAll('td[id="b_img"]').forEach(item => {
  67. const ss = item.parentElement.querySelector('input[name*="ssid"]').value;
  68. if (ss) {
  69. insertAfter($(item).next(), 'SS:', ss);
  70. } else {
  71. const dxNumber = item
  72. .querySelector('a[href*="dxNumber"]')
  73. .getAttribute('href')
  74. .match(/dxNumber=(\d+)/)?.[1];
  75. dxNumber && insertAfter($(item).next(), 'DX:', dxNumber);
  76. }
  77. });
  78. }
  79.  
  80. function showChangeYearBtn() {
  81. const yearSection = Array.from(document.querySelectorAll('#leftcat')).find(it => it.innerHTML.includes('年代'));
  82.  
  83. // 整理容器中已有的内容
  84. yearSection.innerHTML = yearSection.innerHTML.replace(/(&nbsp;){3,}/g, '&nbsp;&nbsp;').replace('</a>&nbsp;&nbsp;<a', '</a><br>&nbsp;&nbsp;<a');
  85.  
  86. // 只有在查看特定年份时才显示按钮
  87. if (!location.href.includes('year=')) {
  88. return;
  89. }
  90.  
  91. const year = parseInt(yearSection.innerHTML.match(/\d{4}/)[0]);
  92.  
  93. // 添加按钮
  94. const buttons = document.createElement('div');
  95.  
  96. buttons.style.cssText = 'display: inline-block; margin-left: 8px;';
  97. const btnPrev = document.createElement('span');
  98. btnPrev.style.cssText = 'cursor: pointer; color: #fff; background-color: cadetblue; padding: 1px 3px; border-radius: 2px; margin: 0px 2px; font-weight: bold;';
  99. btnPrev.addEventListener('click', () => {
  100. location = location.href.replace(/year=\d{4}/, `year=${year - 1}`).replace(/&Pages=\d+/, '&Pages=1');;
  101. });
  102. btnPrev.innerHTML = '&lt;';
  103. buttons.append(btnPrev);
  104.  
  105. const btnNext = document.createElement('span');
  106. btnNext.style.cssText = 'cursor: pointer; color: #fff; background-color: cadetblue; padding: 1px 3px; border-radius: 2px; margin: 0px 2px; font-weight: bold;';
  107. btnNext.addEventListener('click', () => {
  108. location = location.href.replace(/year=\d{4}/, `year=${year + 1}`).replace(/&Pages=\d+/, '&Pages=1');
  109. });
  110. btnNext.innerHTML = '&gt;';
  111. buttons.append(btnNext);
  112.  
  113. yearSection.append(buttons)
  114. }
  115.  
  116.  
  117. if (location.href.includes('/views/specific/')) {
  118. doSpecific();
  119. } else if (location.href.includes('/search')) {
  120. doSearch();
  121. showChangeYearBtn();
  122. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址