东方永页机 - 自动翻页脚本

自动加载并拼接下一分页内容,无需规则即可支持任何网页

当前为 2022-03-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Pagetual - Perpetual pages
  3. // @name:zh-CN 东方永页机 - 自动翻页脚本
  4. // @name:zh-TW 東方永頁機
  5. // @name:ja 東方永頁機
  6. // @name:ru Пейджетуал
  7. // @name:de Pagetual
  8. // @name:es Pagetual
  9. // @name:fr Pagetual
  10. // @name:it Pagetual
  11. // @name:ko 東方永頁機
  12. // @namespace hoothin
  13. // @version 1.6.1
  14. // @description Most compatible Auto Pager script ever. Auto loading next paginated web pages and inserting into current page.
  15. // @description:zh-CN 自动加载并拼接下一分页内容,无需规则即可支持任何网页
  16. // @description:zh-TW 自動加載並拼接下一分頁內容,無需規則即可支持任意網頁
  17. // @description:ja Webページを自動で読み込み継ぎ足し表示を行うブラウザ拡張です
  18. // @description:ru Просто автоматически загрузите следующую страницу
  19. // @description:de Laden Sie einfach automatisch die nächste Seite
  20. // @description:es Carga automática de páginas web paginadas
  21. // @description:fr Chargement automatique des pages Web paginées
  22. // @description:it Caricamento automatico di pagine Web impaginate
  23. // @description:ko 다음 페이지를 자동으로 로드하세요
  24. // @author hoothin
  25. // @include http://*
  26. // @include https://*
  27. // @license MIT
  28. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAdVBMVEUAAAD3VU33VU32VEz8U073VU32VU33VU32VUz0Uk/3VE32VU32VUz2VU32VU32VU32VU33VU33U0z2VU34Wkv3VE32VUz/mpj/nJj2VUz2VU32VE33VEz2VU32VU32VUz3VE32VEz3VE3/mZf2Vkz2VU3/mpilFFolAAAAJXRSTlMA3Lp/GvTBT5YQLuawZ/DOyZwlPQeKc21N04+FX1bqpm9DNoB4T68ePwAAAitJREFUWMPt1tuasiAUBuCFCG5Rs3QybTPV1/1f4v/3PDkyIojn8x5qBrI+ltAfh32/yysmBKvyXb+njb6bDL9kzTd5SzjDAsYT8nFoGSxYe6BVqoNDp2jFDit25BRgVUAODB4YWcETWVTwVNGiGN5iWtBgg4YMCpsoI38dNunmmWyxUTvbPwwbsYR0fIzZLQ0pTG8eieRmBLMmpdH9uimQEf6TNRnXXKLZHixpJtywLzOgMHtFCqdM64DahHRnOE1dsrekm9wr2WtLcAlpdHwcp1pAJySXYnERclzp4+v19jXdmcTvQUJtz+ZaI4i05/V/UGYrCxbaAsOYoNfIKEQxpqQuzCgJJJ/3f42O8ywEZuMVWi/8hODxGj3GW2b0udkbGULLDOjimAG0S3fLGlBnXQM9irG1CiQdVQi0dqQsOSDlyEEz7Vy9OxxfR71VCXsSB23jMrKJYZXSjw57sqgLn5Z0wolsOCz0RyJkyeYjgz7pwwVq20eboZwtVUl2EnN5gJ50dQZFdryATvABRTr/tJXkUMdaAK5pwtCapwtFLskguwuyMh/Sd9WChQ4sIvIUYSk3PYqQvCQlOC04IfN7PkdjOyRKWhdKXMmiAFt9i3sJ5jxoRuR0vqAghxxwHuqfQE5OHGDKOrwEnqs1DgAZ2e4Eev1d45TN7JfhrQLKgfwMFYAsvp33dXII073aVQLI2gN5S58lfmGnKKFtah7nkgnBZB7zlP7Y/QNiTM6sYNzawwAAAABJRU5ErkJggg==
  29. // @grant GM_xmlhttpRequest
  30. // @grant GM_registerMenuCommand
  31. // @grant GM_notification
  32. // @grant GM_getValue
  33. // @grant GM_setValue
  34. // @grant GM_addStyle
  35. // @grant GM_openInTab
  36. // @grant GM_deleteValue
  37. // @grant GM.xmlHttpRequest
  38. // @grant GM.registerMenuCommand
  39. // @grant GM.notification
  40. // @grant GM.getValue
  41. // @grant GM.setValue
  42. // @grant GM.addStyle
  43. // @grant GM.openInTab
  44. // @grant GM.deleteValue
  45. // @supportURL https://github.com/hoothin/UserScripts/issues
  46. // @connect wedata.net
  47. // @connect githubusercontent.com
  48. // @run-at document-idle
  49. // @exclude http://www.toodledo.com/tasks/*
  50. // @exclude http*://maps.google.com*/*
  51. // @exclude *://www.google.*/_/chrome/newtab*
  52. // @exclude *://mega.*/*
  53. // @exclude *://*.mega.*/*
  54. // @exclude *://*.youku.com/v_*
  55. // @exclude *://*pan.baidu.com
  56. // @exclude *://*.iqiyi.com/v_*
  57. // @exclude *://*.iqiyi.com/w_*
  58. // @exclude *://*.iqiyi.com/a_*
  59. // @exclude *://*.le.com/ptv/vplay/*
  60. // @exclude *://v.qq.com/x/cover/*
  61. // @exclude *://v.qq.com/x/page/*
  62. // @exclude *://v.qq.com/tv/*
  63. // @exclude *://*.tudou.com/listplay/*
  64. // @exclude *://*.tudou.com/albumplay/*
  65. // @exclude *://*.tudou.com/programs/view/*
  66. // @exclude *://*.mgtv.com/b/*
  67. // @exclude *://film.sohu.com/album/*
  68. // @exclude *://tv.sohu.com/v/*
  69. // @exclude *://*.bilibili.com/video/*
  70. // @exclude *://*.bilibili.com/bangumi/play/*
  71. // @exclude *://*.baofeng.com/play/*
  72. // @exclude *://vip.pptv.com/show/*
  73. // @exclude *://v.pptv.com/show/*
  74. // @exclude *://www.le.com/ptv/vplay/*
  75. // @exclude *://www.wasu.cn/Play/show/*
  76. // @exclude *://m.v.qq.com/*
  77. // @exclude *://m.iqiyi.com/*
  78. // @exclude *://m.youku.com/alipay_video/*
  79. // @exclude *://m.mgtv.com/b/*
  80. // @exclude *://m.tv.sohu.com/v/*
  81. // @exclude *://m.film.sohu.com/album/*
  82. // @exclude *://m.le.com/ptv/vplay/*
  83. // @exclude *://m.pptv.com/show/*
  84. // @exclude *://m.acfun.cn/v/*
  85. // @exclude *://m.bilibili.com/video/*
  86. // @exclude *://m.bilibili.com/anime/*
  87. // @exclude *://m.bilibili.com/bangumi/play/*
  88. // @exclude *://m.wasu.cn/Play/show/*
  89. // @exclude *://www.youtube.com
  90. // @exclude *://www.youtube.com/
  91. // @exclude *://www.youtube.com/watch*
  92. // @exclude *://baike.baidu.com/*
  93. // @exclude *://*.instagram.com/*
  94. // @exclude *://order.jd.com/*
  95. // @exclude *://pagetual.hoothin.com/*
  96. // @exclude *://hoothin.github.io/PagetualGuide/*
  97. // @exclude *://wenku.baidu.com/view/*
  98. // @connect *
  99. // ==/UserScript==
  100.  
  101. (function() {
  102. 'use strict';
  103.  
  104. if (window.name === 'pagetual-iframe' || (window.frameElement && window.frameElement.name === 'pagetual-iframe')) {
  105. var domloaded = function (){
  106. window.scroll(window.scrollX, 999999);
  107. //window.parent.postMessage('pagetual-iframe:DOMLoaded', '*');
  108. };
  109. if(window.opera){
  110. document.addEventListener('DOMContentLoaded', domloaded, false);
  111. } else {
  112. domloaded();
  113. }
  114. return;
  115. }
  116.  
  117. if(window.top != window.self){
  118. try{
  119. if (top.location.hostname != window.location.hostname || window.top != window.parent) {
  120. return;
  121. }
  122. }catch(e){
  123. return;
  124. }
  125. }
  126.  
  127. const lang = navigator.appName=="Netscape"?navigator.language:navigator.userLanguage;
  128. var i18n=(name, param)=>{
  129. let config={};
  130. switch (lang){
  131. case "zh-CN":
  132. case "zh-SG":
  133. config={
  134. enableDebug:"调试模式,输出信息至控制台",
  135. disable:"暂时禁用",
  136. disableSite:"在此站禁用",
  137. enable:"启用翻页",
  138. toTop:"回到顶部",
  139. toBottom:"前往页尾",
  140. current:"当前页",
  141. forceIframe:"强制拼接",
  142. cancelForceIframe:"取消强制拼接",
  143. configure:"打开配置页",
  144. firstUpdate:"点击此处初始化规则",
  145. update:"更新规则",
  146. click2update:"点击立即更新规则",
  147. loadNow:"立即翻页",
  148. loadConfirm:"要翻几页?(0为不间断)",
  149. noNext:"没有找到下一页,请新建规则",
  150. passSec:"更新于 #t# 秒前",
  151. passMin:"更新于 #t# 分钟前",
  152. passHour:"更新于 #t# 小时前",
  153. passDay:"更新于 #t# 天前",
  154. cantDel:"无法删除内置规则",
  155. confirmDel:"是否确认要删除此规则?",
  156. updateSucc:"更新成功",
  157. beginUpdate:"正在更新,请稍候",
  158. customUrls:"导入规则url,一行一条,AutoPagerize 格式规则需要以\"0|\"开头",
  159. customRules:"输入【东方永页机】格式的自定义规则",
  160. save:"保存设置",
  161. loadingText:"少女祈祷中...",
  162. opacity:"分页隔条不透明值",
  163. hideBar:"空白处双击隐藏分页隔条",
  164. dbClick2Stop:"空白处双击暂停翻页",
  165. sortTitle:"排序在下次更新规则后生效",
  166. autoRun:"自动启用,否则为白名单模式",
  167. inputPageNum:"输入页码跳转",
  168. enableHistory:"翻页后写入历史记录",
  169. initRun:"打开页面后立即尝试翻页,否则滚动至页尾再翻页",
  170. preload:"翻页前预读下一页,加速浏览",
  171. click2ImportRule:"点击下方添加特殊规则库,添加后再行更新:",
  172. forceAllBody:"是否拼接整个页面?",
  173. openInNewTab:"使拼接页面的内容在新页面打开"
  174. };
  175. break;
  176. case "zh-TW":
  177. case "zh-HK":
  178. config={
  179. enableDebug:"調試模式,輸出信息至控制台",
  180. disable:"暫時禁用",
  181. disableSite:"在此站禁用",
  182. enable:"啟用翻頁",
  183. toTop:"回到頂部",
  184. toBottom:"前往頁尾",
  185. current:"當前頁",
  186. forceIframe:"強制拼接",
  187. cancelForceIframe:"取消强制拼接",
  188. configure:"打開配置頁",
  189. firstUpdate:"點擊此處初始化規則",
  190. update:"更新規則",
  191. click2update:"點擊立即更新規則",
  192. loadNow:"立即翻頁",
  193. loadConfirm:"要翻几頁?(0為不間斷)",
  194. noNext:"沒有找到下一頁,請新建規則",
  195. passSec:"更新于 #t# 秒前",
  196. passMin:"更新于 #t# 分鐘前",
  197. passHour:"更新于 #t# 小時前",
  198. passDay:"更新于 #t# 天前",
  199. cantDel:"無法刪除内置規則",
  200. confirmDel:"是否確認要刪除此規則?",
  201. updateSucc:"更新成功",
  202. beginUpdate:"正在更新,請稍候",
  203. customUrls:"導入規則url,一行一條,AutoPagerize 格式規則需要以\"0|\"開頭",
  204. customRules:"輸入【東方永頁機】格式的自定義規則",
  205. save:"存儲設置",
  206. loadingText:"少女祈禱中...",
  207. opacity:"分頁隔條不透明值",
  208. hideBar:"空白處雙擊隱藏分頁隔條",
  209. dbClick2Stop:"空白處雙擊暫停翻頁",
  210. sortTitle:"排序在下次更新規則後生效",
  211. autoRun:"自動啓用,否則為白名單模式",
  212. inputPageNum:"輸入頁碼跳轉",
  213. enableHistory:"翻頁后寫入歷史記錄",
  214. initRun:"打開頁面后立即嘗試翻頁,否則滾動至頁尾再翻頁",
  215. preload:"翻頁前預讀下一頁,加速瀏覽",
  216. click2ImportRule:"點擊下方添加特殊規則庫,添加後再行更新:",
  217. forceAllBody:"是否拼接整個頁面?",
  218. openInNewTab:"使拼接頁面的内容在新頁面打開"
  219. };
  220. break;
  221. case "ja":
  222. config = {
  223. enableDebug:"デバッグモード",
  224. disable: "一時的に無効にする",
  225. disableSite:"このサイト無効",
  226. enable: "ページめくりを有効にする",
  227. toTop: "トップに戻る",
  228. toBottom: "ページの下部に移動",
  229. current: "現在のページ",
  230. forceIframe: "強制ステッチ",
  231. cancelForceIframe: "強制ステッチをキャンセル",
  232. configure: "設定ページを開く",
  233. firstUpdate:"ここをクリックしてルールを初期化します",
  234. update: "更新ルール",
  235. click2update:"今すぐルールを更新してください",
  236. loadNow:"今すぐページをめくる",
  237. loadConfirm:"数ページめくりたいですか?(0は途切れない)",
  238. noNext:"次のページが見つかりません、新しいルールを作成してください",
  239. passSec: "#t#秒前に更新",
  240. passMin: "#t#分前に更新",
  241. passHour: "#t#時間前に更新",
  242. passDay: "#t#日前に更新",
  243. cantDel: "組み込みルールを削除できません",
  244. confirmDel: "このルールを削除してもよろしいですか?",
  245. updateSucc: "更新に成功しました",
  246. beginUpdate: "更新中、お待ちください",
  247. customUrls: "インポートルールのURL、1行に1つ、AutoPagerizeフォーマットルールは\"0|\"で始まる必要があります",
  248. customRules: "【東方永頁機】の形式でカスタムルールを入力してください",
  249. save: "設定を保存",
  250. loadingText: "少女祈祷中...",
  251. opacity:"ページネーションバーの不透明値",
  252. hideBar:"空白部分をダブルクリックして、ページ区切り文字を非表示にします",
  253. dbClick2Stop:"空白部分をダブルクリックしてページめくりを一時停止します",
  254. sortTitle:"並べ替えは、次のルールの更新後に有効になります",
  255. autoRun:"自動的に有効",
  256. inputPageNum:"ジャンプするページ番号を入力",
  257. enableHistory:"ページめくり後の履歴を書く",
  258. initRun:"Webページを開いた直後にページをめくる",
  259. preload:"事前に次のページを読む",
  260. click2ImportRule:"以下をクリックして、ルールベースを追加します:",
  261. forceAllBody:"フルページ埋め込み?",
  262. openInNewTab:"スプライスされたページのコンテンツを新しいページで開きます"
  263. };
  264. break;
  265. default:
  266. config={
  267. enableDebug:"Enable debug output",
  268. disable:"Disable",
  269. disableSite:"Disable on the site",
  270. enable:"Enable",
  271. toTop:"To Top",
  272. toBottom:"To Bottom",
  273. current:"Current Page",
  274. forceIframe:"Force to join",
  275. cancelForceIframe:"Cancel Force join",
  276. configure:"Configure",
  277. firstUpdate:"Click here to initialize the rules",
  278. update:"Update rules",
  279. click2update:"Click to update rules from url now",
  280. loadNow:"Load next now",
  281. loadConfirm:"How much pages do you want to load? (0 means infinite)",
  282. noNext:"No next link found, please create a new rule",
  283. passSec:"Updated #t# seconds ago",
  284. passMin:"Updated #t# minutes ago",
  285. passHour:"Updated #t# hours ago",
  286. passDay:"Updated #t# days ago",
  287. cantDel:"Can't delete buildin rules",
  288. confirmDel:"Are you sure you want to delete this rule?",
  289. updateSucc:"Update succeeded",
  290. beginUpdate:"Begin update, wait a minute please",
  291. customUrls:"Import rule url, One url per line, rules on AutoPagerize format need to start with \"0|\"",
  292. customRules:"Input custom rules with [Pagetual] format",
  293. save:"Save",
  294. loadingText:"Shojo Now Loading...",
  295. opacity:"Pagination spacer opacity",
  296. hideBar:"Double-click on the blank space to hide the paging spacer",
  297. dbClick2Stop:"Double-click on the blank space to stop",
  298. sortTitle:"Sorting takes effect after the next rule update",
  299. autoRun:"Auto run (black list mode)",
  300. inputPageNum:"Enter page number to jump",
  301. enableHistory:"Write history after page turning",
  302. initRun:"Turn pages immediately after opening",
  303. preload:"Preload next page for speeding up",
  304. click2ImportRule:"Click to import base rules link, then click to update rules:",
  305. forceAllBody:"Join full body of page?",
  306. openInNewTab:"Open urls of additions in new tab"
  307. };
  308. break;
  309. }
  310. return config[name]?config[name].replace("#t#",param):name;
  311. };
  312.  
  313. var noRuleTest=false;
  314. var enableDebug=true;
  315. var debug=str=>{
  316. if(enableDebug){
  317. console.debug(str);
  318. }
  319. };
  320.  
  321. var _GM_xmlhttpRequest,_GM_registerMenuCommand,_GM_notification,_GM_addStyle,_GM_openInTab;
  322. if(typeof GM_xmlhttpRequest!='undefined'){
  323. _GM_xmlhttpRequest=GM_xmlhttpRequest;
  324. }else if(typeof GM!='undefined' && typeof GM.xmlHttpRequest!='undefined'){
  325. _GM_xmlhttpRequest=GM.xmlHttpRequest;
  326. }else{
  327. _GM_xmlhttpRequest=(f)=>{fetch(f.url).then(response=>response.text()).then(data=>{let res={response:data};f.onload(res)}).catch(f.onerror())};
  328. }
  329. if(typeof GM_registerMenuCommand!='undefined'){
  330. _GM_registerMenuCommand=GM_registerMenuCommand;
  331. }else if(typeof GM!='undefined' && typeof GM.registerMenuCommand!='undefined'){
  332. _GM_registerMenuCommand=GM.registerMenuCommand;
  333. }else{
  334. _GM_registerMenuCommand=(s,f)=>{};
  335. }
  336. if(typeof GM_notification!='undefined'){
  337. _GM_notification=GM_notification;
  338. }else if(typeof GM!='undefined' && typeof GM.notification!='undefined'){
  339. _GM_notification=GM.notification;
  340. }else{
  341. _GM_notification=(s)=>{alert(s)};
  342. }
  343. if(typeof GM_openInTab!='undefined'){
  344. _GM_openInTab=GM_openInTab;
  345. }else if(typeof GM!='undefined' && typeof GM.openInTab!='undefined'){
  346. _GM_openInTab=GM.openInTab;
  347. }else{
  348. _GM_openInTab=(s,t)=>{window.open(s)};
  349. }
  350. if(typeof GM_addStyle!='undefined'){
  351. _GM_addStyle=GM_addStyle;
  352. }else if(typeof GM!='undefined' && typeof GM.addStyle!='undefined'){
  353. _GM_addStyle=GM.addStyle;
  354. }else{
  355. _GM_addStyle=cssStr=>{
  356. let styleEle=document.createElement("style");
  357. styleEle.innerHTML=cssStr;
  358. document.head.appendChild(styleEle);
  359. };
  360. }
  361. var _unsafeWindow=(typeof unsafeWindow=='undefined')?window:unsafeWindow;//兼容 ios userscripts 的寫法
  362. var storage={
  363. supportGM: typeof GM_getValue=='function' && typeof GM_getValue('a','b')!='undefined',
  364. supportGMPromise: typeof GM!='undefined' && typeof GM.getValue=='function' && typeof GM.getValue('a','b')!='undefined',
  365. mxAppStorage:(function(){
  366. try{
  367. return window.external.mxGetRuntime().storage;
  368. }catch(e){
  369. }
  370. })(),
  371. operaUJSStorage:(function(){
  372. try{
  373. return window.opera.scriptStorage;
  374. }catch(e){
  375. }
  376. })(),
  377. setItem:function(key,value){
  378. if(this.operaUJSStorage){
  379. this.operaUJSStorage.setItem(key,value);
  380. }else if(this.mxAppStorage){
  381. this.mxAppStorage.setConfig(key,value);
  382. }else if(this.supportGM){
  383. GM_setValue(key,value);
  384. if(value==="" && typeof GM_deleteValue!='undefined'){
  385. GM_deleteValue(key);
  386. }
  387. }else if(this.supportGMPromise){
  388. GM.setValue(key,value);
  389. if(value==="" && typeof GM!='undefined' && typeof GM.deleteValue!='undefined'){
  390. GM.deleteValue(key);
  391. }
  392. }else if(window.localStorage){
  393. window.localStorage.setItem(key,value);
  394. }
  395. },
  396. getItem:function(key,cb){
  397. var value;
  398. if(this.operaUJSStorage){
  399. value=this.operaUJSStorage.getItem(key);
  400. }else if(this.mxAppStorage){
  401. value=this.mxAppStorage.getConfig(key);
  402. }else if(this.supportGM){
  403. value=GM_getValue(key);
  404. }else if(this.supportGMPromise){
  405. value=GM.getValue(key).then(v=>{cb(v)});
  406. return;
  407. }else if(window.localStorage){
  408. value=window.localStorage.getItem(key);
  409. };
  410. cb(value);
  411. }
  412. };
  413. var rulesData={},ruleUrls,updateDate,configPage="https://github.com/hoothin/UserScripts/tree/master/Pagetual";
  414. var ruleImportUrlReg=/greasyfork\.org\/.*scripts\/438684[^\/]*(\/discussions|\/?$)|github\.com\/hoothin\/UserScripts\/(tree\/master\/Pagetual|issues)/i;
  415. var allOfBody="body>*";
  416. _GM_registerMenuCommand(i18n("configure"), ()=>{
  417. _GM_openInTab(configPage,{active:true});
  418. });
  419.  
  420. function getElementByXpath(xpath, contextNode, doc){
  421. doc = doc || document;
  422. contextNode = contextNode || doc;
  423. try {
  424. var result = doc.evaluate(xpath, contextNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  425. return result.singleNodeValue && result.singleNodeValue.nodeType === 1 && result.singleNodeValue;
  426. } catch (err) {
  427. throw new Error(`Invalid xpath: ${xpath}`);
  428. }
  429. }
  430.  
  431. function getAllElementsByXpath(xpath, contextNode, doc){
  432. doc = doc || document;
  433. contextNode = contextNode || doc;
  434. var result = [];
  435. try {
  436. var query = doc.evaluate(xpath, contextNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  437. for (var i = 0; i < query.snapshotLength; i++) {
  438. var node = query.snapshotItem(i);
  439. if (node.nodeType === 1) result.push(node);
  440. }
  441. } catch (err) {
  442. throw new Error(`Invalid xpath: ${xpath}`);
  443. }
  444. return result;
  445. }
  446.  
  447. class RuleParser {
  448. constructor() {
  449. this.hpRules=[];
  450. this.customRules=[];
  451. this.rules=[];
  452. this.pageDoc=document;
  453. this.nextLinkHref=null;
  454. this.oldUrl="";
  455. this.curUrl=location.href;
  456. this.curSiteRule={};
  457. }
  458.  
  459. initSavedRules(callback){
  460. var self=this;
  461. storage.getItem("hpRules", hpRules=>{
  462. if(hpRules)self.hpRules=hpRules;
  463. storage.getItem("customRules", customRules=>{
  464. if(customRules)self.customRules=customRules;
  465. storage.getItem("rules", rules=>{
  466. if(rules)self.rules=rules;
  467. callback();
  468. });
  469. });
  470. });
  471. }
  472.  
  473. saveCurSiteRule(){
  474. if(!this.curSiteRule || !this.curSiteRule.url || this.curSiteRule.singleUrl)return;
  475. this.hpRules=this.hpRules.filter(item=>{return item&&item.url!=this.curSiteRule.url});
  476. this.hpRules.unshift(this.curSiteRule);
  477. if(this.hpRules.length>30){
  478. this.hpRules.pop();
  479. }
  480. storage.setItem("hpRules", this.hpRules);
  481. }
  482.  
  483. requestJSON(url, callback){
  484. _GM_xmlhttpRequest({
  485. url: url,
  486. method: 'GET',
  487. timeout: 20000,
  488. onload: function(res) {
  489. let json=null;
  490. try{
  491. json=JSON.parse(res.response||res.responseText);
  492. }catch(e){
  493. debug(e);
  494. }
  495. callback(json);
  496. },
  497. onerror: function(e) {
  498. callback(null, e);
  499. },
  500. ontimeout: function(e) {
  501. callback(null, e);
  502. }
  503. });
  504. }
  505.  
  506. formatRule(item, type, from){
  507. switch(type){
  508. case 0:
  509. return {
  510. from:from,
  511. type:type,
  512. name:item.name,
  513. action:item.data.forceIframe=="true"?1:0,
  514. url:item.data.url,
  515. pageElement:item.data.pageElement,
  516. nextLink:item.data.nextLink,
  517. insert:item.data.insertBefore,
  518. insertPos:1,
  519. updatedAt:item.data.updated_at,
  520. css:item.data.Stylus+item.data.CSS,
  521. pageAction:item.data.bookmarklet
  522. };
  523. break;
  524. case 1:
  525. default:
  526. item.from=from;
  527. if(typeof(item.type) == "undefined")item.type=type;
  528. return item;
  529. break;
  530. }
  531. return null;
  532. }
  533.  
  534. addRuleByUrl(url, type, from, callback) {
  535. if(url.indexOf("?")==-1){
  536. url=url+"?"+Date.now();
  537. }else{
  538. url=url+"&"+Date.now();
  539. }
  540. this.requestJSON(url, (json,err)=>{
  541. if(!json){
  542. debug("Update "+url+" rules fail!");
  543. debug(err);
  544. }
  545. this.addRules(json, type, from);
  546. callback(json, err);
  547. });
  548. }
  549.  
  550. addRules(rules, type, from) {
  551. if(rules && rules.length>0){
  552. this.rules=this.rules.filter(item=>{return item.from!=from});
  553. rules.forEach(item=>{
  554. let rule=this.formatRule(item, type, from);
  555. if(rule){
  556. this.rules.unshift(rule);
  557. }
  558. });
  559. }
  560. }
  561.  
  562. getRule(callback) {
  563. if(noRuleTest){
  564. this.curSiteRule={};
  565. this.curSiteRule.url=location.origin+location.pathname;
  566. this.curSiteRule.singleUrl=true;
  567. callback();
  568. return;
  569. }
  570. if(this.curSiteRule && this.curSiteRule.url){
  571. return this.curSiteRule;
  572. }
  573. var self=this;
  574.  
  575. function setRule(r){
  576. self.curSiteRule=r;
  577. debug(r);
  578. callback();
  579. }
  580.  
  581. function ruleMatch(r){
  582. let pageElement,nextLink,insert;
  583. if(r.nextLink){
  584. nextLink=r.type==0?getElementByXpath(r.nextLink):document.querySelector(r.nextLink);
  585. if(!nextLink)return false;
  586. }
  587. if(r.pageElement){
  588. pageElement=r.type==0?getElementByXpath(r.pageElement):document.querySelector(r.pageElement);
  589. if(!pageElement)return false;
  590. }
  591. if(r.insert){
  592. insert=r.type==0?getElementByXpath(r.insert):document.querySelector(r.insert);
  593. if(!insert)return false;
  594. }
  595. return true;
  596. }
  597.  
  598. function checkRule(r){
  599. let urlReg=new RegExp(r.url, "i");
  600. if(urlReg.test(location.href)){
  601. if(r.wait){
  602. let waitTime=500,checkEval;
  603. if(isNaN(r.wait)){
  604. try{
  605. checkEval=Function("doc",'"use strict";' + r.wait);
  606. }catch(e){
  607. debug(e);
  608. }
  609. }else{
  610. waitTime=r.wait;
  611. }
  612. let checkReady=()=>{
  613. setTimeout(()=>{
  614. if(!ruleMatch(r) || (checkEval && !checkEval(document))){
  615. checkReady();
  616. }else{
  617. setRule(r);
  618. }
  619. },parseInt(waitTime));
  620. };
  621. checkReady();
  622. return true;
  623. }
  624. if(r.pinUrl){
  625. setRule(r);
  626. return true;
  627. }
  628. if(!ruleMatch(r)){
  629. return false;
  630. }
  631. setRule(r);
  632. return true;
  633. }
  634. return false;
  635. }
  636.  
  637. for(let i in this.hpRules){
  638. let rule=this.hpRules[i];
  639. if(!rule || !rule.url)continue;
  640. if(rule.singleUrl){
  641. if(location.origin+location.pathname==rule.url){
  642. setRule(rule);
  643. return;
  644. }
  645. continue;
  646. }
  647. if(checkRule(rule))return;
  648. }
  649. for(let i in this.customRules){
  650. let rule=this.customRules[i];
  651. if(!rule || !rule.url)continue;
  652. if(checkRule(rule))return;
  653. }
  654. let r=0;
  655. function searchByTime(){
  656. setTimeout(()=>{
  657. let end=r+80;
  658. end=end>self.rules.length?self.rules.length:end;
  659. for(;r<end;r++){
  660. let rule=self.rules[r];
  661. if(checkRule(rule))return;
  662. }
  663. if(end>=self.rules.length){
  664. self.curSiteRule={};
  665. self.curSiteRule.url=location.origin+location.pathname;
  666. self.curSiteRule.singleUrl=true;
  667. callback();
  668. return;
  669. }else{
  670. searchByTime();
  671. }
  672. },20);
  673. }
  674. searchByTime();
  675. }
  676.  
  677. geneSelector(ele){
  678. let selector=ele.tagName;
  679. //Google id class都是隨機。百度更過分,style script順序都是隨機的
  680. //if(ele.id) selector += '#' + ele.id;
  681. //if(ele.classList) selector += [].map.call(ele.classList,d=>'.'+d).join('');
  682. let parent = ele.parentElement;
  683. if(parent){
  684. let i,j=0;
  685. for(i=0;i<parent.children.length;i++){
  686. if(parent.children[i].tagName==selector){
  687. j++;
  688. if(parent.children[i]==ele){
  689. break;
  690. }
  691. }
  692. }
  693. selector = this.geneSelector(parent) + ' > ' + selector + (parent.tagName=="HTML"?"":`:nth-of-type(${j})`);
  694. }
  695. return selector;
  696. }
  697.  
  698. getPageElement(doc, curWin, dontFind) {
  699. let pageElement=null;
  700. let self=this;
  701. let body=doc.body;
  702. if(this.curSiteRule.pageElement){
  703. pageElement=this.curSiteRule.type==0?getAllElementsByXpath(this.curSiteRule.pageElement,doc,doc):doc.querySelectorAll(this.curSiteRule.pageElement);
  704. }else if(!this.curSiteRule.singleUrl && this.curSiteRule.type==0){
  705. pageElement=[body];
  706. }
  707. if((!pageElement || pageElement.length==0) && curWin && !dontFind){
  708. if(!body)return null;
  709. let bodyHeight=parseInt(body.scrollHeight);
  710. let curHeight=bodyHeight;
  711. let windowHeight=window.innerHeight || document.documentElement.clientHeight;
  712. let needCheckNext=(doc==document && this.initNext);
  713. function checkElement(ele){
  714. if(ele.childNodes && ele.childNodes.length==1)ele=ele.childNodes[0];
  715. if(ele.tagName=="PICTURE"){
  716. self.curSiteRule.pageElement=self.geneSelector(ele.parentNode)+">"+ele.tagName;
  717. debug(self.curSiteRule.pageElement);
  718. return [ele];
  719. }
  720. if(curHeight/bodyHeight<=0.25){
  721. self.curSiteRule.pageElement=allOfBody;
  722. debug(self.curSiteRule.pageElement);
  723. return [body];
  724. }
  725. if(ele.tagName=="TBODY" || ele.tagName=="UL"){
  726. self.curSiteRule.pageElement=self.geneSelector(ele)+">*";
  727. if(ele.children.length>0 && ele.children[0].querySelector("th")){
  728. self.curSiteRule.pageElement+=":not(:first-child)";
  729. }
  730. debug(self.curSiteRule.pageElement);
  731. return ele.children;
  732. }
  733. if(ele.children.length==0 && !self.curSiteRule.pageElement){
  734. if(ele.parentNode.tagName=="P")ele=ele.parentNode;
  735. self.curSiteRule.pageElement=self.geneSelector(ele.parentNode)+">"+ele.tagName;
  736. debug(self.curSiteRule.pageElement);
  737. return [ele];
  738. }
  739. let i,maxHeight=curHeight*0.6,curMaxEle=null,curMaxArea=0,maxWidth=0;
  740. let isHori=true;
  741. let offsetTop=ele.children[0].offsetTop;
  742. for(i=1;i<ele.children.length;i++){
  743. if(ele.children[i].offsetTop!=offsetTop){
  744. isHori=false;
  745. break;
  746. }
  747. }
  748. for(i=0;i<ele.children.length;i++){
  749. let curNode=ele.children[i];
  750. if(curNode.tagName=="CANVAS")continue;
  751. if(curNode.tagName!="IMG" && curNode.innerHTML=="")continue;
  752. if(needCheckNext && !curNode.contains(self.initNext) && getElementTop(curNode)>windowHeight){
  753. continue;
  754. }
  755. let comStyle=curWin.getComputedStyle(curNode);
  756. let h=parseInt(curNode.scrollHeight);
  757. let w=parseInt(curNode.scrollWidth);
  758. if(isNaN(h) || isNaN(w))continue;
  759. if(isHori && h==0)continue;
  760. let a=h*w+h,moreChild=curNode.children[0];
  761. while(moreChild){
  762. comStyle=curWin.getComputedStyle(moreChild);
  763. let ch=parseInt(moreChild.scrollHeight);
  764. let cw=parseInt(moreChild.scrollWidth);
  765. if(h<ch)h=ch;
  766. if(moreChild.innerText!="" && !isNaN(ch) && !isNaN(cw)){
  767. a+=ch*cw;
  768. }
  769. moreChild=moreChild.nextElementSibling;
  770. }
  771. let isMax=false;
  772. if(isHori){
  773. if(maxWidth<w){
  774. isMax=true;
  775. }else if(maxWidth==w && curMaxArea<a){
  776. isMax=true;
  777. }
  778. }else{
  779. isMax=curMaxArea<a;
  780. }
  781. if(curMaxEle==null || isMax){
  782. if(h>0 || !isHori)curHeight=h;
  783. curMaxArea=a;
  784. maxWidth=w;
  785. curMaxEle=curNode;
  786. }
  787. }
  788. if(curMaxEle && (isHori || curHeight>maxHeight || (needCheckNext && curHeight>windowHeight && ele.contains(self.initNext)))){
  789. return checkElement(curMaxEle);
  790. }
  791. if(ele.tagName=="P" || ele.tagName=="BR")ele=ele.parentNode;
  792. self.curSiteRule.pageElement=self.geneSelector(ele);
  793. if(ele.children.length>1){
  794. let middleChild=ele.children[parseInt(ele.children.length/2)];
  795. if((middleChild.style && middleChild.style.position=="absolute" && middleChild.style.left && middleChild.style.top) || ele.tagName=="UL" || curHeight==0){
  796. ele=[ele];
  797. }else{
  798. self.curSiteRule.pageElement+=">*";
  799. ele=ele.children;
  800. }
  801. }else{
  802. ele=[ele];
  803. }
  804. debug(self.curSiteRule.pageElement);
  805. return ele;
  806. }
  807. pageElement=checkElement(body);
  808. if(pageElement)this.saveCurSiteRule();
  809. }
  810. /*if(this.insert && this.insert.parentNode && pageElement && pageElement[0]){
  811. let example=this.insertPos==2?this.insert.children[0]:this.insert;
  812. let inTable=example.parentNode.tagName=="TABLE" ||
  813. example.tagName=="TR" ||
  814. example.tagName=="TBODY" ||
  815. (example.previousElementSibling && example.previousElementSibling.tagName=="TR") ||
  816. (example.previousElementSibling && example.previousElementSibling.tagName=="TBODY");
  817. if(inTable && pageElement[0].tagName!="TR" && pageElement[0].tagName!="TBODY"){
  818. pageElement=null;
  819. }
  820. }*/
  821. return pageElement;
  822. }
  823.  
  824. getPage(doc){
  825. if(_unsafeWindow.Discourse)return {};
  826. let canSave=false;//發現頁碼選擇器在其他頁對不上,還是別保存了
  827. let url=this.curUrl.replace("#!","").replace("index.php?","?");
  828. let pageNum=1,preStr="",afterStr="";
  829. let pageMatch1=url.match(/(.*[a-z\/\-_](?:p|page)?\/?)(\d+)(\.s?html?$|\/?$)/i);
  830. let pageMatch2=url.match(/(.*[\?&]p(?:age)?=)(\d+)($|[#&].*)/i);
  831. if(pageMatch1){
  832. preStr=pageMatch1[1];
  833. pageNum=parseInt(pageMatch1[2]);
  834. afterStr=pageMatch1[3];
  835. }else if(pageMatch2){
  836. preStr=pageMatch2[1];
  837. pageNum=parseInt(pageMatch2[2]);
  838. afterStr=pageMatch2[3];
  839. }
  840. let curPage=doc,i,cur,jsNext;
  841. let next1,next2,next3,next4,nextJs1,nextJs2,nextJs3;
  842. let next=curPage.querySelector(".page-next>a")||
  843. curPage.querySelector("a.next_page")||
  844. curPage.querySelector("#next_page")||
  845. curPage.querySelector(".nextPage")||
  846. curPage.querySelector("a[data-pagination=next]")||
  847. curPage.querySelector(".pagination a[rel=next]")||
  848. curPage.querySelector(".pagination-nav__item--next>a")||
  849. curPage.querySelector("a[title='Next page']")||
  850. curPage.querySelector("a.pageright")||
  851. curPage.querySelector("a#rightFix")||
  852. curPage.querySelector("a#next")||
  853. curPage.querySelector(".next>a")||
  854. curPage.querySelector(".next>button")||
  855. curPage.querySelector("a[alt=next]")||
  856. curPage.querySelector("button.next")||
  857. curPage.querySelector("[title=next]");
  858. if(!next){
  859. let nexts=curPage.querySelectorAll("a.next");
  860. for(i=0;i<nexts.length;i++){
  861. if(!/^([上前首尾][一1]?[页頁张張]|previous(\s*page)?\s*›?$|前のページ)/i.test(nexts[i].innerText.trim())){
  862. next=nexts[i];
  863. break;
  864. }
  865. }
  866. }
  867. if(next && (!next.href || /^javascript:/.test(next.href))){
  868. jsNext=next;
  869. next=null;
  870. }
  871. if(!next){
  872. next=curPage.querySelectorAll("[aria-label='Next']");
  873. if(next && next.length==1){
  874. next=next[0];
  875. if(!next.href || /^javascript:/.test(next.href)){
  876. jsNext=next;
  877. next=null;
  878. }
  879. }else{
  880. next=null;
  881. }
  882. }
  883. if(!next){
  884. next=curPage.querySelectorAll("[aria-label='Next page']");
  885. if(next && next.length==1){
  886. next=next[0];
  887. if(!next.href || /^javascript:/.test(next.href)){
  888. jsNext=next;
  889. next=null;
  890. }
  891. }else{
  892. next=null;
  893. }
  894. }
  895. if(next && /^[下后後][一1]?[章话話]/i.test(next.innerText.trim())){
  896. next2=next;
  897. next=null;
  898. }
  899. if(!next){
  900. next=curPage.querySelector("a.curr");
  901. if(next){
  902. next=next.nextSibling;
  903. }
  904. }
  905. if(!next){
  906. let pageDiv=curPage.querySelector("div.wp-pagenavi");
  907. if(pageDiv){
  908. cur=pageDiv.querySelector("span.current");
  909. if(cur){
  910. next=cur.nextSibling;
  911. }
  912. }else{
  913. cur=curPage.querySelector("div.article-paging>span");
  914. if(cur){
  915. next=cur.nextElementSibling;
  916. }
  917. }
  918. }
  919. if(!next){
  920. let pageDiv=curPage.querySelector("div.pages>ul");
  921. if(pageDiv){
  922. cur=pageDiv.querySelector("li>b");
  923. if(cur)next=cur.parentNode.nextElementSibling;
  924. if(next)next=next.querySelector("a");
  925. }
  926. }
  927. if(!next){
  928. cur=curPage.querySelector(".number>ul>li.active");
  929. if(cur)next=cur.nextElementSibling;
  930. if(next)next=next.querySelector("a");
  931. }
  932. if(!next){
  933. let aTags=curPage.querySelectorAll("a,button");
  934. for(i=aTags.length-1;i>=0;i--){
  935. let aTag=aTags[i];
  936. if(aTag.innerText=="§")continue;
  937. if(aTag.style.display=="none")continue;
  938. if(next1 && next2 && next3 && next4)break;
  939. if(aTag.href && /next$/i.test(aTag.href))continue;
  940. if(!next1){
  941. if(/^[下后後][一1]?[页頁张張]|^next([ _-]?page)?\s*[›>→»]?$|次のページ|^次へ?$/i.test(aTag.innerText.trim())){
  942. if(aTag.innerText.length>15)continue;
  943. if(!aTag.href || /^javascript:/.test(aTag.href)){
  944. if(!nextJs1)nextJs1=aTag;
  945. }else{
  946. next1=aTag;
  947. }
  948. }
  949. }
  950. if(!next2){
  951. if(/^[下后後][一1]?[章话話]/i.test(aTag.innerText.trim())){
  952. if(!aTag.href || /^javascript:/.test(aTag.href)){
  953. if(!nextJs2)nextJs2=aTag;
  954. }else{
  955. next2=aTag;
  956. }
  957. }
  958. }
  959. if(!next3){
  960. if(aTag.innerText=="&gt;" || aTag.innerText=="▶" || aTag.innerText==">" || aTag.innerText=="›" || aTag.innerText=="→" || aTag.innerText=="»" || /nextpage/i.test(aTag.className)){
  961. if(!aTag.href || /^javascript:/.test(aTag.href)){
  962. if(!nextJs3)nextJs3=aTag;
  963. }else{
  964. next3=aTag;
  965. }
  966. }
  967. }
  968. if(!aTag.href || /^javascript:/.test(aTag.href))continue;
  969. if(!next4 && aTag.href.length<100){
  970. let _aHref=aTag.href.replace("?&","?").replace("#!","").replace("index.php?","?");
  971. if(pageNum<999 && _aHref.replace(preStr,"").replace(afterStr,"")==pageNum+1){
  972. next4=aTag;
  973. }else if(this.curUrl!=aTag.href){
  974. let _url=url.replace(/\.s?html?$/i,"");
  975. _aHref=_aHref.replace(/\.s?html?$/i,"");
  976. if(_aHref.indexOf(_url)!=-1 && /^[\/\?&]?[_-]?(p|page)?=?\/?2(\/|\?|&|$)/i.test(_aHref.replace(_url,""))){
  977. let curHref=aTag.getAttribute("href");
  978. let pageOne=curHref.replace(/\/2(\/|\?|&|$)/,"/1$1");
  979. if(pageOne==curHref)pageOne=null;
  980. else pageOne=curPage.querySelector(`a[href='${pageOne}']`);
  981. if(!pageOne || pageOne.className!=curHref.className)next4=aTag;
  982. }
  983. }
  984. }
  985. }
  986. next=next1||next4||next3||next2||nextJs1||nextJs3||nextJs2;
  987. }
  988. if(!next)next=jsNext;
  989. if(next && next.classList.contains("results-more"))next=null;
  990. return {next:next,canSave:canSave};
  991. }
  992.  
  993. canonicalUri(src, base_path) {
  994. if(src.charAt(0)=="#")return location.href+src;
  995. var root_page = /^[^?#]*\//.exec(location.href)[0],
  996. root_domain = /^\w+\:\/\/\/?[^\/]+/.exec(root_page)[0],
  997. absolute_regex = /^\w+\:\/\//;
  998. src=src.replace(/\.\//,"");
  999. if (/^\/\/\/?/.test(src)){
  1000. src = location.protocol + src;
  1001. }
  1002. else if (!absolute_regex.test(src) && src.charAt(0) != "/"){
  1003. src = (base_path || "") + src;
  1004. }
  1005. return (absolute_regex.test(src) ? src : ((src.charAt(0) == "/" ? root_domain : root_page) + src));
  1006. }
  1007.  
  1008. getLinkByPage(url, pageNum) {
  1009. if(!url)return;
  1010. if(this.curSiteRule.pageNum){
  1011. let result=this.curSiteRule.pageNum;
  1012. let strMatch=result.match(/\{.*?}/);
  1013. if(!strMatch)return null;
  1014. let urlReg=new RegExp("("+result.replace(strMatch[0], ")\\d+(")+")","i");
  1015. let code=strMatch[0].replace(/^{/,"").replace(/}$/,"").replace(/\$p/g,pageNum);
  1016. if(code==pageNum){
  1017. result=url.replace(urlReg,"$1"+code+"$2");
  1018. }else{
  1019. try{
  1020. code=Function('"use strict";return ' + code)();
  1021. result=url.replace(urlReg,"$1"+code+"$2");
  1022. }catch(e){
  1023. debug(e);
  1024. }
  1025. }
  1026. return result==url?url.replace(/([&\/\?](p=|page[=\/-]))\d+/i, "$1"+pageNum).replace(/([_-])\d+\./i, "$1"+pageNum+"."):result;
  1027. }else{
  1028. return url.replace(/([&\/\?](p=|page[=\/-]))\d+/i, "$1"+pageNum).replace(/([_-])\d+\./i, "$1"+pageNum+".");
  1029. }
  1030. }
  1031.  
  1032. getNextLink(doc) {
  1033. let nextLink=null,page;
  1034. if(this.curSiteRule.pageElementByJs){
  1035. this.nextLinkHref="#";
  1036. return true;
  1037. }else if(this.curSiteRule.nextLinkByJs){
  1038. try{
  1039. let targetUrl=Function("doc",'"use strict";' + this.curSiteRule.nextLinkByJs)(doc);
  1040. if(targetUrl)nextLink={href:targetUrl};
  1041. }catch(e){
  1042. debug(e);
  1043. }
  1044. }else if(this.curSiteRule.nextLinkByUrl){
  1045. let targetUrl=this.curUrl.replace(new RegExp(this.curSiteRule.nextLinkByUrl[0],"i"), this.curSiteRule.nextLinkByUrl[1]);
  1046. if(targetUrl != this.curUrl){
  1047. let reps=targetUrl.match(/{.*?}/g);
  1048. if(reps){
  1049. reps.forEach(rep=>{
  1050. let code=rep.replace("{","").replace("}","");
  1051. let result=code.match(/^(\d+)\+1$/);
  1052. if(result){
  1053. result=parseInt(result[1])+1;
  1054. }else{
  1055. try{
  1056. result=Function('"use strict";return ' + code)();
  1057. }catch(e){
  1058. debug(e);
  1059. }
  1060. }
  1061. targetUrl=targetUrl.replace(rep, result);
  1062. });
  1063. }
  1064. }
  1065. nextLink={href:targetUrl};
  1066. }else if(this.curSiteRule.nextLink){
  1067. if(this.curSiteRule.nextLink!="0"){
  1068. nextLink=this.curSiteRule.type==0?getElementByXpath(this.curSiteRule.nextLink,doc,doc):doc.querySelector(this.curSiteRule.nextLink);
  1069. }
  1070. }else{
  1071. page=this.getPage(doc);
  1072. nextLink=page.next;
  1073. if(nextLink){
  1074. if(nextLink.tagName=="INPUT" && nextLink.parentNode.tagName=="FORM"){
  1075. let params=[];
  1076. let form=nextLink.parentNode;
  1077. [].forEach.call(form.querySelectorAll("input"), input=>{
  1078. params.push(input.name+'='+input.value);
  1079. });
  1080. nextLink.href=form.action+'?'+params.join('&');
  1081. }
  1082. if(nextLink.classList.contains("noClick") || nextLink.hasAttribute("disabled")){
  1083. this.nextLinkHref=false;
  1084. return null;
  1085. }else if(doc==document){
  1086. if((!nextLink.href || /^javascript:/.test(nextLink.href)) && !isVisible(nextLink, _unsafeWindow)){
  1087. this.nextLinkHref=false;
  1088. return null;
  1089. }else{
  1090. let nextLinkCs=_unsafeWindow.getComputedStyle(nextLink);
  1091. if(nextLinkCs.cursor=="not-allowed"){
  1092. this.nextLinkHref=false;
  1093. return null;
  1094. }
  1095. this.initNext=nextLink;
  1096. }
  1097. }
  1098. }
  1099. }
  1100. if(nextLink){
  1101. let needUrl=(this.curSiteRule.action==0 || this.curSiteRule.action==1);
  1102. let href=nextLink.getAttribute?nextLink.getAttribute("href"):nextLink.href;
  1103. if((href===""||href===null) && needUrl){
  1104. this.nextLinkHref=false;
  1105. }else if(/^(javascript:|#)/.test(href) && needUrl){
  1106. this.nextLinkHref=false;
  1107. }else{
  1108. this.nextLinkHref=(href && !/^(javascript:|#)/.test(href) && nextLink.href!=location.href)?this.canonicalUri(nextLink.href):"#";
  1109. if(this.nextLinkHref!="#" && (this.nextLinkHref==this.curUrl || this.nextLinkHref==this.curUrl+"#" || this.nextLinkHref==this.oldUrl || this.nextLinkHref==this.oldUrl+"#")){
  1110. this.nextLinkHref=false;
  1111. }else if(doc==document)debug(nextLink);
  1112. }
  1113. }else{
  1114. this.nextLinkHref=false;
  1115. }
  1116. this.preload();
  1117. return nextLink;
  1118. }
  1119.  
  1120. preload(){
  1121. if(!rulesData.preload)return;
  1122. if(!this.nextLinkHref || this.nextLinkHref=="#")return;
  1123. let self=this;
  1124. if(this.curSiteRule && this.curSiteRule.action==1){
  1125. if(!this.preloadIframe){
  1126. this.preloadIframe = document.createElement('iframe');
  1127. let iframe = this.preloadIframe;
  1128. iframe.name = 'pagetual-iframe';
  1129. iframe.width = '100%';
  1130. iframe.height = '0';
  1131. iframe.frameBorder = '0';
  1132. if(ruleParser.curSiteRule.sandbox!=false){
  1133. iframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  1134. }
  1135. iframe.style.cssText = 'margin:0!important;padding:0!important;visibility:hidden!important;';
  1136. iframe.addEventListener('load', function (e) {
  1137. try{
  1138. let doc=iframe.contentDocument || iframe.contentWindow.document;
  1139. var body = doc.body;
  1140. if (body && body.firstChild) {
  1141. self.lazyImgAction(body.children);
  1142. }
  1143. }catch(e){
  1144. return;
  1145. }
  1146. }, false);
  1147. document.body.appendChild(iframe);
  1148. }
  1149. this.preloadIframe.src = this.nextLinkHref;
  1150. }else{
  1151. _GM_xmlhttpRequest({
  1152. url: this.nextLinkHref,
  1153. method: 'GET',
  1154. overrideMimeType: 'text/html;charset='+document.charset,
  1155. headers: {
  1156. 'Referer': location.href
  1157. },
  1158. timeout: 5000,
  1159. onload: function(res) {
  1160. var doc=null;
  1161. try {
  1162. doc=document.implementation.createHTMLDocument('');
  1163. doc.documentElement.innerHTML=res.response;
  1164. var body = doc.body;
  1165. if (body && body.firstChild) {
  1166. self.lazyImgAction(body.children);
  1167. }
  1168. if(!self.preloadDiv){
  1169. self.preloadDiv = document.createElement('div');
  1170. self.preloadDiv.id = "pagetual-preload";
  1171. self.preloadDiv.style.cssText = 'display:none!important;';
  1172. document.body.appendChild(self.preloadDiv);
  1173. self.checkedImgs={};
  1174. }
  1175. [].forEach.call(doc.images, i=>{
  1176. let iSrc=i.src;
  1177. if(iSrc && !self.checkedImgs[iSrc]){
  1178. self.checkedImgs[iSrc] = true;
  1179. let img = document.createElement('img');
  1180. img.src = iSrc;
  1181. self.preloadDiv.appendChild(img);
  1182. }
  1183. });
  1184. }
  1185. catch (e) {
  1186. return;
  1187. }
  1188. }
  1189. });
  1190. }
  1191. }
  1192.  
  1193. getInsert(refresh) {
  1194. if(refresh){
  1195. this.insert=null;
  1196. }
  1197. if(this.insert && !refresh){
  1198. let parent=this.insert;
  1199. while(parent && parent.nodeName!="BODY"){
  1200. parent=parent.parentNode;
  1201. }
  1202. if(parent && parent.nodeName=="BODY"){
  1203. return this.insert;
  1204. }
  1205. }
  1206. if(this.curSiteRule.insert){
  1207. this.insert=this.curSiteRule.type==0?getElementByXpath(this.curSiteRule.insert,document):document.querySelector(this.curSiteRule.insert);
  1208. }else{
  1209. let pageElement=this.getPageElement(document, _unsafeWindow);
  1210. if(pageElement && pageElement.length>0){
  1211. var pELast = pageElement[pageElement.length - 1];
  1212. this.insert = pELast.nextSibling ? pELast.nextSibling : pELast.parentNode.appendChild(document.createTextNode(' '));
  1213. }
  1214. }
  1215. return this.insert;
  1216. }
  1217.  
  1218. pageInit(doc,eles){
  1219. let code=this.curSiteRule.pageInit;
  1220. if(code){
  1221. try{
  1222. Function("doc","eles",'"use strict";' + code)(doc,eles);
  1223. }catch(e){
  1224. debug(e);
  1225. }
  1226. }
  1227. }
  1228.  
  1229. pageAction(doc,eles){
  1230. let code=this.curSiteRule.pageAction;
  1231. if(code){
  1232. try{
  1233. Function("doc","eles",'"use strict";' + code)(doc,eles);
  1234. }catch(e){
  1235. debug(e);
  1236. }
  1237. }
  1238. this.lazyImgAction(eles);
  1239. this.openInNewTab(eles);
  1240. }
  1241.  
  1242. openInNewTab(eles){
  1243. if(rulesData.openInNewTab){
  1244. [].forEach.call(eles, ele=>{
  1245. if(ele.tagName=="A" && ele.href && !/^(mailto:|javascript:|#)/.test(ele.href)){
  1246. ele.setAttribute('target', '_blank');
  1247. }else{
  1248. [].forEach.call(ele.querySelectorAll('a[href]:not([href^="mailto:"]):not([href^="javascript:"]):not([href^="#"])'), a=>{
  1249. a.setAttribute('target', '_blank');
  1250. if (a.getAttribute('onclick') == 'atarget(this)') {
  1251. a.removeAttribute('onclick');
  1252. }
  1253. });
  1254. }
  1255. });
  1256. }
  1257. }
  1258.  
  1259. lazyImgAction(eles){
  1260. let setLazyImg=img=>{
  1261. let realSrc;
  1262. if(img.getAttribute("_src") && !img.src){
  1263. realSrc=img.getAttribute("_src");
  1264. }else if(img.dataset && img.dataset.original){
  1265. realSrc=img.dataset.original;
  1266. }else if(img.dataset && img.dataset.src){
  1267. realSrc=img.dataset.src;
  1268. }else if(img._lazyrias && img._lazyrias.srcset){
  1269. realSrc=img._lazyrias.srcset[img._lazyrias.srcset.length-1];
  1270. }else if(img.dataset && img.dataset.origFile){
  1271. realSrc=img.dataset.origFile;
  1272. }else if(img.srcset){
  1273. var srcs=img.srcset.split(/[xw],/),largeSize=0;
  1274. srcs.forEach(srci=>{
  1275. let srcInfo=srci.trim().split(" "),curSize=parseInt(srcInfo[1]);
  1276. if(srcInfo[1] && curSize>largeSize){
  1277. largeSize=curSize;
  1278. realSrc=srcInfo[0];
  1279. }
  1280. });
  1281. }
  1282. if(realSrc){
  1283. img.src=realSrc;
  1284. img.style.display="block";
  1285. }
  1286. };
  1287. [].forEach.call(eles, ele=>{
  1288. if(ele.tagName=="IMG"){
  1289. setLazyImg(ele);
  1290. }else{
  1291. [].forEach.call(ele.querySelectorAll("img"), img=>{
  1292. setLazyImg(img);
  1293. });
  1294. }
  1295. if(ele.tagName=="A" && ele.classList.contains("lazyload")){
  1296. if(ele.dataset.original){
  1297. ele.style.backgroundImage='url("'+ele.dataset.original+'")';
  1298. }
  1299. }else{
  1300. [].forEach.call(ele.querySelectorAll("a.lazyload"), a=>{
  1301. if(a.dataset.original){
  1302. a.style.backgroundImage='url("'+a.dataset.original+'")';
  1303. }
  1304. });
  1305. }
  1306. });
  1307. let lazyImgSrc=this.curSiteRule.lazyImgSrc;
  1308. if(lazyImgSrc){
  1309. [].forEach.call(eles, ele=>{
  1310. [].forEach.call(ele.querySelectorAll("img"), img=>{
  1311. if(img.getAttribute(lazyImgSrc)){
  1312. img.src=img.getAttribute(lazyImgSrc);
  1313. }
  1314. });
  1315. });
  1316. }
  1317. }
  1318.  
  1319. initPage(callback){
  1320. let self=this;
  1321. curPage=1;
  1322. //if(this.curSiteRule.url && !this.curSiteRule.singleUrl)return;
  1323. this.curSiteRule={};
  1324. this.pageDoc=document;
  1325. this.nextLinkHref=null;
  1326. this.curUrl=location.href;
  1327. this.getRule(()=>{
  1328. if(self.curSiteRule.enable==0){
  1329. debug("Stop as rule disable");
  1330. isPause=true;
  1331. return;
  1332. }
  1333. if(self.curSiteRule && !self.curSiteRule.singleUrl){
  1334. self.hpRules=self.hpRules.filter(item=>{return item&&item.url!=self.curSiteRule.url});
  1335. self.hpRules.unshift(self.curSiteRule);
  1336. if(self.hpRules.length>30){
  1337. self.hpRules.pop();
  1338. }
  1339. storage.setItem("hpRules", self.hpRules);
  1340. }
  1341. let css=self.curSiteRule.css;
  1342. if(css){
  1343. _GM_addStyle(css);
  1344. }
  1345. let code=self.curSiteRule.init;
  1346. if(code){
  1347. try{
  1348. Function('doc','win','"use strict";' + code)(null,null);
  1349. }catch(e){
  1350. debug(e);
  1351. }
  1352. }
  1353. self.getNextLink(document);
  1354. callback();
  1355. });
  1356. }
  1357.  
  1358. insertPage(doc, eles, url, callback, tried){
  1359. this.oldUrl=this.curUrl;
  1360. let oldTitle=this.pageDoc.title;
  1361. this.pageDoc=doc;
  1362. this.curUrl=url;
  1363. let nextLink=this.getNextLink(doc);
  1364. if(curPage==1 && !tried && !nextLink && this.curSiteRule.singleUrl && this.curSiteRule.pageElement && this.curSiteRule.action!=0){
  1365. this.curSiteRule.action=1;
  1366. this.curUrl=location.href;
  1367. return false;
  1368. }
  1369. if(callback)callback(eles);
  1370. this.getInsert();
  1371. var self=this,newEles=[];
  1372. if(!eles || eles.length==0 || !self.insert || !self.insert.parentNode){
  1373. }else{
  1374. this.pageInit(doc, eles);
  1375. [].forEach.call(eles, ele=>{
  1376. let newEle=ele.cloneNode(true);
  1377. let oldCanvass=ele.querySelectorAll("canvas");
  1378. let newCanvass=newEle.querySelectorAll("canvas");
  1379. for(let i=0;i<oldCanvass.length;i++){
  1380. let oldCanvas=oldCanvass[i];
  1381. let newCanvas=newCanvass[i];
  1382. newCanvas.getContext('2d').drawImage(oldCanvas, 0, 0);
  1383. }
  1384. if(self.curSiteRule.insertPos==2){
  1385. self.insert.appendChild(newEle);
  1386. }else{
  1387. self.insert.parentNode.insertBefore(newEle, self.insert);
  1388. }
  1389. newEles.push(newEle);
  1390. });
  1391. }
  1392. this.pageAction(doc, newEles);
  1393. if(this.oldUrl!=location.href){
  1394. let isJs=/^(javascript|#)/.test(this.oldUrl.replace(location.href,""));
  1395. if(rulesData.enableHistory && !isJs){
  1396. _unsafeWindow.history.replaceState(undefined, oldTitle, this.oldUrl);
  1397. document.title=oldTitle;
  1398. }
  1399. }
  1400. return true;
  1401. }
  1402. }
  1403. var ruleParser = new RuleParser();
  1404.  
  1405. function initConfig(){
  1406. _GM_registerMenuCommand(i18n(forceState==1?"enable":"disableSite"), ()=>{
  1407. storage.setItem("forceState_"+location.host, (forceState==1?0:1));
  1408. location.reload();
  1409. });
  1410. _GM_registerMenuCommand(i18n("update"), ()=>{
  1411. updateRules(()=>{
  1412. alert(i18n("updateSucc"));
  1413. location.reload();
  1414. },(rule,err)=>{
  1415. alert("Update "+rule.url+" rules fail!");
  1416. });
  1417. alert(i18n("beginUpdate"));
  1418. });
  1419.  
  1420. var configCon,insertPos;
  1421. if(ruleImportUrlReg.test(location.href)){
  1422. document.addEventListener("click", e=>{
  1423. if(e.target.tagName=="PRE"){
  1424. let nameAttr=e.target.getAttribute("name");
  1425. if(nameAttr=="pagetual" || nameAttr=="user-content-pagetual"){
  1426. let rules=e.target.innerText.trim().split("\n");
  1427. let diff=false;
  1428. for(let c=0;c<rules.length;c++){
  1429. let urlArr=rules[c].split("|"),url,type=1;
  1430. if(urlArr.length==1){
  1431. url=urlArr[0].trim();
  1432. if(!/^http/.test(url)){
  1433. alert("Wrong url, check again!");
  1434. return;
  1435. }
  1436. }else if(urlArr.length==2){
  1437. type=urlArr[0].trim();
  1438. url=urlArr[1].trim();
  1439. if(!/^http/.test(url)){
  1440. alert("Wrong url, check again!");
  1441. return;
  1442. }
  1443. }else{
  1444. break;
  1445. }
  1446. let maxId=0,hasUrl=false;
  1447. if(!rulesData.urls){
  1448. rulesData.urls=[];
  1449. maxId=1;
  1450. }else{
  1451. rulesData.urls.forEach(u=>{
  1452. if(maxId<u.id){
  1453. maxId=u.id;
  1454. }
  1455. if(u.url==url){
  1456. hasUrl=true;
  1457. }
  1458. });
  1459. if(hasUrl)break;
  1460. }
  1461. diff=true;
  1462. if(!rulesData.sort)rulesData.sort=[1];
  1463. rulesData.urls.push({id:maxId+1,url:url,type:type});
  1464. rulesData.sort.unshift(maxId+1);
  1465. }
  1466. if(!diff)return;
  1467. storage.setItem("rulesData", rulesData);
  1468.  
  1469. if(rulesData.urls)ruleUrls=ruleUrls.concat(rulesData.urls);
  1470. if(rulesData.sort){
  1471. let urls=[];
  1472. rulesData.sort.forEach(id=>{
  1473. for(let s=0;s<ruleUrls.length;s++){
  1474. if(id==ruleUrls[s].id){
  1475. urls.push(ruleUrls[s]);
  1476. break;
  1477. }
  1478. }
  1479. });
  1480. ruleUrls=urls;
  1481. }
  1482. alert("OK");
  1483. }
  1484. }
  1485. });
  1486. }
  1487. if(location.href==configPage){
  1488. _GM_addStyle(`
  1489. p>span:nth-child(1),p>span:nth-child(2),p>span:nth-child(3){
  1490. cursor: pointer;
  1491. user-select: none;
  1492. }
  1493. p>span:nth-child(1):hover,p>span:nth-child(2):hover,p>span:nth-child(3):hover{
  1494. color:red;
  1495. }
  1496. .updateDate{
  1497. cursor: pointer;
  1498. user-select: none;
  1499. }
  1500. .updateDate:hover{
  1501. color:red;
  1502. }
  1503. `);
  1504. document.querySelector("[name='user-content-click2import']").innerText=i18n("click2ImportRule")
  1505. configCon=document.querySelector(".markdown-body");
  1506. insertPos=configCon.querySelector("hr");
  1507.  
  1508. if(rulesData.urls && rulesData.urls.length>0){
  1509. document.querySelector("pre[name='user-content-pagetual']").style.display="none";
  1510. document.querySelector("p[name='user-content-click2import']").style.display="none";
  1511. }
  1512. }else return false;
  1513. class Rulebar {
  1514. init(ruleUrl){
  1515. this.ruleUrl=ruleUrl;
  1516. this.item=document.createElement("p");
  1517. this.item.title=ruleUrl.type==0?"AutoPagerize Rules":"Pagetual Rules";
  1518. this.item.dataset.id=this.ruleUrl.id;
  1519. let url=document.createElement("span");
  1520. url.innerHTML=ruleUrl.url;
  1521. let up=document.createElement("span");
  1522. up.innerHTML="↑ ";
  1523. up.title=i18n("sortTitle");
  1524. let down=document.createElement("span");
  1525. down.innerHTML="↓ ";
  1526. down.title=i18n("sortTitle");
  1527. let del=document.createElement("span");
  1528. del.innerHTML="× ";
  1529. up.onclick=e=>{
  1530. this.moveUp();
  1531. };
  1532. down.onclick=e=>{
  1533. this.moveDown();
  1534. };
  1535. del.onclick=e=>{
  1536. this.del();
  1537. };
  1538. this.item.appendChild(up);
  1539. this.item.appendChild(down);
  1540. this.item.appendChild(del);
  1541. this.item.appendChild(url);
  1542. configCon.insertBefore(this.item, insertPos);
  1543. }
  1544. saveSort(){
  1545. let sort=[];
  1546. [].forEach.call(this.item.parentNode.querySelectorAll("p[data-id]"), i=>{
  1547. sort.push(i.dataset.id);
  1548. });
  1549. rulesData.sort=sort;
  1550. storage.setItem("rulesData", rulesData);
  1551. }
  1552. moveUp(){
  1553. let preE=this.item.previousElementSibling;
  1554. if(preE.tagName=="P" && preE.children.length>1){
  1555. this.item.parentNode.insertBefore(this.item,preE);
  1556. this.saveSort();
  1557. }
  1558. }
  1559. moveDown(){
  1560. let nextE=this.item.nextElementSibling;
  1561. if(nextE.tagName=="P" && nextE.children.length>1){
  1562. this.item.parentNode.insertBefore(nextE,this.item);
  1563. this.saveSort();
  1564. }
  1565. }
  1566. del(){
  1567. if(this.ruleUrl.id<2){
  1568. alert(i18n("cantDel"));
  1569. }else if(window.confirm(i18n("confirmDel"))){
  1570. for(let u=0;u<rulesData.urls.length;u++){
  1571. if(this.ruleUrl.id==rulesData.urls[u].id){
  1572. rulesData.urls.splice(u,1);
  1573. break;
  1574. }
  1575. }
  1576. for(let u=0;u<rulesData.sort.length;u++){
  1577. if(this.ruleUrl.id==rulesData.sort[u]){
  1578. rulesData.sort.splice(u,1);
  1579. break;
  1580. }
  1581. }
  1582. storage.setItem("rulesData", rulesData);
  1583. ruleParser.rules=ruleParser.rules.filter(item=>{return item.from!=this.ruleUrl.id});
  1584. storage.setItem("rules", ruleParser.rules);
  1585. this.item.parentNode.removeChild(this.item);
  1586. //location.reload();
  1587. }
  1588. }
  1589. }
  1590. let updateP=document.createElement("p"),i=0;
  1591. let now=new Date().getTime(),inUpdate=false;
  1592.  
  1593.  
  1594. let pastDate=(new Date(updateDate)).toString(),passStr;
  1595. let passTime=(now-updateDate)/1000;
  1596. if(isNaN(passTime)){
  1597. passStr=i18n("firstUpdate");
  1598. }else if(passTime<60){
  1599. passStr=i18n("passSec", parseInt(passTime))+" ← "+i18n("click2update");
  1600. }else if(passTime<60*60){
  1601. passStr=i18n("passMin", parseInt(passTime/60))+" ← "+i18n("click2update");
  1602. }else if(passTime<60*60*24){
  1603. passStr=i18n("passHour", parseInt(passTime/3600))+" ← "+i18n("click2update");
  1604. }else{
  1605. passStr=i18n("passDay", parseInt(passTime/86400))+" ← "+i18n("click2update");
  1606. }
  1607.  
  1608.  
  1609. updateP.className="updateDate";
  1610. updateP.innerHTML=passStr;
  1611. updateP.title=i18n("update")+" - "+pastDate;
  1612. updateP.onclick=e=>{
  1613. updateRules(()=>{
  1614. alert(i18n("updateSucc"));
  1615. updateP.innerHTML=i18n("passSec", 0);
  1616. updateP.title=i18n("update");
  1617. },(rule,err)=>{
  1618. alert("Update "+rule.url+" rules fail!");
  1619. });
  1620. alert(i18n("beginUpdate"));
  1621. };
  1622. configCon.insertBefore(updateP, insertPos);
  1623. if(ruleUrls){
  1624. ruleUrls.forEach(ruleUrl=>{
  1625. var rulebar=new Rulebar();
  1626. rulebar.init(ruleUrl);
  1627. });
  1628. }
  1629. let customUrlsTitle=document.createElement("h2");
  1630. customUrlsTitle.innerHTML=i18n("customUrls");
  1631. configCon.insertBefore(customUrlsTitle, insertPos);
  1632. let customUrlsInput=document.createElement("textarea");
  1633. customUrlsInput.style.width="100%";
  1634. customUrlsInput.placeholder="0|http://wedata.net/databases/AutoPagerize/items_all.json";
  1635. configCon.insertBefore(customUrlsInput, insertPos);
  1636.  
  1637. let opacityTitle=document.createElement("h2");
  1638. opacityTitle.innerHTML=i18n("opacity");
  1639. let opacityInput=document.createElement("input");
  1640. opacityInput.value=rulesData.opacity*100;
  1641. opacityInput.type="number";
  1642. opacityInput.style.width="50px";
  1643. opacityInput.style.margin="0 0 0 10px";
  1644. opacityTitle.appendChild(opacityInput);
  1645. configCon.insertBefore(opacityTitle, insertPos);
  1646.  
  1647. function createCheckbox(innerText, val){
  1648. let title=document.createElement("h2");
  1649. title.innerHTML=innerText;
  1650. let input=document.createElement("input");
  1651. input.type="checkbox";
  1652. input.style.width="50px";
  1653. input.style.height="20px";
  1654. input.checked=val;
  1655. title.appendChild(input);
  1656. configCon.insertBefore(title, insertPos);
  1657. return input;
  1658. }
  1659.  
  1660. let hideBarInput=createCheckbox(i18n("hideBar"), rulesData.hideBar);
  1661. let dbClick2StopInput=createCheckbox(i18n("dbClick2Stop"), rulesData.dbClick2Stop);
  1662. let enableWhiteListInput=createCheckbox(i18n("autoRun"), rulesData.enableWhiteList!=true);
  1663. let enableDebugInput=createCheckbox(i18n("enableDebug"), rulesData.enableDebug!=false);
  1664. let enableHistoryInput=createCheckbox(i18n("enableHistory"), rulesData.enableHistory===true);
  1665. let openInNewTabInput=createCheckbox(i18n("openInNewTab"), rulesData.openInNewTab===true);
  1666. let initRunInput=createCheckbox(i18n("initRun"), rulesData.initRun!=false);
  1667. let preloadInput=createCheckbox(i18n("preload"), rulesData.preload);
  1668.  
  1669. let customRulesTitle=document.createElement("h2");
  1670. customRulesTitle.innerHTML=i18n("customRules")
  1671. configCon.insertBefore(customRulesTitle, insertPos);
  1672. let customRulesInput=document.createElement("textarea");
  1673. customRulesInput.style.width="100%";
  1674. customRulesInput.style.height="500px";
  1675. customRulesInput.placeholder=`[\n {\n "name":"yande",\n "action":"0",\n "url":"^https:\/\/yande\\.re\/",\n "pageElement":"ul#post-list-posts>li",\n "nextLink":"a.next_page",\n "css":".javascript-hide {display: inline-block !important;}"\n },\n {\n "name":"tieba",\n "action":"1",\n "url":"^https:\/\/tieba\\.baidu.com\/f\\?kw=",\n "pageElement":"ul#thread_list>li",\n "nextLink":".next.pagination-item "\n }\n]`;
  1676. customRulesInput.value=getFormatJSON(ruleParser.customRules);
  1677. configCon.insertBefore(customRulesInput, insertPos);
  1678. let saveBtn=document.createElement("button");
  1679. saveBtn.innerHTML=i18n("save");
  1680. saveBtn.style.width="100%";
  1681. configCon.insertBefore(saveBtn, insertPos);
  1682. saveBtn.onclick=e=>{
  1683. try{
  1684. storage.setItem("hpRules", []);
  1685. if(customRulesInput.value==""){
  1686. storage.setItem("customRules", "");
  1687. }else{
  1688. let customRules=JSON.parse(customRulesInput.value);
  1689. if(Array && Array.isArray && !Array.isArray(customRules)){
  1690. alert("Rules must be a Array!");
  1691. return;
  1692. }
  1693. debug(customRules);
  1694. storage.setItem("customRules", customRules);
  1695. }
  1696. }catch(e){
  1697. debug(e);
  1698. alert("JSON error, check again!");
  1699. return;
  1700. }
  1701. rulesData.opacity=opacityInput.value/100;
  1702. rulesData.hideBar=hideBarInput.checked;
  1703. rulesData.dbClick2Stop=dbClick2StopInput.checked;
  1704. rulesData.enableWhiteList=!enableWhiteListInput.checked;
  1705. rulesData.enableDebug=enableDebugInput.checked;
  1706. rulesData.enableHistory=enableHistoryInput.checked;
  1707. rulesData.openInNewTab=openInNewTabInput.checked;
  1708. rulesData.initRun=initRunInput.checked;
  1709. rulesData.preload=preloadInput.checked;
  1710. storage.setItem("rulesData", rulesData);
  1711. let customUrls=customUrlsInput.value.trim();
  1712. if(customUrls){
  1713. customUrls=customUrls.split(/\n/);
  1714. for(let c=0;c<customUrls.length;c++){
  1715. let urlArr=customUrls[c].split("|"),url,type=1;
  1716. if(urlArr.length==1){
  1717. url=urlArr[0].trim();
  1718. if(!/^http/.test(url)){
  1719. alert("Wrong url, check again!");
  1720. return;
  1721. }
  1722. }else if(urlArr.length==2){
  1723. type=urlArr[0].trim();
  1724. url=urlArr[1].trim();
  1725. if(!/^http/.test(url)){
  1726. alert("Wrong url, check again!");
  1727. return;
  1728. }
  1729. }else{
  1730. break;
  1731. }
  1732. let maxId=0,hasUrl=false;
  1733. if(!rulesData.urls){
  1734. rulesData.urls=[];
  1735. maxId=1;
  1736. }else{
  1737. rulesData.urls.forEach(u=>{
  1738. if(maxId<u.id){
  1739. maxId=u.id;
  1740. }
  1741. if(u.url==url){
  1742. hasUrl=true;
  1743. }
  1744. });
  1745. if(hasUrl)break;
  1746. }
  1747. if(!rulesData.sort)rulesData.sort=[1];
  1748. rulesData.urls.push({id:maxId+1,url:url,type:type});
  1749. rulesData.sort.push(maxId+1);
  1750. storage.setItem("rulesData", rulesData);
  1751. }
  1752. }
  1753. alert("The settings are saved");
  1754. location.reload();
  1755. };
  1756. return true;
  1757. }
  1758.  
  1759. var inUpdate=false;
  1760. function updateRules(success,fail){
  1761. if(inUpdate)return;
  1762. inUpdate=true;
  1763. let ruleIndex=ruleUrls.length-1;
  1764. storage.setItem("hpRules", []);
  1765. function addNextRule(){
  1766. if(ruleIndex<0){
  1767. let now=new Date().getTime();
  1768. storage.setItem("ruleLastUpdate", now);
  1769. storage.setItem("rules", ruleParser.rules);
  1770. inUpdate=false;
  1771. success();
  1772. }else{
  1773. let rule=ruleUrls[ruleIndex--];
  1774. ruleParser.addRuleByUrl(rule.url, rule.type, rule.id, (json,err)=>{
  1775. if(!json){
  1776. fail(rule,err);
  1777. }
  1778. addNextRule();
  1779. })
  1780. }
  1781. }
  1782. addNextRule();
  1783. }
  1784.  
  1785. function objIsArr(obj) {
  1786. return obj &&
  1787. typeof obj === 'object' &&
  1788. typeof obj.length === 'number' &&
  1789. !(obj.propertyIsEnumerable('length'));
  1790. }
  1791.  
  1792. function isVisible(el, win) {
  1793. if(!el.offsetParent)return false;
  1794. var loopable = true,
  1795. visible = el.tagName && win.getComputedStyle(el).display != 'none' && win.getComputedStyle(el).visibility != 'hidden';
  1796. while(loopable && visible) {
  1797. el = el.parentNode;
  1798.  
  1799. if(el && el.tagName && el.tagName!="BODY") {
  1800. visible = win.getComputedStyle(el).display != 'none' && win.getComputedStyle(el).visibility != 'hidden';
  1801. }else {
  1802. loopable = false;
  1803. }
  1804. }
  1805. return visible;
  1806. }
  1807.  
  1808. function getElementTop(ele) {
  1809. var actualTop = ele.offsetTop;
  1810. var current = ele.offsetParent;
  1811. while (current !== null) {
  1812. actualTop += current.offsetTop;
  1813. current = current.offsetParent;
  1814. }
  1815. return actualTop;
  1816. }
  1817.  
  1818. function getFormatJSON(obj){
  1819. if(!objIsArr(obj))return "";
  1820. let ret="[\n";
  1821. let len=obj.length,i=0,isLast;
  1822. obj.forEach(item=>{
  1823. ret+=" {\n";
  1824. let iLen=Object.keys(item).length,j=0;
  1825. for(let key in item){
  1826. isLast=(++j)==iLen;
  1827. let value=item[key];
  1828. if(objIsArr(value)){
  1829. let vstr="[",v=0,vIsLast=false;
  1830. value.forEach(vi=>{
  1831. vIsLast=(++v)==value.length;
  1832. vstr+="\""+vi.replace(/\\/g,"\\\\")+"\""+(vIsLast?"":",");
  1833. });
  1834. vstr+="]";
  1835. ret+=" \""+key+"\":"+vstr+""+(isLast?"":",")+"\n";
  1836. }else{
  1837. if(typeof value=="string"){
  1838. value=value.replace(/\\/g,"\\\\").replace(/"/g,"\\\"");
  1839. value="\""+value+"\"";
  1840. }
  1841. ret+=" \""+key+"\":"+value+""+(isLast?"":",")+"\n";
  1842. }
  1843. }
  1844. isLast=(++i)==len;
  1845. ret+=" }"+(isLast?"":",")+"\n";
  1846. });
  1847. ret+="]";
  1848. return ret;
  1849. }
  1850.  
  1851. function initRules(callback) {
  1852. /*0 wedata格式,1 pagetual格式*/
  1853. ruleUrls=[
  1854. {
  1855. id:1,
  1856. url:'http://wedata.net/databases/AutoPagerize/items_all.json',
  1857. type:0,
  1858. }
  1859. ];var i=0,j=0;
  1860.  
  1861. ruleParser.initSavedRules(()=>{
  1862. storage.getItem("rulesData", data=>{
  1863. if(data){
  1864. rulesData=data;
  1865. if(data.urls)ruleUrls=ruleUrls.concat(data.urls);
  1866. if(data.sort){
  1867. let urls=[];
  1868. data.sort.forEach(id=>{
  1869. for(let s=0;s<ruleUrls.length;s++){
  1870. if(id==ruleUrls[s].id){
  1871. urls.push(ruleUrls[s]);
  1872. break;
  1873. }
  1874. }
  1875. });
  1876. ruleUrls=urls;
  1877. }
  1878. }
  1879. if(typeof(rulesData.opacity)=="undefined"){
  1880. rulesData.opacity=0.3;
  1881. }
  1882. if(typeof(rulesData.hideBar)=="undefined"){
  1883. rulesData.hideBar=false;
  1884. }
  1885. if(typeof(rulesData.dbClick2Stop)=="undefined"){
  1886. rulesData.dbClick2Stop=true;
  1887. }
  1888. if(typeof(rulesData.enableWhiteList)=="undefined"){
  1889. rulesData.enableWhiteList=false;
  1890. }
  1891. if(typeof(rulesData.enableHistory)=="undefined"){
  1892. rulesData.enableHistory=false;
  1893. }
  1894. if(typeof(rulesData.openInNewTab)=="undefined"){
  1895. rulesData.openInNewTab=false;
  1896. }
  1897. if(typeof(rulesData.enableDebug)=="undefined"){
  1898. rulesData.enableDebug=true;
  1899. }
  1900. if(typeof(rulesData.initRun)=="undefined"){
  1901. rulesData.initRun=true;
  1902. }
  1903. if(typeof(rulesData.preload)=="undefined"){
  1904. rulesData.preload=false;
  1905. }
  1906. enableDebug=rulesData.enableDebug;
  1907. storage.getItem("forceState_"+location.host, v=>{
  1908. storage.getItem("ruleLastUpdate", date=>{
  1909. if(typeof(v)=="undefined"){
  1910. v=(rulesData.enableWhiteList?1:0);
  1911. }
  1912. forceState=v;
  1913. updateDate=date;
  1914. if(initConfig())return;
  1915. if(forceState==1)return;
  1916. let now=new Date().getTime();
  1917. if(!date || now-date>2*24*60*60*1000){
  1918. //storage.setItem("ruleLastUpdate", now);
  1919. updateRules(()=>{
  1920. //callback();
  1921. },(rule,err)=>{});
  1922. }
  1923. callback();
  1924. });
  1925. });
  1926. });
  1927. });
  1928. }
  1929.  
  1930. function requestDoc(url, callback){
  1931. _GM_xmlhttpRequest({
  1932. url: url,
  1933. method: 'GET',
  1934. overrideMimeType: 'text/html;charset='+document.charset,
  1935. headers: {
  1936. 'Referer': location.href
  1937. },
  1938. timeout: 5000,
  1939. onload: function(res) {
  1940. var doc=null;
  1941. try {
  1942. doc=document.implementation.createHTMLDocument('');
  1943. doc.documentElement.innerHTML=res.response;
  1944. }
  1945. catch (e) {
  1946. debug('parse error'+e.toString());
  1947. }
  1948. let pageElement=ruleParser.getPageElement(doc);
  1949. if(inCors && (!pageElement || pageElement.length==0)){
  1950. ruleParser.curSiteRule.pageElement=allOfBody;
  1951. pageElement=ruleParser.getPageElement(doc);
  1952. ruleParser.getInsert(true);
  1953. }
  1954. //只有1的話怕不是圖片哦
  1955. ruleParser.curSiteRule.action=0;
  1956. if(pageElement && (pageElement.length>1 || (pageElement.length==1 && pageElement[0].tagName!="IMG") )){
  1957. let result=ruleParser.insertPage(doc, pageElement, url, callback, false);
  1958. if(!result){
  1959. ruleParser.curSiteRule.action=1;
  1960. requestFromIframe(url, (doc, eles)=>{
  1961. loadPageOver();
  1962. if(eles){
  1963. ruleParser.insertPage(doc, eles, url, callback, true);
  1964. }
  1965. });
  1966. }
  1967. }else if(ruleParser.curSiteRule.singleUrl || curPage==1){
  1968. ruleParser.curSiteRule.action=1;
  1969. requestFromIframe(url, (doc, eles)=>{
  1970. loadPageOver();
  1971. if(eles){
  1972. ruleParser.insertPage(doc, eles, url, callback, true);
  1973. }
  1974. });
  1975. }else{
  1976. isPause=true;
  1977. callback(false);
  1978. }
  1979. },
  1980. onerror: function(e){
  1981. debug(e);
  1982. callback(false);
  1983. },
  1984. ontimeout: function(e){
  1985. debug(e);
  1986. callback(false);
  1987. }
  1988. });
  1989. }
  1990.  
  1991. var failFromIframe=0;
  1992. var inCors=false;
  1993. function requestFromIframe(url, callback){
  1994. let orgPage,curPage;
  1995. let iframe = document.createElement('iframe');
  1996. iframe.name = 'pagetual-iframe';
  1997. iframe.width = '100%';
  1998. iframe.height = '0';
  1999. iframe.frameBorder = '0';
  2000. if(ruleParser.curSiteRule.sandbox!=false){
  2001. iframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  2002. }
  2003. iframe.style.cssText = 'margin:0!important;padding:0!important;visibility:hidden!important;';
  2004. iframe.addEventListener("load", e=>{
  2005. let waitTime=500,checkEval;
  2006. if(ruleParser.curSiteRule.wait){
  2007. if(isNaN(ruleParser.curSiteRule.wait)){
  2008. try{
  2009. checkEval=Function("doc",'"use strict";' + ruleParser.curSiteRule.wait);
  2010. }catch(e){
  2011. debug(e);
  2012. }
  2013. }else{
  2014. waitTime=ruleParser.curSiteRule.wait;
  2015. }
  2016. }
  2017. setTimeout(()=>{
  2018. let tryTimes=0;
  2019. function checkIframe(){
  2020. try{
  2021. let doc=iframe.contentDocument || iframe.contentWindow.document;
  2022. let eles=ruleParser.getPageElement(doc, iframe.contentWindow);
  2023. if(eles && eles.length>0 && (!checkEval || checkEval(doc))){
  2024. callback(doc, eles);
  2025. }else if(tryTimes++ < 10){
  2026. setTimeout(()=>{
  2027. checkIframe();
  2028. },waitTime);
  2029. return;
  2030. }else{
  2031. if(failFromIframe++ > 2){
  2032. failFromIframe=0;
  2033. debug("Stop as failFromIframe");
  2034. isPause=true;
  2035. callback(false, false);
  2036. }else{
  2037. callback(false, false);
  2038. }
  2039. }
  2040. }catch(e){
  2041. debug("Stop as cors");
  2042. inCors=true;
  2043. //isPause=true;
  2044. if(!ruleParser.curSiteRule.pageElement){
  2045. ruleParser.curSiteRule.pageElement=allOfBody;
  2046. ruleParser.getInsert(true);
  2047. }
  2048. ruleParser.curSiteRule.action=0;
  2049. ruleParser.nextLinkHref=url;
  2050. callback(false, false);
  2051. nextPage();
  2052. }
  2053. document.body.removeChild(iframe);
  2054. }
  2055. checkIframe();
  2056. },waitTime);
  2057. });
  2058. iframe.src=url;
  2059. document.body.appendChild(iframe);
  2060. }
  2061.  
  2062. function initPage(){
  2063. ruleParser.initPage(()=>{
  2064. if(ruleParser.nextLinkHref){
  2065. initView();
  2066. let isJs=/^(javascript|#)/.test(ruleParser.nextLinkHref.replace(location.href,""));
  2067. if(!isJs){
  2068. let inForce=(forceState == 2 || forceState == 3);
  2069. _GM_registerMenuCommand(i18n(inForce?"cancelForceIframe":"forceIframe"), ()=>{
  2070. if(inForce){
  2071. storage.setItem("forceState_"+location.host, "");
  2072. }else{
  2073. let _state=confirm(i18n("forceAllBody"))?2:3;
  2074. storage.setItem("forceState_"+location.host, _state);
  2075. }
  2076. location.reload();
  2077. });
  2078. }
  2079. _GM_registerMenuCommand(i18n("loadNow"), ()=>{
  2080. let loadNum=window.prompt(i18n("loadConfirm"), "1");
  2081. if(loadNum==="" || loadNum===null)return;
  2082. autoLoadNum=Math.abs(parseInt(loadNum));
  2083. nextPage();
  2084. });
  2085. }
  2086. initListener();
  2087. if(rulesData.initRun && ruleParser.curSiteRule.initRun!=0)nextPage();
  2088. });
  2089. }
  2090.  
  2091. function initView(){
  2092. _GM_addStyle(`
  2093. .pagetual_pageBar.stop {
  2094. -webkit-filter: invert(100%);
  2095. filter: invert(100%);
  2096. opacity: 1!important;
  2097. }
  2098. .pagetual_pageBar.hide {
  2099. display: none!important;
  2100. }
  2101. .pagetual_pageBar:hover {
  2102. opacity: 1!important;
  2103. }
  2104. .pagetual_pageBar span {
  2105. vertical-align: super;
  2106. }
  2107.  
  2108. .pagetual_pageBar span>svg:hover {
  2109. animation: touhouAni 1s infinite;
  2110. }
  2111. .pagetual_pageBar .pagetual_pageNum{
  2112. color: #55555f;
  2113. }
  2114. .pagetual_pageBar .pagetual_pageNum:hover{
  2115. color: #ff6464;
  2116. }
  2117.  
  2118. @keyframes touhouAni{
  2119. from {transform: rotate(0deg) scale3d(1.2, 1.2, 1.2);}
  2120. to {transform: rotate(360deg);}
  2121. }
  2122. @-webkit-keyframes touhouAni{
  2123. from {transform: rotate(0deg) scale3d(1.2, 1.2, 1.2);}
  2124. to {transform: rotate(360deg);}
  2125. }
  2126. .pagetual_loading {
  2127. width: 50px;
  2128. height: 50px;
  2129. margin: 10px auto;
  2130. border-radius: 100%;
  2131. -webkit-animation: pagetual_loading_scaleout 1.0s infinite ease-in-out;
  2132. animation: pagetual_loading_scaleout 1.0s infinite ease-in-out;
  2133. }
  2134. @-webkit-keyframes pagetual_loading_scaleout {
  2135. 0% { -webkit-transform: scale(0.0) }
  2136. 100% {
  2137. -webkit-transform: scale(1.0);
  2138. opacity: 0;
  2139. }
  2140. }
  2141. @keyframes pagetual_loading_scaleout {
  2142. 0% {
  2143. transform: scale(0.0);
  2144. -webkit-transform: scale(0.0);
  2145. } 100% {
  2146. transform: scale(1.0);
  2147. -webkit-transform: scale(1.0);
  2148. opacity: 0;
  2149. }
  2150. }
  2151. `);
  2152. pageBarStyle=`opacity:${rulesData.opacity};display:${rulesData.opacity==0?"none":"block"};padding:0;box-shadow: 0px 0px 10px 0px #000000aa;border-radius: 20px;background-color: rgb(240 240 240 / 80%);font-size: 30px;visibility: visible; position: initial; width: auto; height: 30px; float: none; clear: both; margin: 5px auto; text-align: center;`;
  2153. }
  2154. var loadingDiv=document.createElement("div");
  2155. loadingDiv.style.cssText="cy: initial;d: initial;dominant-baseline: initial;empty-cells: initial;fill: initial;fill-opacity: initial;fill-rule: initial;filter: initial;flex: initial;flex-flow: initial;float: initial;flood-color: initial;flood-opacity: initial;grid: initial;grid-area: initial;height: initial;hyphens: initial;image-orientation: initial;image-rendering: initial;inline-size: initial;inset-block: initial;inset-inline: initial;isolation: initial;letter-spacing: initial;lighting-color: initial;line-break: initial;list-style: initial;margin-block: initial;margin: 0px auto;margin-inline: initial;marker: initial;mask: initial;mask-type: initial;max-block-size: initial;max-height: initial;max-inline-size: initial;max-width: initial;min-block-size: initial;min-height: initial;min-inline-size: initial;min-width: initial;mix-blend-mode: initial;object-fit: initial;object-position: initial;offset: initial;opacity: initial;order: initial;origin-trial-test-property: initial;orphans: initial;outline: initial;outline-offset: initial;overflow-anchor: initial;overflow-clip-margin: initial;overflow-wrap: initial;overflow: initial;overscroll-behavior-block: initial;overscroll-behavior-inline: initial;overscroll-behavior: initial;padding-block: initial;padding: initial;padding-inline: initial;page: initial;page-orientation: initial;paint-order: initial;perspective: initial;perspective-origin: initial;pointer-events: initial;position: initial;quotes: initial;r: initial;resize: initial;ruby-position: initial;rx: initial;ry: initial;scroll-behavior: initial;scroll-margin-block: initial;scroll-margin: initial;scroll-margin-inline: initial;scroll-padding-block: initial;scroll-padding: initial;scroll-padding-inline: initial;scroll-snap-align: initial;scroll-snap-stop: initial;scroll-snap-type: initial;scrollbar-gutter: initial;shape-image-threshold: initial;shape-margin: initial;shape-outside: initial;shape-rendering: initial;size: initial;speak: initial;stop-color: initial;stop-opacity: initial;stroke: initial;stroke-dasharray: initial;stroke-dashoffset: initial;stroke-linecap: initial;stroke-linejoin: initial;stroke-miterlimit: initial;stroke-opacity: initial;stroke-width: initial;tab-size: initial;table-layout: initial;text-align: initial;text-align-last: initial;text-anchor: initial;text-combine-upright: initial;text-decoration: initial;text-decoration-skip-ink: initial;text-indent: initial;text-overflow: initial;text-shadow: initial;text-size-adjust: initial;text-transform: initial;text-underline-offset: initial;text-underline-position: initial;touch-action: initial;transform: initial;transform-box: initial;transform-origin: initial;transform-style: initial;transition: initial;user-select: initial;vector-effect: initial;vertical-align: initial;visibility: initial;border-spacing: initial;-webkit-border-image: initial;-webkit-box-align: initial;-webkit-box-decoration-break: initial;-webkit-box-direction: initial;-webkit-box-flex: initial;-webkit-box-ordinal-group: initial;-webkit-box-orient: initial;-webkit-box-pack: initial;-webkit-box-reflect: initial;-webkit-highlight: initial;-webkit-hyphenate-character: initial;-webkit-line-break: initial;-webkit-line-clamp: initial;-webkit-mask-box-image: initial;-webkit-mask: initial;-webkit-mask-composite: initial;-webkit-perspective-origin-x: initial;-webkit-perspective-origin-y: initial;-webkit-print-color-adjust: initial;-webkit-rtl-ordering: initial;-webkit-ruby-position: initial;-webkit-tap-highlight-color: initial;-webkit-text-combine: initial;-webkit-text-decorations-in-effect: initial;-webkit-text-emphasis: initial;-webkit-text-emphasis-position: initial;-webkit-text-fill-color: initial;-webkit-text-security: initial;-webkit-text-stroke: initial;-webkit-transform-origin-x: initial;-webkit-transform-origin-y: initial;-webkit-transform-origin-z: initial;-webkit-user-drag: initial;-webkit-user-modify: initial;white-space: initial;widows: initial;width: initial;will-change: initial;word-break: initial;word-spacing: initial;x: initial;y: initial;z-index: 2147483647;";
  2156. loadingDiv.innerHTML=`<p style="display: block; position: initial; margin: auto auto 5px auto; shape-rendering: auto; vertical-align: middle; visibility: visible; width: initial; height: initial; text-align: center; color: #6e6e6e;">${i18n("loadingText")}</p><div class="pagetual_loading"><svg width="50" height="50" style="margin-left:-25px;position:absolute;cursor: pointer;width: 50px;height: 50px;vertical-align: middle;fill: currentColor;overflow: hidden;" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6364"><path d="M296 440c-44.1 0-80 35.9-80 80s35.9 80 80 80 80-35.9 80-80-35.9-80-80-80z" fill="#6e6e6e" p-id="6365"></path><path d="M960 512c0-247-201-448-448-448S64 265 64 512c0 1.8 0.1 3.5 0.1 5.3 0 0.9-0.1 1.8-0.1 2.7h0.2C68.5 763.3 267.7 960 512 960c236.2 0 430.1-183.7 446.7-415.7 0.1-0.8 0.1-1.6 0.2-2.3 0.4-4.6 0.5-9.3 0.7-13.9 0.1-2.7 0.4-5.3 0.4-8h-0.2c0-2.8 0.2-5.4 0.2-8.1z m-152 8c0 44.1-35.9 80-80 80s-80-35.9-80-80 35.9-80 80-80 80 35.9 80 80zM512 928C284.4 928 99 744.3 96.1 517.3 97.6 408.3 186.6 320 296 320c110.3 0 200 89.7 200 200 0 127.9 104.1 232 232 232 62.9 0 119.9-25.2 161.7-66-66 142.7-210.4 242-377.7 242z" fill="#6e6e6e" p-id="6366"></path></svg></div>`;
  2157. var loadingCSS=`display: block; position: initial; margin: auto auto 5px auto; shape-rendering: auto; vertical-align: middle; visibility: visible; width: initial; height: initial; text-align: center; color: #6e6e6e;`;
  2158.  
  2159. var upSvg=`<svg width="30" height="30" style="position:absolute;cursor: pointer;margin: 0 -45px;width: 30px;height: 30px;vertical-align: middle;fill: currentColor;overflow: hidden;" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6364"><path d="M296 440c-44.1 0-80 35.9-80 80s35.9 80 80 80 80-35.9 80-80-35.9-80-80-80z" fill="#604b4a" p-id="6365"></path><path d="M960 512c0-247-201-448-448-448S64 265 64 512c0 1.8 0.1 3.5 0.1 5.3 0 0.9-0.1 1.8-0.1 2.7h0.2C68.5 763.3 267.7 960 512 960c236.2 0 430.1-183.7 446.7-415.7 0.1-0.8 0.1-1.6 0.2-2.3 0.4-4.6 0.5-9.3 0.7-13.9 0.1-2.7 0.4-5.3 0.4-8h-0.2c0-2.8 0.2-5.4 0.2-8.1z m-152 8c0 44.1-35.9 80-80 80s-80-35.9-80-80 35.9-80 80-80 80 35.9 80 80zM512 928C284.4 928 99 744.3 96.1 517.3 97.6 408.3 186.6 320 296 320c110.3 0 200 89.7 200 200 0 127.9 104.1 232 232 232 62.9 0 119.9-25.2 161.7-66-66 142.7-210.4 242-377.7 242z" fill="#604b4a" p-id="6366"></path></svg>`;
  2160. var upSvgCSS=`position:absolute;cursor: pointer;margin: 0 -45px;width: 30px;height: 30px;vertical-align: middle;fill: currentColor;overflow: hidden;`;
  2161. var downSvg=`<svg width="30" height="30" style="position:absolute;cursor: pointer;margin: 0 15px;width: 30px;height: 30px;vertical-align: middle;fill: currentColor;overflow: hidden;transform: rotate(180deg);" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6364"><path d="M296 440c-44.1 0-80 35.9-80 80s35.9 80 80 80 80-35.9 80-80-35.9-80-80-80z" fill="#604b4a" p-id="6365"></path><path d="M960 512c0-247-201-448-448-448S64 265 64 512c0 1.8 0.1 3.5 0.1 5.3 0 0.9-0.1 1.8-0.1 2.7h0.2C68.5 763.3 267.7 960 512 960c236.2 0 430.1-183.7 446.7-415.7 0.1-0.8 0.1-1.6 0.2-2.3 0.4-4.6 0.5-9.3 0.7-13.9 0.1-2.7 0.4-5.3 0.4-8h-0.2c0-2.8 0.2-5.4 0.2-8.1z m-152 8c0 44.1-35.9 80-80 80s-80-35.9-80-80 35.9-80 80-80 80 35.9 80 80zM512 928C284.4 928 99 744.3 96.1 517.3 97.6 408.3 186.6 320 296 320c110.3 0 200 89.7 200 200 0 127.9 104.1 232 232 232 62.9 0 119.9-25.2 161.7-66-66 142.7-210.4 242-377.7 242z" fill="#604b4a" p-id="6366"></path></svg>`;
  2162. var downSvgCSS=`position:absolute;cursor: pointer;margin: 0 15px;width: 30px;height: 30px;vertical-align: middle;fill: currentColor;overflow: hidden;transform: rotate(180deg);`;
  2163. var pageBarStyle;
  2164. var initStyle=`display: contents;right: unset;left: unset;top: unset;bottom: unset;inset: unset;clear: both;cy: initial;d: initial;dominant-baseline: initial;empty-cells: initial;fill: initial;fill-opacity: initial;fill-rule: initial;filter: initial;flex: initial;flex-flow: initial;float: initial;flood-color: initial;flood-opacity: initial;grid: initial;grid-area: initial;height: initial;hyphens: initial;image-orientation: initial;image-rendering: initial;inline-size: initial;inset-block: initial;inset-inline: initial;isolation: initial;letter-spacing: initial;lighting-color: initial;line-break: initial;list-style: initial;margin-block: initial;margin: 0px auto;margin-inline: initial;marker: initial;mask: initial;mask-type: initial;max-block-size: initial;max-height: initial;max-inline-size: initial;max-width: initial;min-block-size: initial;min-height: initial;min-inline-size: initial;min-width: initial;mix-blend-mode: initial;object-fit: initial;object-position: initial;offset: initial;opacity: initial;order: initial;orphans: initial;outline: initial;outline-offset: initial;overflow-anchor: initial;overflow-clip-margin: initial;overflow-wrap: initial;overflow: initial;overscroll-behavior-block: initial;overscroll-behavior-inline: initial;overscroll-behavior: initial;padding-block: initial;padding: initial;padding-inline: initial;page: initial;page-orientation: initial;paint-order: initial;perspective: initial;perspective-origin: initial;pointer-events: initial;position: relative;quotes: initial;r: initial;resize: initial;ruby-position: initial;rx: initial;ry: initial;scroll-behavior: initial;scroll-margin-block: initial;scroll-margin: initial;scroll-margin-inline: initial;scroll-padding-block: initial;scroll-padding: initial;scroll-padding-inline: initial;scroll-snap-align: initial;scroll-snap-stop: initial;scroll-snap-type: initial;scrollbar-gutter: initial;shape-image-threshold: initial;shape-margin: initial;shape-outside: initial;shape-rendering: initial;size: initial;speak: initial;stop-color: initial;stop-opacity: initial;stroke: initial;stroke-dasharray: initial;stroke-dashoffset: initial;stroke-linecap: initial;stroke-linejoin: initial;stroke-miterlimit: initial;stroke-opacity: initial;stroke-width: initial;tab-size: initial;table-layout: initial;text-align: initial;text-align-last: initial;text-anchor: initial;text-combine-upright: initial;text-decoration: initial;text-decoration-skip-ink: initial;text-indent: initial;text-overflow: initial;text-shadow: initial;text-size-adjust: initial;text-transform: initial;text-underline-offset: initial;text-underline-position: initial;touch-action: initial;transform: initial;transform-box: initial;transform-origin: initial;transform-style: initial;transition: initial;user-select: initial;vector-effect: initial;vertical-align: initial;visibility: initial;border-spacing: initial;-webkit-border-image: initial;-webkit-box-align: initial;-webkit-box-decoration-break: initial;-webkit-box-direction: initial;-webkit-box-flex: initial;-webkit-box-ordinal-group: initial;-webkit-box-orient: initial;-webkit-box-pack: initial;-webkit-box-reflect: initial;-webkit-highlight: initial;-webkit-hyphenate-character: initial;-webkit-line-break: initial;-webkit-line-clamp: initial;-webkit-mask-box-image: initial;-webkit-mask: initial;-webkit-mask-composite: initial;-webkit-perspective-origin-x: initial;-webkit-perspective-origin-y: initial;-webkit-print-color-adjust: initial;-webkit-rtl-ordering: initial;-webkit-ruby-position: initial;-webkit-tap-highlight-color: initial;-webkit-text-combine: initial;-webkit-text-decorations-in-effect: initial;-webkit-text-emphasis: initial;-webkit-text-emphasis-position: initial;-webkit-text-fill-color: initial;-webkit-text-security: initial;-webkit-text-stroke: initial;-webkit-transform-origin-x: initial;-webkit-transform-origin-y: initial;-webkit-transform-origin-z: initial;-webkit-user-drag: initial;-webkit-user-modify: initial;white-space: initial;widows: initial;width: initial;will-change: initial;word-break: initial;word-spacing: initial;x: initial;y: initial;`;
  2165. var pageTextStyle=`line-height: 30px;text-decoration: none;user-select: none;visibility: visible;position: initial;width: auto;height: auto;float: none;clear: both;margin: 0px auto;text-align: center;display: inline;font-weight: bold;font-style: normal;font-size: 16px;letter-spacing: initial;vertical-align: super;color: rgb(85, 85, 95);`;
  2166.  
  2167. var isPause=false,isLoading=false,curPage=1,forceState=0,bottomGap=1000,autoLoadNum=-1;
  2168.  
  2169. function changeStop(stop, hide){
  2170. isPause=stop;
  2171. [].forEach.call(document.querySelectorAll(".pagetual_pageBar"), bar=>{
  2172. if(isPause){
  2173. bar.classList.add("stop");
  2174. if(hide)bar.classList.add("hide");
  2175. }else{
  2176. bar.classList.remove("stop");
  2177. if(hide)bar.classList.remove("hide");
  2178. }
  2179. });
  2180. }
  2181.  
  2182. function isInViewPort(element) {
  2183. if(!element.parentNode)return false;
  2184. if(_unsafeWindow.getComputedStyle(element).display=="none")return false;
  2185. const viewWidth = window.innerWidth || document.documentElement.clientWidth;
  2186. const viewHeight = window.innerHeight || document.documentElement.clientHeight;
  2187. const {
  2188. top,
  2189. right,
  2190. bottom,
  2191. left,
  2192. } = element.getBoundingClientRect();
  2193.  
  2194. return (
  2195. top >= 0 &&
  2196. left >= 0 &&
  2197. right <= viewWidth &&
  2198. bottom <= viewHeight
  2199. );
  2200. }
  2201.  
  2202. var urlChanged=false;
  2203. var _wr = function(type) {
  2204. var orig = history[type];
  2205. return function() {
  2206. var rv = orig.apply(this, arguments);
  2207. var e = new Event(type);
  2208. e.arguments = arguments;
  2209. window.dispatchEvent(e);
  2210. return rv;
  2211. };
  2212. };
  2213. history.pushState = _wr('pushState');
  2214. window.addEventListener('pushState', function(e) {
  2215. urlChanged=true;
  2216. isPause=true;
  2217. setTimeout(()=>{
  2218. if(location.href==configPage){
  2219. location.reload();
  2220. }
  2221. },1);
  2222. });
  2223.  
  2224. function initListener(){
  2225. let loadmoreBtn,loading=true;
  2226. setTimeout(()=>{
  2227. loadmoreBtn=getLoadMore(document);
  2228. if(loadmoreBtn && isVisible(loadmoreBtn, _unsafeWindow)){
  2229. loading=false;
  2230. }
  2231. },500);
  2232. let checkScrollReach=()=>{
  2233. let scrolly=window.scrollY;
  2234. let windowHeight=window.innerHeight || document.documentElement.clientHeight;
  2235. let scrollH=Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
  2236. if(scrollH-scrolly-windowHeight<bottomGap){
  2237. nextPage();
  2238. }
  2239. };
  2240. document.addEventListener('scroll', e=>{
  2241. if(urlChanged){
  2242. ruleParser.initPage(()=>{
  2243. if(ruleParser.nextLinkHref)initView();
  2244. });
  2245. urlChanged=false;
  2246. isPause=false;
  2247. }
  2248. if(isPause)return;
  2249. if(!loading){
  2250. if(!loadmoreBtn || !loadmoreBtn.parentNode){
  2251. loadmoreBtn=getLoadMore(document);
  2252. }
  2253. if(loadmoreBtn){
  2254. if(isInViewPort(loadmoreBtn)){
  2255. emuClick(loadmoreBtn);
  2256. loading=true;
  2257. setTimeout(()=>{loading=false},200);
  2258. }
  2259. }else{
  2260. loading=true;
  2261. setTimeout(()=>{loading=false},200);
  2262. }
  2263. }
  2264. setTimeout(()=>{
  2265. if(!isLoading){
  2266. checkScrollReach();
  2267. }
  2268. },100);
  2269. }, true);
  2270. document.addEventListener('dblclick', e=>{
  2271. if(rulesData.dbClick2Stop){
  2272. setTimeout(()=>{
  2273. changeStop(!isPause, rulesData.hideBar);
  2274. if(!isPause){
  2275. checkScrollReach();
  2276. }
  2277. },200);
  2278. }
  2279. });
  2280. }
  2281.  
  2282. function getLoadMore(doc){
  2283. if(ruleParser.curSiteRule.loadMore==="")return null;
  2284. let btnSel=ruleParser.curSiteRule.loadMore||".LoadMore,.load-more";
  2285. let loadmoreBtn=doc.querySelector(btnSel);
  2286. if(!loadmoreBtn){
  2287. let buttons=doc.querySelectorAll("input,button,a,div[onclick]"),loadmoreReg=/^\s*(加载更多|加載更多|load\s*more|もっと読み込む)\s*$/i;
  2288. for(let i=0;i<buttons.length;i++){
  2289. let button=buttons[i];
  2290. if(button && loadmoreReg.test(button.innerText)){
  2291. loadmoreBtn=button;
  2292. break;
  2293. }
  2294. }
  2295. }
  2296. if(loadmoreBtn && !ruleParser.curSiteRule.loadMore && loadmoreBtn.href && !/^(javascript|#)/.test(loadmoreBtn.href))loadmoreBtn=null;
  2297. return loadmoreBtn;
  2298. }
  2299.  
  2300. var hasPageNum=true;
  2301. function createPageBar(url){
  2302. let insert=ruleParser.getInsert();
  2303. if(!insert || !insert.parentNode)return;
  2304. curPage++;
  2305. if(rulesData.opacity==0)return;
  2306. let example=ruleParser.curSiteRule.insertPos==2?insert.children[0]:(insert.previousElementSibling||insert);
  2307. if(!example || !example.parentNode)example=insert;
  2308. let inTable=example.parentNode.tagName=="TABLE" ||
  2309. example.tagName=="TR" ||
  2310. example.tagName=="TBODY" ||
  2311. (example.previousElementSibling && example.previousElementSibling.tagName=="TR") ||
  2312. (example.previousElementSibling && example.previousElementSibling.tagName=="TBODY");
  2313. let inLi=example.tagName=="LI" || (example.previousElementSibling && example.previousElementSibling.tagName=="LI");
  2314. if(forceState==2){
  2315. inTable=inLi=false;
  2316. }
  2317. let pageBar=document.createElement(inTable?"tr":(inLi?"li":"div"));
  2318. let upSpan=document.createElement("span");
  2319. let downSpan=document.createElement("span");
  2320. let pageText=document.createElement("a");
  2321. let pageNum;
  2322. pageBar.className="pagetual_pageBar";
  2323. pageBar.id="pagetual_pageBar";
  2324. if(isPause){
  2325. pageBar.classList.add("stop");
  2326. }
  2327. pageBar.style.cssText=pageBarStyle;
  2328. pageBar.title=i18n(isPause?"enable":"disable");
  2329. upSpan.innerHTML=upSvg;
  2330. upSpan.children[0].style.cssText=upSvgCSS;
  2331. upSpan.title=i18n("toTop");
  2332. downSpan.innerHTML=downSvg;
  2333. downSpan.children[0].style.cssText=downSvgCSS;
  2334. downSpan.title=i18n("toBottom");
  2335. upSpan.style.cssText=initStyle;
  2336. downSpan.style.cssText=initStyle;
  2337. pageText.href=url;
  2338. pageText.style.cssText=pageTextStyle;
  2339. pageText.title=i18n("current");
  2340. pageBar.appendChild(upSpan);
  2341. pageBar.appendChild(pageText);
  2342. if(ruleParser.curSiteRule.pageNum || (hasPageNum && /[&\/\?](p=|page[=\/-])\d+|[_-]\d+\./.test(url))){
  2343. pageText.innerHTML="Page ";
  2344. pageNum=document.createElement("span");
  2345. pageNum.innerText=curPage;
  2346. pageNum.className="pagetual_pageNum";
  2347. pageNum.title=i18n("inputPageNum");
  2348. pageNum.style.cssText=pageTextStyle;
  2349. pageNum.style.cursor="pointer";
  2350. pageNum.style.color="";
  2351. pageNum.addEventListener("click", e=>{
  2352. let pageInput=prompt(i18n("inputPageNum"), "1");
  2353. if(pageInput){
  2354. let pageLink=ruleParser.getLinkByPage(url, pageInput);
  2355. if(pageLink){
  2356. _GM_openInTab(pageLink,{active:true});
  2357. }
  2358. }
  2359. e.preventDefault();
  2360. e.stopPropagation();
  2361. });
  2362. pageBar.appendChild(pageNum);
  2363. }else{
  2364. pageText.innerHTML="Page "+curPage;
  2365. hasPageNum=false;
  2366. }
  2367. pageBar.appendChild(downSpan);
  2368. let parentStyle=_unsafeWindow.getComputedStyle(example.parentNode);
  2369. let parentWidth=example.parentNode.scrollWidth||parseInt(parentStyle.width);
  2370. pageBar.style.width=parentWidth-parseInt(parentStyle.paddingLeft)-parseInt(parentStyle.paddingRight)-10+"px";
  2371. if(parentStyle.display=="grid" || parentStyle.display=="inline-grid"){
  2372. pageBar.style.gridColumnStart=1;
  2373. pageBar.style.gridColumnEnd=1+parseInt(example.parentNode.offsetWidth/example.offsetWidth);
  2374. }
  2375. if(inTable){
  2376. example=(example.tagName=="TR" || example.tagName=="TBODY")?example:example.previousElementSibling;
  2377. if(example.previousElementSibling)example=example.previousElementSibling;
  2378. let tdNum=0;
  2379. [].forEach.call(example.querySelectorAll("td,th"), td=>{
  2380. tdNum+=td.colSpan||1;
  2381. });
  2382. pageBar.style.display="table-row";
  2383. pageBar.style.backgroundColor="unset";
  2384. pageBar.style.lineHeight="20px";
  2385. let td=document.createElement("td");
  2386. td.style.backgroundColor="rgb(240 240 240 / 80%)";
  2387. td.style.borderRadius="20px";
  2388. td.colSpan=tdNum||1;
  2389. td.style.padding="0 0";
  2390. td.style.textAlign="center";
  2391. td.appendChild(upSpan);
  2392. td.appendChild(pageText);
  2393. if(pageNum)td.appendChild(pageNum);
  2394. td.appendChild(downSpan);
  2395. pageBar.appendChild(td);
  2396. }else if(inLi){
  2397. example=example.tagName=="LI"?example:example.previousElementSibling;
  2398. pageBar.style.display="table-row";
  2399. pageBar.style.backgroundColor="unset";
  2400. pageBar.style.lineHeight="20px";
  2401. let td=document.createElement("td");
  2402. td.style.backgroundColor="rgb(240 240 240 / 80%)";
  2403. td.style.borderRadius="20px";
  2404. td.colSpan=example.children.length;
  2405. td.style.padding="0 0";
  2406. td.style.textAlign="center";
  2407. td.style.minWidth="150px";
  2408. td.appendChild(upSpan);
  2409. td.appendChild(pageText);
  2410. td.style.width=pageBar.style.width;
  2411. if(pageNum)td.appendChild(pageNum);
  2412. td.appendChild(downSpan);
  2413. pageBar.appendChild(td);
  2414. }
  2415.  
  2416. upSpan.addEventListener("click", e=>{
  2417. document.body.scrollTop=0;
  2418. document.documentElement.scrollTop=0;
  2419. e.preventDefault();
  2420. e.stopPropagation();
  2421. });
  2422. downSpan.addEventListener("click", e=>{
  2423. changeStop(true);
  2424. pageBar.title=i18n(isPause?"enable":"disable");
  2425. document.body.scrollTop=9999999;
  2426. document.documentElement.scrollTop=9999999;
  2427. e.preventDefault();
  2428. e.stopPropagation();
  2429. });
  2430. pageBar.addEventListener("click", e=>{
  2431. changeStop(!isPause);
  2432. pageBar.title=i18n(isPause?"enable":"disable");
  2433. });
  2434. pageText.addEventListener("click", e=>{
  2435. e.stopPropagation();
  2436. });
  2437. if(ruleParser.curSiteRule.insertPos==2){
  2438. insert.appendChild(pageBar);
  2439. }else{
  2440. insert.parentNode.insertBefore(pageBar, insert);
  2441. }
  2442. if(ruleParser.curSiteRule.pageBar){
  2443. try{
  2444. Function("pageBar",'"use strict";' + ruleParser.curSiteRule.pageBar)(pageBar);
  2445. }catch(e){
  2446. debug(e);
  2447. }
  2448. }
  2449.  
  2450. let scrollH=Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
  2451. let posEle=pageBar.nextElementSibling||pageBar;
  2452. while(posEle && !posEle.offsetParent){
  2453. posEle=posEle.previousElementSibling||posEle.parentNode;
  2454. }
  2455. if(posEle){
  2456. let actualTop = getElementTop(posEle);
  2457. bottomGap=scrollH-actualTop+(window.innerHeight||document.documentElement.clientHeight)*(ruleParser.curSiteRule.rate||1);
  2458. if(bottomGap<100)bottomGap=100;
  2459. }else{
  2460. bottomGap=1000;
  2461. }
  2462. return pageBar;
  2463. }
  2464.  
  2465. function emuClick(btn){
  2466. if(!PointerEvent)return btn.click();
  2467. let eventParam={
  2468. isTrusted: true,
  2469. altKey: false,
  2470. azimuthAngle: 0,
  2471. bubbles: true,
  2472. button: 0,
  2473. buttons: 0,
  2474. clientX: 1,
  2475. clientY: 1,
  2476. cancelBubble: false,
  2477. cancelable: true,
  2478. composed: true,
  2479. ctrlKey: false,
  2480. defaultPrevented: false,
  2481. detail: 1,
  2482. eventPhase: 2,
  2483. fromElement: null,
  2484. height: 1,
  2485. isPrimary: false,
  2486. metaKey: false,
  2487. pointerId: 1,
  2488. pointerType: "mouse",
  2489. pressure: 0,
  2490. relatedTarget: null,
  2491. returnValue: true,
  2492. shiftKey: false,
  2493. toElement: null,
  2494. twist: 0,
  2495. type: "click",
  2496. which: 1
  2497. };
  2498. var mouseclick = new PointerEvent("click",eventParam);
  2499. btn.dispatchEvent(mouseclick);
  2500. }
  2501.  
  2502. var emuIframe;
  2503. function emuPage(callback){
  2504. let orgPage=null,curPage,iframeDoc,times=0,loadmoreBtn,loadmoreEnd=false,waitTimes=10;
  2505. function checkPage(){
  2506. if(isPause)return;
  2507. try{
  2508. iframeDoc=emuIframe.contentDocument || emuIframe.contentWindow.document;
  2509. }catch(e){
  2510. debug("Stop as cors");
  2511. isPause=true;
  2512. callback(false, false);
  2513. return;
  2514. }
  2515.  
  2516. let nextLink=ruleParser.getNextLink(iframeDoc);
  2517. let waitTime=500,checkEval;
  2518. if(ruleParser.curSiteRule.wait){
  2519. if(isNaN(ruleParser.curSiteRule.wait)){
  2520. try{
  2521. checkEval=Function("doc",'"use strict";' + ruleParser.curSiteRule.wait);
  2522. }catch(e){
  2523. debug(e);
  2524. }
  2525. }else{
  2526. waitTime=ruleParser.curSiteRule.wait;
  2527. }
  2528. }
  2529.  
  2530. if(!orgPage){
  2531. if(!loadmoreEnd){
  2532. loadmoreBtn=getLoadMore(iframeDoc);
  2533. if(loadmoreBtn && isVisible(loadmoreBtn, iframeDoc.defaultView)){
  2534. emuClick(loadmoreBtn);
  2535. let intv=setInterval(()=>{
  2536. loadmoreBtn=getLoadMore(iframeDoc);
  2537. if(!loadmoreBtn || !loadmoreBtn.parentNode || !isVisible(loadmoreBtn, iframeDoc.defaultView)){
  2538. clearInterval(intv);
  2539. loadmoreEnd=true;
  2540. setTimeout(()=>{
  2541. checkPage();
  2542. },500);
  2543. }else{
  2544. emuClick(loadmoreBtn);
  2545. }
  2546. },200);
  2547. return;
  2548. }else{
  2549. loadmoreEnd=true;
  2550. }
  2551. }
  2552. let pageEle=ruleParser.getPageElement(iframeDoc, iframeDoc.defaultView, true);
  2553. if(!nextLink || !pageEle || pageEle.length==0 || (checkEval && !checkEval(iframeDoc))){
  2554. if(waitTimes-->0){
  2555. setTimeout(()=>{
  2556. checkPage();
  2557. },waitTime);
  2558. return;
  2559. }
  2560. }
  2561. orgPage=pageEle;
  2562. if(!orgPage || orgPage.length==0){
  2563. debug("Stop as no page when emu");
  2564. isPause=true;
  2565. callback(false, false);
  2566. return;
  2567. }
  2568. if(orgPage && orgPage[0].tagName=="UL")orgPage=orgPage[0].children;
  2569. if(orgPage && nextLink){
  2570. orgPage=orgPage[parseInt(orgPage.length/2)];
  2571. if(!isVisible(nextLink, iframeDoc.defaultView)){
  2572. debug("Stop as next hide when emu");
  2573. isPause=true;
  2574. callback(false, false);
  2575. }else{
  2576. emuClick(nextLink);
  2577. setTimeout(()=>{
  2578. checkPage();
  2579. },500);
  2580. }
  2581. }else{
  2582. debug("Stop as no next when emu");
  2583. isPause=true;
  2584. callback(false, false);
  2585. }
  2586. return;
  2587. }
  2588. if(times++ > 20){
  2589. //emuClick(nextLink);
  2590. debug("Stop as timeout when emu");
  2591. isPause=true;
  2592. callback(false, false);
  2593. return;
  2594. }
  2595. let eles=ruleParser.getPageElement(iframeDoc, iframeDoc.defaultView, true),checkItem;
  2596. if(eles && eles.length>0){
  2597. checkItem=eles;
  2598. if(eles[0].tagName=="UL"){
  2599. checkItem=eles[0].children;
  2600. }
  2601. checkItem=checkItem[parseInt(checkItem.length/2)];
  2602. }
  2603. if(!checkItem || orgPage == checkItem || (checkEval && !checkEval(iframeDoc))){
  2604. setTimeout(()=>{
  2605. checkPage();
  2606. },waitTime);
  2607. }else{
  2608. callback(iframeDoc, eles);
  2609. }
  2610. }
  2611. if(!emuIframe){
  2612. let loaded=false;
  2613. emuIframe = document.createElement('iframe');
  2614. emuIframe.name = 'pagetual-iframe';
  2615. if(ruleParser.curSiteRule.sandbox!=false){
  2616. emuIframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  2617. }
  2618. emuIframe.width = '100%';
  2619. emuIframe.height = '0';
  2620. emuIframe.frameBorder = '0';
  2621. emuIframe.style.cssText = 'margin:0!important;padding:0!important;visibility:hidden!important;';
  2622. emuIframe.addEventListener("load", e=>{
  2623. setTimeout(()=>{
  2624. try{
  2625. iframeDoc=emuIframe.contentDocument || emuIframe.contentWindow.document;
  2626. }catch(e){
  2627. debug("Stop as cors");
  2628. isPause=true;
  2629. callback(false, false);
  2630. return;
  2631. }
  2632. let code=ruleParser.curSiteRule.init;
  2633. if(code){
  2634. try{
  2635. Function('doc','win','"use strict";' + code)(iframeDoc, iframeDoc.defaultView);
  2636. }catch(e){
  2637. debug(e);
  2638. }
  2639. }
  2640. if(loaded)return;
  2641. loaded=true;
  2642. checkPage();
  2643. },500);
  2644. });
  2645. emuIframe.src=location.href;
  2646. document.body.appendChild(emuIframe);
  2647. }else{
  2648. checkPage();
  2649. }
  2650. }
  2651.  
  2652. function forceIframe(url, callback){
  2653. let curIframe = document.createElement('iframe'),iframeDoc;
  2654. curIframe.name = 'pagetual-iframe';
  2655. curIframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  2656. curIframe.frameBorder = '0';
  2657. curIframe.scrolling="no";
  2658. curIframe.style.cssText = 'display: block; visibility: visible; float: none; clear: both; width: 100%;height:0;background: initial; border: 0px; border-radius: 0px; margin: 0px 0px 2rem; padding: 0px; z-index: 2147483647;';
  2659. curIframe.addEventListener("load", e=>{
  2660. try{
  2661. iframeDoc=curIframe.contentDocument || curIframe.contentWindow.document;
  2662. }catch(e){
  2663. debug("Stop as cors");
  2664. isPause=true;
  2665. callback(false, false);
  2666. return;
  2667. }
  2668. ruleParser.insertPage(iframeDoc, [], url, null, true);
  2669. callback(curIframe, true);
  2670. curIframe.style.height=iframeDoc.body.scrollHeight+"px";
  2671. curIframe.style.width=iframeDoc.body.scrollWidth+"px";
  2672. });
  2673. let inAction=false;
  2674. let forceRefresh=e=>{
  2675. if(inAction || !iframeDoc)return;
  2676. inAction=true;
  2677. setTimeout(()=>{
  2678. inAction=false;
  2679. curIframe.style.height=iframeDoc.body.scrollHeight+"px";
  2680. curIframe.style.width=iframeDoc.body.scrollWidth+"px";
  2681. if(!ruleParser.nextLinkHref){
  2682. ruleParser.getNextLink(iframeDoc);
  2683. if(ruleParser.nextLinkHref){
  2684. document.removeEventListener("scroll", forceRefresh);
  2685. }
  2686. }else{
  2687. document.removeEventListener("scroll", forceRefresh);
  2688. }
  2689. },500);
  2690. curIframe.style.height=iframeDoc.body.scrollHeight+"px";
  2691. curIframe.style.width=iframeDoc.body.scrollWidth+"px";
  2692. };
  2693. document.addEventListener("scroll", forceRefresh);
  2694. curIframe.src=url;
  2695. let insert=ruleParser.getInsert();
  2696. document.body.appendChild(curIframe);
  2697. return curIframe;
  2698. }
  2699.  
  2700. function loadPageOver(){
  2701. isLoading=false;
  2702. if(loadingDiv.parentNode){
  2703. loadingDiv.parentNode.removeChild(loadingDiv);
  2704. }
  2705. setTimeout(()=>{
  2706. let scrollH=Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
  2707. if(scrollH < (window.innerHeight || document.documentElement.clientHeight)){
  2708. nextPage();
  2709. }
  2710. },1);
  2711. }
  2712.  
  2713. function nextPage(){
  2714. if(isPause || isLoading)return;
  2715. let nextLink=ruleParser.nextLinkHref;
  2716. if(!nextLink)return;
  2717. let pvGallery=document.querySelector("span.pv-gallery-container");
  2718. if(pvGallery && pvGallery.style.display!="none")return;
  2719. let insert=ruleParser.getInsert();
  2720. if(insert){
  2721. let isJs=/^(javascript|#)/.test(nextLink.replace(location.href,""));
  2722. if(location.protocol=="https:" && /^http:/.test(nextLink)){
  2723. nextLink=nextLink.replace(/^http/,"https");
  2724. }
  2725. isLoading=true;
  2726. document.body.appendChild(loadingDiv);
  2727. loadingDiv.style.cssText=loadingCSS;
  2728. let sleep=ruleParser.curSiteRule.sleep||0;
  2729. setTimeout(()=>{
  2730. if(ruleParser.curSiteRule.pageElementByJs){
  2731. var over=ele=>{
  2732. loadPageOver();
  2733. if(ele){
  2734. createPageBar(nextLink);
  2735. ruleParser.insertPage(null, ele, nextLink, null, true);
  2736. if(autoLoadNum>=0){
  2737. if(autoLoadNum!=0 && --autoLoadNum==0){
  2738. autoLoadNum=-1;
  2739. }else{
  2740. nextPage();
  2741. }
  2742. }
  2743. }else{
  2744. debug("Stop as no page when get by js");
  2745. isPause=true;
  2746. }
  2747. };
  2748. try{
  2749. Function("over",'"use strict";' + ruleParser.curSiteRule.pageElementByJs)(over);
  2750. }catch(e){
  2751. debug(e);
  2752. }
  2753. }else if((forceState==3||ruleParser.curSiteRule.action==1) && !isJs){
  2754. requestFromIframe(nextLink, (doc, eles)=>{
  2755. loadPageOver();
  2756. if(eles){
  2757. createPageBar(nextLink);
  2758. ruleParser.insertPage(doc, eles, nextLink, null, true);
  2759. if(autoLoadNum>=0){
  2760. if(autoLoadNum!=0 && --autoLoadNum==0){
  2761. autoLoadNum=-1;
  2762. }else{
  2763. nextPage();
  2764. }
  2765. }
  2766. }
  2767. });
  2768. }else if((forceState==2||ruleParser.curSiteRule.action==2) && !isJs){
  2769. forceIframe(nextLink, (iframe, eles)=>{
  2770. loadPageOver();
  2771. let pageBar=createPageBar(nextLink);
  2772. if(pageBar)iframe.parentNode.insertBefore(pageBar, iframe);
  2773. if(autoLoadNum>=0){
  2774. if(autoLoadNum!=0 && --autoLoadNum==0){
  2775. autoLoadNum=-1;
  2776. }else{
  2777. nextPage();
  2778. }
  2779. }
  2780. });
  2781. }else{
  2782. if(!isJs){
  2783. requestDoc(nextLink, (eles)=>{
  2784. loadPageOver();
  2785. if(eles){
  2786. createPageBar(nextLink);
  2787. if(autoLoadNum>=0){
  2788. if(autoLoadNum!=0 && --autoLoadNum==0){
  2789. autoLoadNum=-1;
  2790. }else{
  2791. nextPage();
  2792. }
  2793. }
  2794. }
  2795. });
  2796. }else{
  2797. emuPage((doc, eles)=>{
  2798. loadPageOver();
  2799. if(eles){
  2800. createPageBar(nextLink);
  2801. ruleParser.insertPage(doc, eles, "", null, true);
  2802. if(autoLoadNum>=0){
  2803. if(autoLoadNum!=0 && --autoLoadNum==0){
  2804. autoLoadNum=-1;
  2805. }else{
  2806. nextPage();
  2807. }
  2808. }
  2809. }
  2810. });
  2811. }
  2812. }
  2813. },sleep);
  2814. }
  2815. }
  2816.  
  2817. function init(){
  2818. initRules(()=>{
  2819. initPage();
  2820. });
  2821. }
  2822. setTimeout(()=>{
  2823. init();
  2824. },300);
  2825. })();