Mobile01 (2019/08) - Load all pages

Load next page content until the end.

当前为 2019-08-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Mobile01 (2019/08) - Load all pages
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Load next page content until the end.
  6. // @author nio127
  7. // @match https://www.mobile01.com/topicdetail.php*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. //'use strict';
  13.  
  14.  
  15.  
  16. var NEXT_PAGE_TYPE =
  17. {
  18. "FROM_ACTIVE" : 1,
  19. "FROM_NEXT" : 2
  20. };
  21. var dataset = {
  22. "mobile01": {
  23. "site" : "mobile01",
  24. "url_match" : "www.mobile01.com/*",
  25. "content_selector" : ".l-content__main>.u-gapNextV--lg",
  26. "content_index" : 2,
  27. "next_page_type" : NEXT_PAGE_TYPE.FROM_ACTIVE,
  28. "next_page_selector" : ".c-pagination--next",
  29. "active_page_selector" : ".l-pagination__page.is-active",
  30. "append_to" : ".l-content__main",
  31. "hide_selectors" : [
  32. ".u-gapNextV--lg>.l-navigation",
  33. ".u-gapNextV--lg>.l-heading"
  34. ],
  35. "sep_html" : "<hr class='page_separator' /><p class='page_info'>page: <a class='page_num'></a></p>"
  36. }
  37. };
  38. //var content = document.querySelector('.goods_content');
  39. //var page_count = content.querySelectorAll('p.news-list-btn a').length;
  40. //var callback_count = page_count - 1;
  41. var loop_count = 1;
  42.  
  43. var show_message = function(count)
  44. {
  45. var box = document.createElement("div");
  46. var html = [];
  47.  
  48. html.push('<span style="display:block;margin:0px auto; opacity:1; vertical-align:middle; text-align:center; color:#fff; ">');
  49. html.push(count + " pages loaded...");
  50. html.push('</span>');
  51. box.innerHTML = html.join("");
  52. box.style = "background:#333; opacity:0.4; width:100%; position:fixed; top:0px; padding:5px; ";
  53.  
  54. document.body.appendChild(box);
  55.  
  56. setTimeout(function deleteMe(){
  57. document.body.removeChild(box);
  58. }, 3000);
  59. };
  60.  
  61. var load_next_page = function (rule, content)//, loop_count)
  62. {
  63. // var link_count = content.querySelectorAll('#goods-list-pages a').length;
  64. // var url = content.querySelectorAll('#goods-list-pages a')[(link_count-1)].href;
  65. if (content == undefined)
  66. content = document;
  67.  
  68. var get_next_url = function(node) {
  69. var next_link;
  70. if (rule["next_page_type"] == NEXT_PAGE_TYPE.FROM_NEXT)
  71. {
  72. next_link = node.querySelector(rule["next_page_selector"]);
  73. }
  74. else if (rule["next_page_type"] == NEXT_PAGE_TYPE.FROM_ACTIVE)
  75. {
  76. next_link = node.querySelector(rule["active_page_selector"]);
  77. next_link = next_link.nextElementSibling;
  78. console.log('next_link', next_link);
  79. if (next_link && next_link.href == undefined)
  80. {
  81. next_link = next_link.querySelector("a");
  82. }
  83. }
  84. var next_url = "";
  85. if (next_link != undefined)
  86. {
  87. next_url = next_link.href;
  88. console.log("next_url = " + next_url);
  89. }
  90. else
  91. {
  92. console.log("cannot find next page link!");
  93. }
  94. return next_url;
  95. };
  96.  
  97.  
  98. var xhr = new XMLHttpRequest();
  99. var url = get_next_url(content);
  100. if (url == "")
  101. {
  102. //show load message
  103. show_message(loop_count);
  104.  
  105. //exit
  106. return;
  107. }
  108. xhr.open('GET', url);
  109. xhr.onload = function() {
  110. if (xhr.status === 200) {
  111. //loop_count--;
  112. var data = xhr;
  113. tmp = document.createElement('div');
  114. tmp.innerHTML = data.responseText;
  115.  
  116. var next_contents = tmp.querySelectorAll(rule["content_selector"]);
  117. var next_content;
  118. if (rule["content_index"] != -1)
  119. {
  120. next_content = next_contents[rule["content_index"]];
  121. }
  122. else
  123. {
  124. next_content = next_contents[0];
  125. }
  126.  
  127. //hide elements
  128. if (rule["hide_selectors"].length)
  129. {
  130. var hide_length = rule["hide_selectors"].length;
  131. for (i=0; i<hide_length; i++)
  132. {
  133. var items = next_content.querySelectorAll(rule["hide_selectors"]);
  134. items.forEach(function(item) {
  135. item.style.display = "none";
  136. });
  137. }
  138. }
  139.  
  140. //content.append(next_content);
  141. var append_to = document.querySelector(rule["append_to"]);
  142.  
  143. //before next page content, insert separate html
  144. var sep_div = document.createElement("div");
  145. sep_div.innerHTML = rule["sep_html"];
  146. sep_div.id = "next_page_" + ++loop_count;
  147. sep_div.querySelector("a").href = url;
  148. sep_div.querySelector("a").innerText = loop_count;
  149. append_to.append(sep_div);
  150.  
  151. //append next page content
  152. append_to.append(next_content);
  153.  
  154. //check if next page exists
  155. load_next_page(rule, next_content);
  156.  
  157. delete tmp;
  158. }
  159. else {
  160. alert('Request failed. Returned status of ' + xhr.status);
  161. }
  162. };
  163. xhr.send();
  164. };
  165.  
  166. load_next_page(dataset["mobile01"]);
  167.  
  168.  
  169.  
  170.  
  171.  
  172. })();