V2EX favorates collection extractor

lazy livid did not implement https://www.v2ex.com/t/95800 ; please read https://www.v2ex.com/t/632448 for instruction

  1. // ==UserScript==
  2. // @name V2EX favorates collection extractor
  3. // @namespace https://github.com/cxumol/userscripts
  4. // @version 0.1
  5. // @description lazy livid did not implement https://www.v2ex.com/t/95800 ; please read https://www.v2ex.com/t/632448 for instruction
  6. // @author cxumol
  7. // @match https://www.v2ex.com/my/topics*
  8. // @grant GM_xmlhttpRequest
  9. // @connect https://www.v2ex.com/my/topics*
  10. // ==/UserScript==
  11.  
  12. // Editor: Tampermonkey v4.10 for Firefox
  13. // Formater: https://beautifier.io/
  14.  
  15. window.onload = function() {
  16. 'use strict';
  17.  
  18. // Your code here...
  19. let getAllFav = function() {
  20. let totalPageNum = Number(document.querySelector(".page_normal:last-of-type").textContent);
  21. console.log("totalPageNum: " + totalPageNum);
  22. let presentPageNum = 0;
  23. // let topicLinks = Object.values(document.getElementsByClassName("item"));
  24. let topicLinksO = new Object(); // for keeping the order of pages under async XHRs
  25. let topicLinks = new Array(); // store topics with original order
  26. let pageCount = 0;
  27.  
  28. if (totalPageNum >= 1) {
  29. while (presentPageNum < totalPageNum) {
  30. presentPageNum++;
  31. // ref https://www.tampermonkey.net/documentation.php?ext=dhdg#GM_xmlhttpRequest
  32. GM_xmlhttpRequest({
  33. method: "GET",
  34. url: "https://www.v2ex.com/my/topics?p=" + presentPageNum,
  35. // responseType: "document", // Greasy Monkey API doesn't support this
  36. onload: function(response) {
  37. let temp = document.createElement("html");
  38. temp.innerHTML = response.responseText;
  39.  
  40. let thisPresentPageNum = Number(temp.getElementsByClassName("page_current")[0].textContent);
  41. console.log("got topics from page # " + thisPresentPageNum);
  42. topicLinksO[thisPresentPageNum] = Object.values(temp.getElementsByClassName("item"));
  43. //
  44. pageCount++;
  45. if (pageCount == totalPageNum) {
  46. console.log("pageCount: " + pageCount);
  47. console.log("totalPageNum: " + totalPageNum);
  48. for (let i = 1; i <= totalPageNum; i++) {
  49. topicLinks = topicLinks.concat(topicLinksO[i]);
  50. }
  51. showItems(topicLinks);
  52. addDownloader();
  53. }
  54. }
  55. })
  56. }
  57. }
  58. // return topicLinks;
  59. }
  60.  
  61. let showItems = function(topicLinks) {
  62. let resultList = document.createElement("ol");
  63. console.log("Showing Whole Collection!");
  64. for (let i of topicLinks) {
  65. let li = document.createElement("li");
  66. li.append(i);
  67. resultList.append(li);
  68. }
  69. if (!document.getElementById("showcase")) {
  70. console.log("Rebuilt!");
  71. addShowcase();
  72. }
  73. document.getElementById("all_fav").append(resultList);
  74. }
  75. //
  76. let doFavExt = function() {
  77. getAllFav()
  78. }
  79. //
  80. let addShowcase = function() {
  81. let showcase = document.createElement('div');
  82. showcase.className = "box";
  83. showcase.id = "showcase";
  84. showcase.innerHTML = '<button id="btn_do_fav_ext">Show All Fav Topics</button><div id="all_fav"></div>';
  85. document.getElementById("Rightbar").append(showcase);
  86. document.getElementById("btn_do_fav_ext").addEventListener("click", () => doFavExt());
  87. }
  88. //
  89. let addDownloader = function() {
  90. // https://stackoverflow.com/a/18197341
  91. var downloader = document.createElement('a');
  92. downloader.textContent = "Save";
  93. downloader.className = 'page_current';
  94. let HTML2down = document.createElement("html");
  95. HTML2down.innerHTML = `<head>
  96. <meta name="Content-Type" content="text/html;charset=utf-8" />
  97. <link rel="stylesheet" type="text/css" media="screen" href="https://www.v2ex.com/css/basic.css" />
  98. <link rel="stylesheet" type="text/css" media="screen" href="https://www.v2ex.com/static/dist/combo.css" />
  99. <link rel="stylesheet" type="text/css" media="screen" href="https://www.v2ex.com/css/desktop.css" />
  100. <link rel="stylesheet" href="https://www.v2ex.com/static/css/tomorrow.css" type="text/css" />
  101. <link rel="icon" sizes="192x192" href="https://www.v2ex.com/static/img/v2ex_192.png" />
  102. <link rel="shortcut icon" href="https://www.v2ex.com/static/img/icon_rayps_64.png" type="image/png" />
  103. </head><body>` + document.getElementById("all_fav").outerHTML + `</body>`;
  104. HTML2down.append();
  105. downloader.setAttribute('href', 'data:application/octet-stream,' + encodeURIComponent(HTML2down.outerHTML));
  106. downloader.setAttribute('download', "All_my_V2EX_favorate_collections.html");
  107. document.getElementById("btn_do_fav_ext").after(downloader)
  108. }
  109.  
  110. // if (document.readyState == 'complete') {
  111. console.log("Initialize!");
  112. addShowcase();
  113. // }
  114.  
  115.  
  116. };

QingJ © 2025

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