jc_qidian_prefetch_page

Prefetch chinese novell site (起點)

  1. // ==UserScript==
  2. // @name jc_qidian_prefetch_page
  3. // @namespace http://localhost/jc/
  4. // @require https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js
  5. // @include http://*.qidian.com/*
  6. // @description Prefetch chinese novell site (起點)
  7. // @description Can use Hotkey to Next Page : right arrow == n == 0 in right area == Insert == Delete
  8. // @description 沒試過-->有儲點的用戶讀到需付款的頁面是否會自動扣款,有疑慮者建議不使用...
  9. // @grant GM_log
  10. // @modified_time 2014.09.18.22.00
  11. // @version 1.5
  12. // ==/UserScript==
  13.  
  14.  
  15. // 預抓下一頁
  16. // 關鍵 a[contains(.,'下一頁')]
  17.  
  18. (function() {
  19.  
  20. var G_prefetch_url = ''; // 預抓的網址
  21.  
  22. function search(target,sel) {
  23. return target.evaluate(
  24. sel,
  25. target,
  26. null,
  27. XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  28. null
  29. );
  30. }
  31.  
  32. function position(elmt){
  33. var x = 0;
  34. var y = 0;
  35. while(elmt != null){
  36. x += elmt.offsetLeft;
  37. y += elmt.offsetTop;
  38. elmt = elmt.offsetParent;
  39. } // while
  40. return {'x':x,'y':y};
  41. }
  42.  
  43. function scrollTo(elmt){
  44. var pos = position(elmt)
  45. //GM_log('x:'+pos.x+', y:'+pos.y)
  46. window.scrollTo(pos.x, pos.y);
  47. }
  48.  
  49. var myif;
  50. var next;
  51. var nhref;
  52.  
  53. function clickNext(event) {
  54. //var n_main = myif.contentDocument.getElementById('Main')
  55. //var mainDivId_1 = 'form1'; // for before 2015-11
  56. var mainDivId_1 = 'wrapbig'; // for after 2015-11
  57. var mainDivId = mainDivId_1;
  58. var n_main = myif.contentDocument.getElementById( mainDivId );
  59. if (!n_main) {
  60. GM_log('1. iframe n_main not found');
  61. return;
  62. }
  63. // GM_log('n_main found')
  64. var main = document.getElementById( mainDivId ); // TODO: check if 'main' fully loaded?
  65. if (!main) {
  66. GM_log('2. n_main not found');
  67. return;
  68. }
  69. // GM_log('main found')
  70. main.innerHTML = n_main.innerHTML;
  71. //main.parentNode.replaceChild(n_main,main)
  72. //GM_log(history.current)
  73. //history.current = nhref;
  74. scrollTo(main);
  75. // prefetch again
  76. prefetch();
  77. // change location URL
  78. GM_log('G_prefetch_url = ' + G_prefetch_url + ' , location.href = ' + location.href);
  79. if ((''!=G_prefetch_url) && (G_prefetch_url != location.href)) {
  80. history.pushState({url: G_prefetch_url, url2: location.href}, null, G_prefetch_url); // 改變網址但不會重新載入內容
  81. }
  82. if (event.stopPropagation) {
  83. event.stopPropagation();
  84. }
  85. if (event.cancelable && event.preventDefault) {
  86. event.preventDefault();
  87. }
  88. }
  89.  
  90. function checkKey(event) {
  91. //alert(event.keyCode)
  92. if (event.keyCode == 39) { clickNext(next); } // right arrow
  93. if (event.keyCode == 78) { clickNext(next); } // n
  94. if (event.keyCode == 96) { clickNext(next); } // 0 in right area
  95. if (event.keyCode == 45) { clickNext(next); } // Insert
  96. if (event.keyCode == 46) { clickNext(next); } // Delete
  97. }
  98.  
  99. function prefetch() {
  100. // var links = search(document,"//a[@class='Next']")
  101. //var links = search(document,"//a[contains(.,'下一章')]")
  102. var links = search(document,"//a[@id='NextLink']");
  103. if (links.snapshotLength ==0) {
  104. links = search(document,"//a[@id='pageNextBottomBtn']"); // for 2015 新版
  105. }
  106. if (links.snapshotLength ==0) {
  107. GM_log('links.snapshotLength == 0 , exit');
  108. return;
  109. }
  110. next = links.snapshotItem(0);
  111.  
  112. nhref = next.getAttribute('href');
  113. GM_log('1. myif typeof == ' + typeof myif);
  114. if (myif) {
  115. myif.parentNode.removeChild(myif);
  116. myif=undefined;
  117. }
  118. GM_log('2. myif typeof == ' + typeof myif);
  119. if (!myif) {
  120. myif = document.createElement('iframe')
  121. //myif.style.display='none'
  122. myif.style.visibility = 'hidden'
  123. myif.style.width='1px';
  124. myif.style.height='1px';
  125. document.body.appendChild(myif);
  126. myif.contentWindow.addEventListener('DOMContentLoaded',function(){
  127. //GM_log('iframe content loaded')
  128. if (next) {
  129. next.style.color='#ff0084';
  130. next.addEventListener('click', clickNext ,true);
  131. document.addEventListener('keydown', checkKey ,true);
  132. }
  133. var links_test = search(document,"//a[@id='NextLink']");
  134. if (links_test.snapshotLength ==0) {
  135. links_test = search(document,"//a[@id='pageNextBottomBtn']"); // for 2015 新版
  136. }
  137. if (links_test.snapshotLength == 0) {
  138. //alert('Cannot fetch Next page , retry....');
  139. GM_log('Cannot fetch Next page , retry....');
  140. myif.removeNode(true);
  141. prefetch();
  142. }
  143. },false);
  144. }
  145. myif.setAttribute('src', nhref);
  146. G_prefetch_url = nhref;
  147. }
  148.  
  149. function onLoad() {
  150. if (window != top) return; // avoid creating infinitely deep nested frames
  151. prefetch();
  152. }
  153.  
  154. window.addEventListener('load', function() {
  155. onLoad();
  156. }, true);
  157. window.addEventListener('popstate' , function(event) {
  158. alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
  159. //History.back();
  160. }, true);
  161.  
  162. })();
  163.  
  164.  
  165.  
  166.  
  167.