Mobile01 (2019/08) - Load all pages beta

Load next page content until the end.

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