豆瓣读书生成BibTeX引用

从豆瓣读书网页中提取元数据并生成BibTeX引用

安装此脚本?
作者推荐脚本

您可能也喜欢谷歌学术直接复制bibtex

安装此脚本
  1. // ==UserScript==
  2. // @name 豆瓣读书生成BibTeX引用
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description 从豆瓣读书网页中提取元数据并生成BibTeX引用
  6. // @author entr0pia
  7. // @match https://book.douban.com/*
  8. // @grant GM_setClipboard
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. var title = document.querySelector("#wrapper > h1 > span").textContent;
  16.  
  17. var subtitle, author1st, authors, publisher, year, pages, translators='';
  18. function catSpan(n) {
  19. var key = n.textContent.trim().split(':')[0];
  20. var ast;
  21. switch (key) {
  22. case '作者':
  23. author1st = n.querySelectorAll('a')[0].textContent;
  24. var authorsList = Array.from(n.querySelectorAll('a'))
  25. .map(a => a.textContent);
  26. authors = authorsList.join(' and ');
  27. ast = authors.split(/[,、]/);
  28. if (ast.length > 1) {
  29. author1st = ast[0]
  30. authors = ast.join(' and ');
  31. }
  32. console.log('一作:'+author1st);
  33. break;
  34. case '出版社':
  35. publisher = n.nextElementSibling.textContent;
  36. break;
  37. case '副标题':
  38. subtitle = n.nextSibling.textContent.trim();
  39. console.log('副标题:' + subtitle);
  40. break;
  41. case '出版年':
  42. year = n.nextSibling.textContent.split("-")[0].trim();
  43. break;
  44. case '页数':
  45. pages = n.nextSibling.textContent.trim();
  46. break;
  47. case '译者':
  48. translators = Array.from(n.querySelectorAll('a'))
  49. .map(a => a.textContent)
  50. .join(' and ');
  51. ast = authors.split(/[,、]/)
  52. if (ast.length > 1) {
  53. translators = ast.join(' and ');
  54. }
  55. console.log('译者:' + translators);
  56. break;
  57. }
  58. }
  59.  
  60. // 提取元数据
  61. Array.from(document.querySelectorAll('#info > span'))
  62. .map(n => catSpan(n));
  63.  
  64. var citekey=`${author1st}${year}${title}`;
  65.  
  66. if (subtitle != null){
  67. title = `${title}:${subtitle}`;
  68. }
  69.  
  70. // 格式化为BibTeX文本
  71. var bibtex = `@book{${citekey},
  72. title = {${title}},
  73. author = {${authors}},
  74. publisher = {${publisher}},
  75. year = {${year}},
  76. pages = {${pages}},
  77. translator = {${translators}}
  78. }`;
  79.  
  80.  
  81. // 添加一个按钮,点击后将BibTeX文本复制到剪贴板
  82. const bibButton = document.createElement('button');
  83. bibButton.textContent = '复制BibTeX引用';
  84. bibButton.className = 'j a_show_login lnk-sharing lnk-douban-sharing';
  85. bibButton.type = 'button';
  86. const container = document.querySelector("#content > div > div.article > div.indent > div.rec-sec");
  87. if (container != null) {
  88. container.appendChild(bibButton);
  89. }
  90.  
  91. bibButton.addEventListener('click', () => {
  92. GM_setClipboard(bibtex);
  93. alert('复制成功,请手动填写页码');
  94. });
  95. })();

QingJ © 2025

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