JavDB Exporter

导出 想看、看过、清单 | Export Want, watched, list

  1. // ==UserScript==
  2. // @name JavDB Exporter
  3. // @namespace https://github.com/nini22P/monkey-script/tree/main/javdb-exporter
  4. // @version 1.3
  5. // @description 导出 想看、看过、清单 | Export Want, watched, list
  6. // @match https://javdb.com/users/want_watch_videos*
  7. // @match https://javdb.com/users/watched_videos*
  8. // @match https://javdb.com/users/list_detail*
  9. // @match https://javdb.com/lists*
  10. // @grant GM_xmlhttpRequest
  11. // @grant GM_listValues
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. let allVideosInfo = JSON.parse(localStorage.getItem('allVideosInfo')) || [];
  16. let allowExport = JSON.parse(localStorage.getItem('allowExport')) || false;
  17. let exportButton = null;
  18. let url = window.location.href;
  19.  
  20. function getVideosInfo() {
  21. const videoElements = document.querySelectorAll('.item');
  22. const videosInfo = Array.from(videoElements).map((element) => {
  23. const title = element.querySelector('.video-title').textContent.trim();
  24. const [id, ...titleWords] = title.split(' ');
  25. const formattedTitle = titleWords.join(' ');
  26. const [score, scoreNumber] = element.querySelector('.value').textContent.replace(/[^0-9-.,]/g, '').split(',');
  27. const releaseDate = element.querySelector('.meta').textContent.replace(/[^0-9-]/g, '');
  28. return {
  29. id,
  30. title: formattedTitle,
  31. score: Number(score),
  32. scoreNumber: Number(scoreNumber),
  33. releaseDate: releaseDate
  34. };
  35. });
  36. return videosInfo;
  37. }
  38.  
  39. function scrapeAllPages() {
  40. const videosInfo = getVideosInfo();
  41. allVideosInfo = allVideosInfo.concat(videosInfo);
  42. localStorage.setItem('allVideosInfo', JSON.stringify(allVideosInfo));
  43. const nextPageButton = document.querySelector('.pagination-next');
  44. if (nextPageButton) {
  45. nextPageButton.click();
  46. setTimeout(() => scrapeAllPages(), 2000);
  47. } else {
  48. exportVideosInfo();
  49. }
  50. }
  51.  
  52. function exportVideosInfo() {
  53. allowExport = false;
  54. localStorage.setItem('allowExport', JSON.stringify(allowExport));
  55. allVideosInfo.sort((a, b) => a.id.localeCompare(b.id));
  56. const json = JSON.stringify(allVideosInfo);
  57. const jsonBlob = new Blob([json], { type: 'application/json' });
  58. const jsonUrl = URL.createObjectURL(jsonBlob);
  59. const downloadLink = document.createElement('a');
  60. const dateTime = (new Date()).toISOString().replace('T', ' ').split('.')[0];
  61. let fileName = '';
  62. if (url.includes('/watched_videos')) {
  63. fileName = 'watched-videos'
  64. } else if (url.includes('/want_watch_videos')) {
  65. fileName = 'want-watch-videos'
  66. } else if (url.includes('/list_detail')) {
  67. const breadcrumb = document.getElementsByClassName('breadcrumb')[0];
  68. const li = breadcrumb.parentNode.querySelectorAll('li');
  69. fileName = li[1].innerText;
  70. } else if (url.includes('/lists')) {
  71. fileName = document.querySelector('.actor-section-name').innerText;
  72. }
  73. downloadLink.href = jsonUrl;
  74. downloadLink.download = `${fileName} ${dateTime}.json`;
  75. document.body.appendChild(downloadLink);
  76. downloadLink.click();
  77. localStorage.removeItem('allVideosInfo');
  78. exportButton.textContent = '导出完毕';
  79. }
  80.  
  81. function startExport() {
  82. const allImages = document.querySelectorAll('img'); //移除图像增加速度
  83. allImages.forEach((image) => {
  84. image.remove();
  85. });
  86. allowExport = true;
  87. localStorage.setItem('allowExport', JSON.stringify(allowExport));
  88. exportButton.textContent = '导出中...';
  89. exportButton.disabled = true;
  90. scrapeAllPages();
  91. }
  92.  
  93. function createExportButton() {
  94. exportButton = document.createElement('button');
  95. exportButton.textContent = '导出 json';
  96. exportButton.className = 'button is-small';
  97. exportButton.addEventListener('click', startExport);
  98. if (url.includes('/list_detail')) {
  99. document.querySelector('.breadcrumb').querySelector('ul').appendChild(exportButton);
  100. } else {
  101. document.querySelector('.toolbar').appendChild(exportButton);
  102. }
  103. }
  104.  
  105. if (url.includes('/watched_videos')
  106. || url.includes('/want_watch_videos')
  107. || url.includes('/list_detail')
  108. || url.includes('/lists')
  109. ) {
  110. createExportButton();
  111. if (allowExport) {
  112. startExport();
  113. }
  114. }