东方永页机

自动翻页

当前为 2022-01-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Pagetual
  3. // @name:zh-CN 东方永页机
  4. // @name:zh-TW 東方永頁機
  5. // @name:ja 東方永頁機
  6. // @name:ru Пейджетуал
  7. // @name:de Pagetual
  8. // @name:ko 東方永頁機
  9. // @namespace hoothin
  10. // @version 0.8.0
  11. // @description Simply auto load the next page
  12. // @description:zh-CN 自动翻页
  13. // @description:zh-TW 自動翻頁
  14. // @description:ja Webページを自動で読み込み継ぎ足し表示を行うブラウザ拡張です
  15. // @description:ru Просто автоматически загрузите следующую страницу
  16. // @description:de Laden Sie einfach automatisch die nächste Seite
  17. // @description:ko 다음 페이지를 자동으로 로드하세요
  18. // @author hoothin
  19. // @include http://*
  20. // @include https://*
  21. // @license MIT
  22. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAdVBMVEUAAADZHQbYHQXaHQbYHgbZHATYHgbYHgbZHQXYHgXZHgbZHgfYHQXYHQbXHQDYHgbYHgbYHQXZHgbYHgbYHgbYHgXaHQbYHQbYHQbZHgbZHgbZHQbZHgbYHgbYHQbYHQXYHgfaHwDZHQXZHQbYHwTVHQrYHgbCqWmRAAAAJnRSTlMAUL8oyD/el2cy1CCPcg/35euggPG6FajOr4Z617OdXkkIi1g4GcTbfAwAAAJDSURBVFjD7dbXtqpADADQ0NsA0hHQY83/f+K9rqNOCzPwfvaTugimjEH4s86739yX0+3n8dwd7dQh/seiaWbHscUqCnZEP24LvrXjXLyyaVJMp2RbeFajZInhJWCHcurB7idEVfS+89SGsS28qJFwyN59ZcgsnXeRVrwvSNLFNJEA1/jfDqW4Po8Z1+XwsX6HGA1Cft1Y0ndw0OjCrwxDj4hPDqgYXbGlZ35pj0fQ1dr0C4DM5++lZHMt/qLNXq2rAG5qtWPdoaL5/ZwXloHAP1onUKs3SOSOO5YEsFNLkJN2T/IRRt0gj6ZUUpZTyMmzUx/Vw8wVhwgEI9pog2PiHRO0OhTq2hLnytBK3yRdsKeCGjRnoagKLRbQNfwsXdGiAkLMu5ihWUSvr5S/tPWPlIV8kxvn58CKatMu7Ly1+Kzcsgwj0wqXlgnNMT7CrFPwwaTp+OsSKS4Y1UJ5Phl/BSM3F7JBQmKO78UGJWT/zWJpTae7E4CTtJcnVJ2E7dV4RAWlY36uM96gFnHQT0HFe0zOIZYbnGsVRJZHc/DZFS15pu54URfUSgYJvVJ8BgpPWWu3TwfJUxkvPahylAzy573ybXfQHVEUFt/2uC67KgWcybPp602gndMMKF647bfIUg9oQYmCH6B1PF7jHFDQk2n6PJ7gDcil1FekowdGuVBGqw7rGZXNFSweLnJRJo4pD4cENphb/CoH57O6o8q/wzbJtCBXuXVz7sqQBbBDMJ9a6e+SA/tlzjx0Q3TLL0/4Y/QPSYfEIomUUSEAAAAASUVORK5CYII=
  23. // @grant GM_xmlhttpRequest
  24. // @grant GM_registerMenuCommand
  25. // @grant GM_notification
  26. // @grant GM_getValue
  27. // @grant GM_setValue
  28. // @grant GM_addStyle
  29. // @grant GM_openInTab
  30. // @grant GM.xmlHttpRequest
  31. // @grant GM.registerMenuCommand
  32. // @grant GM.notification
  33. // @grant GM.getValue
  34. // @grant GM.setValue
  35. // @grant GM.addStyle
  36. // @grant GM.openInTab
  37. // @connect wedata.net
  38. // @connect githubusercontent.com
  39. // @run-at document-idle
  40. // @exclude http://www.toodledo.com/tasks/*
  41. // @exclude http*://maps.google.com*/*
  42. // @exclude *://www.google.*/_/chrome/newtab*
  43. // @exclude *://mega.*/*
  44. // @exclude *://*.mega.*/*
  45. // @exclude *://*.youku.com/v_*
  46. // @exclude *://*pan.baidu.com
  47. // @exclude *://*.iqiyi.com/v_*
  48. // @exclude *://*.iqiyi.com/w_*
  49. // @exclude *://*.iqiyi.com/a_*
  50. // @exclude *://*.le.com/ptv/vplay/*
  51. // @exclude *://v.qq.com/x/cover/*
  52. // @exclude *://v.qq.com/x/page/*
  53. // @exclude *://v.qq.com/tv/*
  54. // @exclude *://*.tudou.com/listplay/*
  55. // @exclude *://*.tudou.com/albumplay/*
  56. // @exclude *://*.tudou.com/programs/view/*
  57. // @exclude *://*.mgtv.com/b/*
  58. // @exclude *://film.sohu.com/album/*
  59. // @exclude *://tv.sohu.com/v/*
  60. // @exclude *://*.bilibili.com/video/*
  61. // @exclude *://*.bilibili.com/bangumi/play/*
  62. // @exclude *://*.baofeng.com/play/*
  63. // @exclude *://vip.pptv.com/show/*
  64. // @exclude *://v.pptv.com/show/*
  65. // @exclude *://www.le.com/ptv/vplay/*
  66. // @exclude *://www.wasu.cn/Play/show/*
  67. // @exclude *://m.v.qq.com/x/cover/*
  68. // @exclude *://m.v.qq.com/x/page/*
  69. // @exclude *://m.v.qq.com/*
  70. // @exclude *://m.iqiyi.com/*
  71. // @exclude *://m.iqiyi.com/kszt/*
  72. // @exclude *://m.youku.com/alipay_video/*
  73. // @exclude *://m.mgtv.com/b/*
  74. // @exclude *://m.tv.sohu.com/v/*
  75. // @exclude *://m.film.sohu.com/album/*
  76. // @exclude *://m.le.com/ptv/vplay/*
  77. // @exclude *://m.pptv.com/show/*
  78. // @exclude *://m.acfun.cn/v/*
  79. // @exclude *://m.bilibili.com/video/*
  80. // @exclude *://m.bilibili.com/anime/*
  81. // @exclude *://m.bilibili.com/bangumi/play/*
  82. // @exclude *://m.wasu.cn/Play/show/*
  83. // @exclude *://www.youtube.com
  84. // @exclude *://www.youtube.com/
  85. // @exclude *://www.youtube.com/watch*
  86. // ==/UserScript==
  87.  
  88. (function() {
  89. 'use strict';
  90.  
  91. if (window.name === 'pagetual-iframe') {
  92. var domloaded = function (){
  93. window.scroll(window.scrollX, 99999);
  94. //window.parent.postMessage('pagetual-iframe:DOMLoaded', '*');
  95. };
  96. if(window.opera){
  97. document.addEventListener('DOMContentLoaded', domloaded, false);
  98. } else {
  99. domloaded();
  100. }
  101. return;
  102. }
  103.  
  104. if(window.top != window.self){
  105. return;
  106. }
  107.  
  108. const lang = navigator.appName=="Netscape"?navigator.language:navigator.userLanguage;
  109. var i18n=(name, param)=>{
  110. let config={};
  111. switch (lang){
  112. case "zh-CN":
  113. case "zh-SG":
  114. config={
  115. disable:"暂时禁用",
  116. disableSite:"在此站禁用",
  117. enable:"启用翻页",
  118. toTop:"回到顶部",
  119. toBottom:"前往页尾",
  120. current:"当前页",
  121. forceIframe:"强制拼接",
  122. cancelForceIframe:"取消强制拼接",
  123. configure:"打开配置页",
  124. update:"立即更新规则",
  125. passSec:"更新于 #t# 秒前",
  126. passMin:"更新于 #t# 分钟前",
  127. passHour:"更新于 #t# 小时前",
  128. passDay:"更新于 #t# 天前",
  129. cantDel:"无法删除内置规则",
  130. confirmDel:"是否确认要删除此规则?",
  131. updateSucc:"更新成功",
  132. beginUpdate:"正在更新,请稍候",
  133. customUrls:"导入规则url,一行一条,AutoPagerize 格式规则需要以\"0|\"开头",
  134. customRules:"输入【东方永页机】格式的自定义规则",
  135. save:"保存设置",
  136. loadingText:"少女祈祷中...",
  137. opacity:"分页隔条透明值",
  138. hideBar:"空白处双击隐藏分页隔条",
  139. sortTitle:"排序在下次更新规则后生效"
  140. };
  141. break;
  142. case "zh-TW":
  143. case "zh-HK":
  144. config={
  145. disable:"暫時禁用",
  146. disableSite:"在此站禁用",
  147. enable:"啟用翻頁",
  148. toTop:"回到頂部",
  149. toBottom:"前往頁尾",
  150. current:"當前頁",
  151. forceIframe:"強制拼接",
  152. cancelForceIframe:"取消强制拼接",
  153. configure:"打開配置頁",
  154. update:"立即更新規則",
  155. passSec:"更新于 #t# 秒前",
  156. passMin:"更新于 #t# 分鐘前",
  157. passHour:"更新于 #t# 小時前",
  158. passDay:"更新于 #t# 天前",
  159. cantDel:"無法刪除内置規則",
  160. confirmDel:"是否確認要刪除此規則?",
  161. updateSucc:"更新成功",
  162. beginUpdate:"正在更新,請稍候",
  163. customUrls:"導入規則url,一行一條,AutoPagerize 格式規則需要以\"0|\"開頭",
  164. customRules:"輸入【東方永頁機】格式的自定義規則",
  165. save:"存儲設置",
  166. loadingText:"少女祈禱中...",
  167. opacity:"分頁隔條透明值",
  168. hideBar:"空白處雙擊隱藏分頁隔條",
  169. sortTitle:"排序在下次更新規則後生效"
  170. };
  171. break;
  172. case "ja":
  173. config = {
  174. disable: "一時的に無効にする",
  175. disableSite:"このサイト無効",
  176. enable: "ページめくりを有効にする",
  177. toTop: "トップに戻る",
  178. toBottom: "ページの下部に移動",
  179. current: "現在のページ",
  180. forceIframe: "強制ステッチ",
  181. cancelForceIframe: "強制ステッチをキャンセル",
  182. configure: "設定ページを開く",
  183. update: "今すぐルールを更新してください",
  184. passSec: "#t#秒前に更新",
  185. passMin: "#t#分前に更新",
  186. passHour: "#t#時間前に更新",
  187. passDay: "#t#日前に更新",
  188. cantDel: "組み込みルールを削除できません",
  189. confirmDel: "このルールを削除してもよろしいですか?",
  190. updateSucc: "更新に成功しました",
  191. beginUpdate: "更新中、お待ちください",
  192. customUrls: "インポートルールのURL、1行に1つ、AutoPagerizeフォーマットルールは\"0|\"で始まる必要があります",
  193. customRules: "【東方永頁機】の形式でカスタムルールを入力してください",
  194. save: "設定を保存",
  195. loadingText: "少女祈祷中...",
  196. opacity:"ページネーションバーの透明値",
  197. hideBar:"空白部分をダブルクリックして、ページ区切り文字を非表示にします",
  198. sortTitle:"並べ替えは、次のルールの更新後に有効になります"
  199. };
  200. break;
  201. default:
  202. config={
  203. disable:"Disable",
  204. disableSite:"Disable in the site",
  205. enable:"Enable",
  206. toTop:"To Top",
  207. toBottom:"To Bottom",
  208. current:"Current Page",
  209. forceIframe:"Force to join",
  210. cancelForceIframe:"Cancel Force join",
  211. configure:"Configure",
  212. update:"Update rules from url now",
  213. passSec:"Updated #t# seconds ago",
  214. passMin:"Updated #t# minutes ago",
  215. passHour:"Updated #t# hours ago",
  216. passDay:"Updated #t# days ago",
  217. cantDel:"Can't delete buildin rules",
  218. confirmDel:"Are you sure you want to delete this rule?",
  219. updateSucc:"Update succeeded",
  220. beginUpdate:"Begin update, wait a minute please",
  221. customUrls:"Import rule url, One url per line, rules on AutoPagerize format need to start with \"0|\"",
  222. customRules:"Input custom rules with [Pagetual] format",
  223. save:"Save",
  224. loadingText:"Shojo Now Loading...",
  225. opacity:"Pagination spacer opacity",
  226. hideBar:"Double-click on the blank space to hide the paging spacer",
  227. sortTitle:"Sorting takes effect after the next rule update"
  228. };
  229. break;
  230. }
  231. return config[name]?config[name].replace("#t#",param):name;
  232. };
  233.  
  234. var enableDebug=true;
  235. var debug=str=>{
  236. if(enableDebug){
  237. console.debug(str);
  238. }
  239. };
  240.  
  241. var _GM_xmlhttpRequest,_GM_registerMenuCommand,_GM_notification,_GM_addStyle,_GM_openInTab;
  242. if(typeof GM_xmlhttpRequest!='undefined'){
  243. _GM_xmlhttpRequest=GM_xmlhttpRequest;
  244. }else if(typeof GM!='undefined' && typeof GM.xmlHttpRequest!='undefined'){
  245. _GM_xmlhttpRequest=GM.xmlHttpRequest;
  246. }
  247. if(typeof GM_registerMenuCommand!='undefined'){
  248. _GM_registerMenuCommand=GM_registerMenuCommand;
  249. }else if(typeof GM!='undefined' && typeof GM.registerMenuCommand!='undefined'){
  250. _GM_registerMenuCommand=GM.registerMenuCommand;
  251. }
  252. if(typeof GM_notification!='undefined'){
  253. _GM_notification=GM_notification;
  254. }else if(typeof GM!='undefined' && typeof GM.notification!='undefined'){
  255. _GM_notification=GM.notification;
  256. }
  257. if(typeof GM_openInTab!='undefined'){
  258. _GM_openInTab=GM_openInTab;
  259. }else if(typeof GM!='undefined' && typeof GM.openInTab!='undefined'){
  260. _GM_openInTab=GM.openInTab;
  261. }
  262. if(typeof GM_addStyle!='undefined'){
  263. _GM_addStyle=GM_addStyle;
  264. }else if(typeof GM!='undefined' && typeof GM.addStyle!='undefined'){
  265. _GM_addStyle=GM.addStyle;
  266. }else{
  267. _GM_addStyle=cssStr=>{
  268. let styleEle=document.createElement("style");
  269. styleEle.innerHTML=cssStr;
  270. document.head.appendChild(styleEle);
  271. };
  272. }
  273.  
  274. if(typeof _GM_xmlhttpRequest=='undefined')_GM_xmlhttpRequest=(f)=>{};
  275. if(typeof _GM_registerMenuCommand=='undefined')_GM_registerMenuCommand=(s,f)=>{};
  276. if(typeof _GM_notification=='undefined')_GM_notification=(s)=>{};
  277. var _unsafeWindow=(typeof unsafeWindow=='undefined')?window:unsafeWindow;
  278. var storage={
  279. supportGM: typeof GM_getValue=='function' && typeof GM_getValue('a','b')!='undefined',
  280. supportGMPromise: typeof GM!='undefined' && typeof GM.getValue=='function' && typeof GM.getValue('a','b')!='undefined',
  281. mxAppStorage:(function(){
  282. try{
  283. return window.external.mxGetRuntime().storage;
  284. }catch(e){
  285. }
  286. })(),
  287. operaUJSStorage:(function(){
  288. try{
  289. return window.opera.scriptStorage;
  290. }catch(e){
  291. }
  292. })(),
  293. setItem:function(key,value){
  294. if(this.operaUJSStorage){
  295. this.operaUJSStorage.setItem(key,value);
  296. }else if(this.mxAppStorage){
  297. this.mxAppStorage.setConfig(key,value);
  298. }else if(this.supportGM){
  299. GM_setValue(key,value);
  300. }else if(this.supportGMPromise){
  301. GM.setValue(key,value);
  302. }else if(window.localStorage){
  303. window.localStorage.setItem(key,value);
  304. }
  305. },
  306. getItem:function(key,cb){
  307. var value;
  308. if(this.operaUJSStorage){
  309. value=this.operaUJSStorage.getItem(key);
  310. }else if(this.mxAppStorage){
  311. value=this.mxAppStorage.getConfig(key);
  312. }else if(this.supportGM){
  313. value=GM_getValue(key);
  314. }else if(this.supportGMPromise){
  315. value=GM.getValue(key).then(v=>{cb(v)});
  316. return;
  317. }else if(window.localStorage){
  318. value=window.localStorage.getItem(key);
  319. };
  320. cb(value);
  321. }
  322. };
  323. _GM_registerMenuCommand(i18n("configure"), ()=>{
  324. _GM_openInTab("https://github.com/hoothin/UserScripts/tree/master/Pagetual");
  325. });
  326.  
  327. function getElementByXpath(xpath, contextNode, doc){
  328. doc = doc || document;
  329. contextNode = contextNode || doc;
  330. try {
  331. var result = doc.evaluate(xpath, contextNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  332. return result.singleNodeValue && result.singleNodeValue.nodeType === 1 && result.singleNodeValue;
  333. } catch (err) {
  334. throw new Error(`Invalid xpath: ${xpath}`);
  335. }
  336. }
  337.  
  338. function getAllElementsByXpath(xpath, contextNode, doc){
  339. doc = doc || document;
  340. contextNode = contextNode || doc;
  341. var result = [];
  342. try {
  343. var query = doc.evaluate(xpath, contextNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  344. for (var i = 0; i < query.snapshotLength; i++) {
  345. var node = query.snapshotItem(i);
  346. if (node.nodeType === 1) result.push(node);
  347. }
  348. } catch (err) {
  349. throw new Error(`Invalid xpath: ${xpath}`);
  350. }
  351. return result;
  352. }
  353.  
  354. class RuleParser {
  355. constructor() {
  356. this.hpRules=[];
  357. this.customRules=[];
  358. this.rules=[];
  359. this.pageDoc=document;
  360. this.nextLinkHref=null;
  361. this.curUrl=location.href;
  362. this.curSiteRule={};
  363. }
  364.  
  365. initSavedRules(callback){
  366. var self=this;
  367. storage.getItem("hpRules", hpRules=>{
  368. if(hpRules)self.hpRules=hpRules;
  369. storage.getItem("customRules", customRules=>{
  370. if(customRules)self.customRules=customRules;
  371. storage.getItem("rules", rules=>{
  372. if(rules)this.rules=rules;
  373. callback();
  374. });
  375. });
  376. });
  377. }
  378.  
  379. saveCurSiteRule(){
  380. if(!this.curSiteRule || !this.curSiteRule.url)return;
  381. this.hpRules=this.hpRules.filter(item=>{return item&&item.url!=this.curSiteRule.url});
  382. this.hpRules.unshift(this.curSiteRule);
  383. if(this.hpRules.length>30){
  384. this.hpRules.pop();
  385. }
  386. storage.setItem("hpRules", this.hpRules);
  387. }
  388.  
  389. requestJSON(url, callback){
  390. _GM_xmlhttpRequest({
  391. url: url,
  392. method: 'GET',
  393. onload: function(res) {
  394. let json=null;
  395. try{
  396. json=JSON.parse(res.response);
  397. }catch(e){
  398. debug(e);
  399. }
  400. callback(json);
  401. }
  402. });
  403. }
  404.  
  405. formatRule(item, type, from){
  406. switch(type){
  407. case 0:
  408. return {
  409. from:from,
  410. type:type,
  411. name:item.name,
  412. action:item.data.forceIframe=="true"?1:0,
  413. url:item.data.url,
  414. pageElement:item.data.pageElement,
  415. nextLink:item.data.nextLink,
  416. insert:item.data.insertBefore,
  417. insertPos:1,
  418. updatedAt:item.data.updated_at,
  419. css:item.data.Stylus+item.data.CSS,
  420. pageAction:item.data.bookmarklet
  421. };
  422. break;
  423. case 1:
  424. default:
  425. item.from=from;
  426. if(typeof(item.type) == "undefined")item.type=type;
  427. return item;
  428. break;
  429. }
  430. return null;
  431. }
  432.  
  433. addRuleByUrl(url, type, from, callback) {
  434. this.requestJSON(url, json=>{
  435. this.addRules(json, type, from);
  436. storage.setItem("rules", this.rules);
  437. callback();
  438. });
  439. }
  440.  
  441. addRules(rules, type, from) {
  442. if(rules && rules.length>0){
  443. this.rules=this.rules.filter(item=>{return item.from!=from});
  444. rules.forEach(item=>{
  445. let rule=this.formatRule(item, type, from);
  446. if(rule){
  447. this.rules.push(rule);
  448. }
  449. });
  450. }
  451. }
  452.  
  453. getRule(callback) {
  454. if(this.curSiteRule && this.curSiteRule.url){
  455. return this.curSiteRule;
  456. }
  457. var self=this;
  458. for(let i in this.hpRules){
  459. let rule=this.hpRules[i];
  460. if(!rule || rule.enable==0 || !rule.url)continue;
  461. if(rule.singleUrl){
  462. if(location.origin+location.pathname==rule.url){
  463. this.curSiteRule=rule;
  464. debug(rule);
  465. callback();
  466. return rule;
  467. }
  468. continue;
  469. }
  470. let urlReg=new RegExp(rule.url, "i");
  471. if(urlReg.test(location.href)){
  472. let pageElement,nextLink,insert;
  473. if(rule.pageElement)pageElement=rule.type==0?getElementByXpath(rule.pageElement):document.querySelector(rule.pageElement);
  474. if(rule.nextLink)nextLink=rule.type==0?getElementByXpath(rule.nextLink):document.querySelector(rule.nextLink);
  475. if(rule.insert)insert=rule.type==0?getElementByXpath(rule.insert):document.querySelector(rule.insert);
  476. if((rule.pageElement && !pageElement) ||
  477. (rule.nextLink && !nextLink) ||
  478. (rule.insert && !insert)){
  479. continue;
  480. }
  481. this.curSiteRule=rule;
  482. debug(rule);
  483. callback();
  484. return rule;
  485. }
  486. }
  487. for(let i in this.customRules){
  488. let rule=this.customRules[i];
  489. if(!rule || rule.enable==0 || !rule.url)continue;
  490. let urlReg=new RegExp(rule.url, "i");
  491. if(urlReg.test(location.href)){
  492. let pageElement,nextLink,insert;
  493. if(rule.pageElement)pageElement=rule.type==0?getElementByXpath(rule.pageElement):document.querySelector(rule.pageElement);
  494. if(rule.nextLink)nextLink=rule.type==0?getElementByXpath(rule.nextLink):document.querySelector(rule.nextLink);
  495. if(rule.insert)insert=rule.type==0?getElementByXpath(rule.insert):document.querySelector(rule.insert);
  496. if((rule.pageElement && !pageElement) ||
  497. (rule.nextLink && !nextLink) ||
  498. (rule.insert && !insert)){
  499. continue;
  500. }
  501. this.curSiteRule=rule;
  502. debug(rule);
  503. callback();
  504. return rule;
  505. }
  506. }
  507. let r=0;
  508. function searchByTime(){
  509. setTimeout(()=>{
  510. let end=r+80;
  511. end=end>self.rules.length?self.rules.length:end;
  512. for(;r<end;r++){
  513. let rule=self.rules[r];
  514. if(rule.enable==0)continue;
  515. let urlReg=new RegExp(rule.url, "i");
  516. if(urlReg.test(location.href)){
  517. let pageElement,nextLink,insert;
  518. if(rule.pageElement)pageElement=rule.type==0?getElementByXpath(rule.pageElement):document.querySelector(rule.pageElement);
  519. if(rule.nextLink)nextLink=rule.type==0?getElementByXpath(rule.nextLink):document.querySelector(rule.nextLink);
  520. if(rule.insert)insert=rule.type==0?getElementByXpath(rule.insert):document.querySelector(rule.insert);
  521. if((rule.pageElement && !pageElement) ||
  522. (rule.nextLink && !nextLink) ||
  523. (rule.insert && !insert)){
  524. continue;
  525. }
  526. self.curSiteRule=rule;
  527. debug(rule);
  528. callback();
  529. return;
  530. }
  531. }
  532. if(end>=self.rules.length){
  533. self.curSiteRule={};
  534. self.curSiteRule.url=location.origin+location.pathname;
  535. self.curSiteRule.singleUrl=true;
  536. callback();
  537. return;
  538. }else{
  539. searchByTime();
  540. }
  541. },20);
  542. }
  543. searchByTime();
  544. }
  545.  
  546. geneSelector(ele){
  547. let selector=ele.tagName;
  548. //Google id class都是隨機。百度更過分,style script順序都是隨機的
  549. //if(ele.id) selector += '#' + ele.id;
  550. //if(ele.classList) selector += [].map.call(ele.classList,d=>'.'+d).join('');
  551. let parent = ele.parentElement;
  552. if(parent){
  553. let i,j=0;
  554. for(i=0;i<parent.children.length;i++){
  555. if(parent.children[i].tagName==selector){
  556. j++;
  557. if(parent.children[i]==ele){
  558. break;
  559. }
  560. }
  561. }
  562. selector = this.geneSelector(parent) + ' > ' + selector + (parent.tagName=="HTML"?"":`:nth-of-type(${j})`);
  563. }
  564. return selector;
  565. }
  566.  
  567. getPageElement(doc, curWin, dontFind) {
  568. let pageElement=null;
  569. let self=this;
  570. if(this.curSiteRule.pageElement){
  571. pageElement=this.curSiteRule.type==0?getAllElementsByXpath(this.curSiteRule.pageElement,doc,doc):doc.querySelectorAll(this.curSiteRule.pageElement);
  572. }
  573. if((!pageElement || pageElement.length==0) && curWin && !dontFind){
  574. let body=doc.body;
  575. if(!body)return null;
  576. let bodyHeight=parseInt(curWin.getComputedStyle(body).height);
  577. function checkElement(ele){
  578. let curHeight=parseInt(curWin.getComputedStyle(ele).height);
  579. if(curHeight/bodyHeight<=0.35)return null;
  580. if(ele.children.length==0 && !self.curSiteRule.pageElement){
  581. self.curSiteRule.pageElement=self.geneSelector(ele.parentNode)+">"+ele.tagName;
  582. self.curSiteRule.type=1;
  583. debug(self.curSiteRule.pageElement);
  584. return [ele];
  585. }
  586. let i,maxHeight=curHeight*0.35,curMaxEle=null,curMaxArea=0;
  587. for(i=0;i<ele.children.length;i++){
  588. let curNode=ele.children[i];
  589. let comStyle=curWin.getComputedStyle(curNode);
  590. let h=parseInt(comStyle.height);
  591. let w=parseInt(comStyle.width);
  592. if(isNaN(h) || isNaN(w))continue;
  593. let a=h*w+h,moreChild=curNode.children[0];
  594. while(moreChild){
  595. comStyle=curWin.getComputedStyle(moreChild);
  596. let ch=parseInt(comStyle.height);
  597. let cw=parseInt(comStyle.width);
  598. if(moreChild.innerText!="" && !isNaN(ch) && !isNaN(cw)){
  599. a+=ch*cw;
  600. }
  601. moreChild=moreChild.nextElementSibling;
  602. }
  603. if(curMaxEle==null || curMaxArea<a){
  604. curHeight=h;
  605. curMaxArea=a;
  606. curMaxEle=curNode;
  607. }
  608. }
  609. if(curMaxEle && curHeight>maxHeight){
  610. return checkElement(curMaxEle);
  611. }
  612. if(!self.curSiteRule.pageElement){
  613. self.curSiteRule.pageElement=self.geneSelector(ele)+">*";
  614. self.curSiteRule.type=1;
  615. debug(self.curSiteRule.pageElement);
  616. }
  617. return ele.children;
  618. }
  619. pageElement=checkElement(body);
  620. if(pageElement)this.saveCurSiteRule();
  621. }
  622. return pageElement;
  623. }
  624.  
  625. getPage(doc){
  626. let canSave=false;//發現頁碼選擇器在其他頁對不上,還是別保存了
  627. let url=this.curUrl;
  628. let pageNum=0,preStr="",afterStr="";
  629. let pageMatch1=url.match(/(.*[a-zA-Z0-9\/][\-_](?:p|page)?)(\d+)(\.html?$|$)/i);
  630. let pageMatch2=url.match(/(.*[\?&]p(?:age)?=)(\d+)($|[#&].*)/i);
  631. if(pageMatch1){
  632. preStr=pageMatch1[1];
  633. pageNum=pageMatch1[2];
  634. afterStr=pageMatch1[3];
  635. }else if(pageMatch2){
  636. preStr=pageMatch2[1];
  637. pageNum=pageMatch2[2];
  638. afterStr=pageMatch2[3];
  639. }
  640. var curPage=doc,i,cur;
  641. let next=curPage.querySelector("a.next"),jsNext;
  642. if(!next)next=curPage.querySelector("a#next");
  643. if(!next)next=curPage.querySelector("a#rightFix");
  644. if(!next)next=curPage.querySelector("a.next_page");
  645. if(!next)next=curPage.querySelector(".next>a");
  646. if(!next)next=curPage.querySelector("#next_page");
  647. if(!next)next=curPage.querySelector(".next>button");
  648. if(next && (!next.href || /javascript:/.test(next.href))){
  649. jsNext=next;
  650. next=null;
  651. }
  652. if(!next){
  653. let pageDiv=curPage.querySelector("div.wp-pagenavi");
  654. if(pageDiv){
  655. cur=pageDiv.querySelector("span.current");
  656. next=cur.nextSibling;
  657. }else{
  658. cur=curPage.querySelector("div.article-paging>span");
  659. if(cur){
  660. next=cur.nextElementSibling;
  661. }
  662. }
  663. }
  664. if(!next){
  665. let pageDiv=curPage.querySelector("div.pages>ul");
  666. if(pageDiv){
  667. cur=pageDiv.querySelector("li>b");
  668. if(cur)next=cur.parentNode.nextElementSibling.querySelector("a");
  669. }
  670. }
  671. if(!next){
  672. let aTags=curPage.querySelectorAll("a");
  673. let nextf,nexts,nextt,nextfo;
  674. for(i=0;i<aTags.length;i++){
  675. let aTag=aTags[i];
  676. if(nextf && nexts && nextt)break;
  677. if(!nextf){
  678. if(/(\s|^)下[一1]?[页頁张張]|^next( page)?\s*$|次のページ/i.test(aTag.innerText)){
  679. if(!aTag.href || /javascript:/.test(aTag.href)){
  680. nextfo=aTag;
  681. }else{
  682. nextf=aTag;
  683. }
  684. }
  685. }
  686. if(!nexts){
  687. if(aTag.innerText=="&gt;"){
  688. if(!aTag.href || /javascript:/.test(aTag.href)){
  689. nextfo=aTag;
  690. }else{
  691. nexts=aTag;
  692. }
  693. }
  694. }
  695. if(!aTag.href || /javascript:/.test(aTag.href))continue;
  696. if(!nextt){
  697. aTag.href=aTag.href.replace(/\?&/,"?");
  698. if(aTag.innerText=="»"){
  699. nextt=aTag;
  700. }else if(aTag.href.replace(preStr,"").replace(afterStr,"")==parseInt(pageNum)+1){
  701. nextt=aTag;
  702. }else if(aTag.href.indexOf(url)!=-1 && /^[\/\?&]?[_-]?(p|page)?=?2(\?|&|$)/i.test(aTag.href.replace(url,""))){
  703. nextt=aTag;
  704. }
  705. }
  706. }
  707. next=nextf||nexts||nextt||nextfo;
  708. }
  709. if(!next)next=jsNext||curPage.querySelector('[rel="next"]');
  710. return {next:next,canSave:canSave};
  711. }
  712.  
  713. getNextLink(doc) {
  714. let nextLink=null,page;
  715. if(this.curSiteRule.pageElementByJs){
  716. this.nextLinkHref="#";
  717. return true;
  718. }else if(this.curSiteRule.nextLinkByJs){
  719. try{
  720. let targetUrl=Function("doc",'"use strict";' + this.curSiteRule.nextLinkByJs)(doc);;
  721. nextLink={href:targetUrl};
  722. }catch(e){}
  723. }else if(this.curSiteRule.nextLinkByUrl){
  724. let targetUrl=this.curUrl.replace(new RegExp(this.curSiteRule.nextLinkByUrl[0]), this.curSiteRule.nextLinkByUrl[1]);
  725. if(targetUrl != this.curUrl){
  726. let reps=targetUrl.match(/{.*?}/g);
  727. if(reps){
  728. reps.forEach(rep=>{
  729. let code=rep.replace("{","").replace("}","");
  730. let result=code.match(/^(\d+)\+1$/);
  731. if(result){
  732. result=parseInt(result[1])+1;
  733. }else{
  734. try{
  735. result=Function('"use strict";return ' + code)();;
  736. }catch(e){}
  737. }
  738. targetUrl=targetUrl.replace(rep, result);
  739. });
  740. }
  741. }
  742. nextLink={href:targetUrl};
  743. }else if(this.curSiteRule.nextLink){
  744. nextLink=this.curSiteRule.type==0?getElementByXpath(this.curSiteRule.nextLink,doc,doc):doc.querySelector(this.curSiteRule.nextLink);
  745. }
  746. if(!nextLink){
  747. page=this.getPage(doc);
  748. nextLink=page.next;
  749. if(nextLink && doc==document){
  750. let nextLinkCs=_unsafeWindow.getComputedStyle(nextLink);
  751. if(nextLinkCs.cursor=="not-allowed"){
  752. self.nextLinkHref=false;
  753. return null;
  754. }
  755. }
  756. }
  757. if(nextLink){
  758. if(!this.curSiteRule.nextLink && page && page.canSave){
  759. this.curSiteRule.nextLink=this.geneSelector(nextLink);
  760. this.curSiteRule.type=1;
  761. this.saveCurSiteRule();
  762. }
  763. }
  764. if(nextLink){
  765. this.nextLinkHref=nextLink.href?nextLink.href:"#";
  766. debug(nextLink);
  767. }else{
  768. this.nextLinkHref=false;
  769. }
  770. return nextLink;
  771. }
  772.  
  773. getInsert(refresh) {
  774. if(this.insert && !refresh){
  775. let parent=this.insert;
  776. while(parent && parent.nodeName!="BODY"){
  777. parent=parent.parentNode;
  778. }
  779. if(parent && parent.nodeName=="BODY"){
  780. return this.insert;
  781. }
  782. }
  783. if(this.curSiteRule.insert){
  784. this.insert=this.curSiteRule.type==0?getElementByXpath(this.curSiteRule.insert,document):document.querySelector(this.curSiteRule.insert);
  785. }else{
  786. let pageElement=this.getPageElement(document, _unsafeWindow);
  787. if(pageElement && pageElement.length>0){
  788. var pELast = pageElement[pageElement.length - 1];
  789. this.insert = pELast.nextSibling ? pELast.nextSibling : pELast.parentNode.appendChild(document.createTextNode(' '));
  790. }
  791. }
  792. return this.insert;
  793. }
  794.  
  795. pageAction(doc,eles){
  796. let code=this.curSiteRule.pageAction;
  797. if(code){
  798. try{
  799. Function("doc","eles",'"use strict";' + code)(doc,eles);
  800. }catch(e){}
  801. }
  802. let css=this.curSiteRule.css;
  803. if(css){
  804. _GM_addStyle(css);
  805. }
  806. [].forEach.call(eles, ele=>{
  807. [].forEach.call(ele.querySelectorAll("img"), img=>{
  808. //if(img.src==""){
  809. let realSrc;
  810. if(img.dataset && img.dataset.original){
  811. realSrc=img.dataset.original;
  812. }else if(img.dataset && img.dataset.src){
  813. realSrc=img.dataset.src;
  814. }else if(img._lazyrias && img._lazyrias.srcset){
  815. realSrc=img._lazyrias.srcset[img._lazyrias.srcset.length-1];
  816. }else if(img.dataset && img.dataset.origFile){
  817. realSrc=img.dataset.origFile;
  818. }else if(img.srcset){
  819. var srcs=img.srcset.split(","),largeSize=0;
  820. srcs.forEach(srci=>{
  821. let srcInfo=srci.trim().split(" "),curSize=parseInt(srcInfo[1]);
  822. if(srcInfo[1] && curSize>largeSize){
  823. largeSize=curSize;
  824. realSrc=srcInfo[0];
  825. }
  826. });
  827. }
  828. if(realSrc)img.src=realSrc;
  829. //}
  830. });
  831. [].forEach.call(ele.querySelectorAll("a.lazyload"), a=>{
  832. if(a.dataset.original){
  833. a.style.backgroundImage='url("'+a.dataset.original+'")';
  834. }
  835. });
  836. });
  837. let lazyImgSrc=this.curSiteRule.lazyImgSrc;
  838. if(lazyImgSrc){
  839. [].forEach.call(eles, ele=>{
  840. [].forEach.call(ele.querySelectorAll("img"), img=>{
  841. if(img[lazyImgSrc]){
  842. img.src=img[lazyImgSrc];
  843. }
  844. });
  845. });
  846. }
  847. }
  848.  
  849. initPage(callback){
  850. let self=this;
  851. this.getRule(()=>{
  852. if(self.curSiteRule){
  853. self.hpRules=self.hpRules.filter(item=>{return item&&item.url!=self.curSiteRule.url});
  854. self.hpRules.unshift(self.curSiteRule);
  855. if(self.hpRules.length>30){
  856. self.hpRules.pop();
  857. }
  858. storage.setItem("hpRules", self.hpRules);
  859. }
  860. let code=self.curSiteRule.init;
  861. if(code){
  862. try{
  863. Function('"use strict";' + code)();
  864. }catch(e){}
  865. }
  866. self.getNextLink(document);
  867. callback();
  868. });
  869. }
  870.  
  871. insertPage(doc, eles, url){
  872. this.pageDoc=doc;
  873. this.curUrl=url;
  874. this.pageAction(doc, eles);
  875. this.getNextLink(doc);
  876. this.getInsert();
  877. var self=this;
  878. if(!eles || eles.length==0 || !self.insert || !self.insert.parentNode){
  879. }else{
  880. [].forEach.call(eles, ele=>{
  881. if(self.curSiteRule.insertPos==2){
  882. self.insert.appendChild(ele.cloneNode(true));
  883. }else{
  884. self.insert.parentNode.insertBefore(ele.cloneNode(true), self.insert);
  885. }
  886. });
  887. }
  888. }
  889. }
  890. var ruleParser = new RuleParser();
  891.  
  892. var rulesData={},ruleUrls,updateDate;
  893. function initConfig(){
  894. _GM_registerMenuCommand(i18n(forceState==1?"enable":"disableSite"), ()=>{
  895. storage.setItem("forceState_"+location.host, (forceState==1?0:1));
  896. location.reload();
  897. });
  898.  
  899. _GM_registerMenuCommand(i18n(forceState==2?"cancelForceIframe":"forceIframe"), ()=>{
  900. storage.setItem("forceState_"+location.host, (forceState==2?0:2));
  901. location.reload();
  902. });
  903. var configCon,insertPos;
  904. if(location.href=="https://github.com/hoothin/UserScripts/tree/master/Pagetual"){
  905. _GM_addStyle(`
  906. p>span:nth-child(1),p>span:nth-child(2),p>span:nth-child(3){
  907. cursor: pointer;
  908. user-select: none;
  909. }
  910. p>span:nth-child(1):hover,p>span:nth-child(2):hover,p>span:nth-child(3):hover{
  911. color:red;
  912. }
  913. .updateDate{
  914. cursor: pointer;
  915. user-select: none;
  916. }
  917. .updateDate:hover{
  918. color:red;
  919. }
  920. `);
  921. configCon=document.querySelector(".markdown-body");
  922. insertPos=configCon.querySelector("hr");
  923. }else if(location.href=="https://github.com/hoothin/UserScripts/tree/master/Pagetual"){
  924. }else return;
  925. class Rulebar {
  926. init(ruleUrl){
  927. this.ruleUrl=ruleUrl;
  928. this.item=document.createElement("p");
  929. this.item.title=ruleUrl.type==0?"AutoPagerize Rules":"Pagetual Rules";
  930. this.item.dataset.id=this.ruleUrl.id;
  931. let url=document.createElement("span");
  932. url.innerHTML=ruleUrl.url;
  933. let up=document.createElement("span");
  934. up.innerHTML="↑ ";
  935. up.title=i18n("sortTitle");
  936. let down=document.createElement("span");
  937. down.innerHTML="↓ ";
  938. down.title=i18n("sortTitle");
  939. let del=document.createElement("span");
  940. del.innerHTML="× ";
  941. up.onclick=e=>{
  942. this.moveUp();
  943. };
  944. down.onclick=e=>{
  945. this.moveDown();
  946. };
  947. del.onclick=e=>{
  948. this.del();
  949. };
  950. this.item.appendChild(up);
  951. this.item.appendChild(down);
  952. this.item.appendChild(del);
  953. this.item.appendChild(url);
  954. configCon.insertBefore(this.item, insertPos);
  955. }
  956. saveSort(){
  957. let sort=[];
  958. [].forEach.call(this.item.parentNode.querySelectorAll("p[data-id]"), i=>{
  959. sort.push(i.dataset.id);
  960. });
  961. rulesData.sort=sort;
  962. storage.setItem("importRuleUrl", rulesData);
  963. }
  964. moveUp(){
  965. let preE=this.item.previousElementSibling;
  966. if(preE.tagName=="P" && preE.children.length>1){
  967. this.item.parentNode.insertBefore(this.item,preE);
  968. this.saveSort();
  969. }
  970. }
  971. moveDown(){
  972. let nextE=this.item.nextElementSibling;
  973. if(nextE.tagName=="P" && nextE.children.length>1){
  974. this.item.parentNode.insertBefore(nextE,this.item);
  975. this.saveSort();
  976. }
  977. }
  978. del(){
  979. if(this.ruleUrl.id<2){
  980. alert(i18n("cantDel"));
  981. }else if(window.confirm(i18n("confirmDel"))){
  982. for(let u=0;u<rulesData.urls.length;u++){
  983. if(this.ruleUrl.id==rulesData.urls[u].id){
  984. rulesData.urls.splice(u,1);
  985. break;
  986. }
  987. }
  988. for(let u=0;u<rulesData.sort.length;u++){
  989. if(this.ruleUrl.id==rulesData.sort[u]){
  990. rulesData.sort.splice(u,1);
  991. break;
  992. }
  993. }
  994. storage.setItem("importRuleUrl", rulesData);
  995. ruleParser.rules=ruleParser.rules.filter(item=>{return item.from!=this.ruleUrl.id});
  996. storage.setItem("rules", ruleParser.rules);
  997. this.item.parentNode.removeChild(this.item);
  998. //location.reload();
  999. }
  1000. }
  1001. }
  1002. let updateP=document.createElement("p"),i=0;
  1003. let now=new Date().getTime(),inUpdate=false;
  1004.  
  1005.  
  1006. let pastDate=(new Date(updateDate)).toString(),passStr;
  1007. let passTime=(now-updateDate)/1000;
  1008. if(passTime<60){
  1009. passStr=i18n("passSec", passTime);
  1010. }else if(passTime<60*60){
  1011. passStr=i18n("passMin", parseInt(passTime/60));
  1012. }else if(passTime<60*60*24){
  1013. passStr=i18n("passHour", parseInt(passTime/3600));
  1014. }else{
  1015. passStr=i18n("passDay", parseInt(passTime/86400));
  1016. }
  1017.  
  1018.  
  1019. updateP.className="updateDate";
  1020. updateP.innerHTML=passStr;
  1021. updateP.title=i18n("update")+" - "+pastDate;
  1022. updateP.onclick=e=>{
  1023. if(inUpdate)return;
  1024. inUpdate=true;
  1025. let ruleIndex=0;
  1026. storage.setItem("hpRules", []);
  1027. function addNextRule(){
  1028. if(ruleIndex>=ruleUrls.length){
  1029. storage.setItem("ruleLastUpdate", now);
  1030. alert(i18n("updateSucc"));
  1031. inUpdate=false;
  1032. updateP.innerHTML=i18n("passSec", 0);
  1033. updateP.title=i18n("update");
  1034. }else{
  1035. let rule=ruleUrls[ruleIndex++];
  1036. ruleParser.addRuleByUrl(rule.url, rule.type, rule.id, ()=>{
  1037. addNextRule();
  1038. })
  1039. }
  1040. }
  1041. addNextRule();
  1042. alert(i18n("beginUpdate"));
  1043. };
  1044. configCon.insertBefore(updateP, insertPos);
  1045. if(ruleUrls){
  1046. ruleUrls.forEach(ruleUrl=>{
  1047. var rulebar=new Rulebar();
  1048. rulebar.init(ruleUrl);
  1049. });
  1050. }
  1051. let customUrlsTitle=document.createElement("h2");
  1052. customUrlsTitle.innerHTML=i18n("customUrls");
  1053. configCon.insertBefore(customUrlsTitle, insertPos);
  1054. let customUrlsInput=document.createElement("textarea");
  1055. customUrlsInput.style.width="100%";
  1056. customUrlsInput.placeholder="0|http://wedata.net/databases/AutoPagerize/items_all.json";
  1057. configCon.insertBefore(customUrlsInput, insertPos);
  1058.  
  1059. let opacityTitle=document.createElement("h2");
  1060. opacityTitle.innerHTML=i18n("opacity");
  1061. let opacityInput=document.createElement("input");
  1062. opacityInput.value=rulesData.opacity*100;
  1063. opacityInput.type="number";
  1064. opacityInput.style.width="50px";
  1065. opacityInput.style.margin="0 0 0 10px";
  1066. opacityTitle.appendChild(opacityInput);
  1067. configCon.insertBefore(opacityTitle, insertPos);
  1068.  
  1069.  
  1070. let hideBarTitle=document.createElement("h2");
  1071. hideBarTitle.innerHTML=i18n("hideBar");
  1072. let hideBarInput=document.createElement("input");
  1073. hideBarInput.type="checkbox";
  1074. hideBarInput.style.width="50px";
  1075. hideBarInput.style.height="20px";
  1076. hideBarInput.checked=rulesData.hideBar;
  1077. hideBarTitle.appendChild(hideBarInput);
  1078. configCon.insertBefore(hideBarTitle, insertPos);
  1079.  
  1080. let customRulesTitle=document.createElement("h2");
  1081. customRulesTitle.innerHTML=i18n("customRules")
  1082. configCon.insertBefore(customRulesTitle, insertPos);
  1083. let customRulesInput=document.createElement("textarea");
  1084. customRulesInput.style.width="100%";
  1085. customRulesInput.style.height="500px";
  1086. 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]`;
  1087. customRulesInput.value=getFormatJSON(ruleParser.customRules);
  1088. configCon.insertBefore(customRulesInput, insertPos);
  1089. let saveBtn=document.createElement("button");
  1090. saveBtn.innerHTML=i18n("save");
  1091. saveBtn.style.width="100%";
  1092. configCon.insertBefore(saveBtn, insertPos);
  1093. saveBtn.onclick=e=>{
  1094. try{
  1095. storage.setItem("hpRules", []);
  1096. if(customRulesInput.value==""){
  1097. storage.setItem("customRules", "");
  1098. }else{
  1099. let customRules=JSON.parse(customRulesInput.value);
  1100. debug(customRules);
  1101. storage.setItem("customRules", customRules);
  1102. }
  1103. }catch(e){
  1104. debug(e);
  1105. alert("JSON error, check again!");
  1106. return;
  1107. }
  1108. rulesData.opacity=opacityInput.value/100;
  1109. rulesData.hideBar=hideBarInput.checked;
  1110. storage.setItem("importRuleUrl", rulesData);
  1111. let customUrls=customUrlsInput.value.trim();
  1112. if(customUrls){
  1113. customUrls=customUrls.split(/\n/);
  1114. for(let c=0;c<customUrls.length;c++){
  1115. let urlArr=customUrls[c].split("|"),url,type=1;
  1116. if(urlArr.length==1){
  1117. url=urlArr[0].trim();
  1118. if(!/^http/.test(url)){
  1119. alert("Wrong url, check again!");
  1120. return;
  1121. }
  1122. }else if(urlArr.length==2){
  1123. type=urlArr[0].trim();
  1124. url=urlArr[1].trim();
  1125. if(!/^http/.test(url)){
  1126. alert("Wrong url, check again!");
  1127. return;
  1128. }
  1129. }else{
  1130. break;
  1131. }
  1132. let maxId=0,hasUrl=false;;
  1133. if(!rulesData.urls){
  1134. rulesData.urls=[];
  1135. maxId=1;
  1136. }else{
  1137. rulesData.urls.forEach(u=>{
  1138. if(maxId<u.id){
  1139. maxId=u.id;
  1140. }
  1141. if(u.url==url){
  1142. hasUrl=true;
  1143. }
  1144. });
  1145. if(hasUrl)break;
  1146. }
  1147. rulesData.urls.push({id:maxId+1,url:url,type:type});
  1148. rulesData.sort.push(maxId+1);
  1149. storage.setItem("importRuleUrl", rulesData);
  1150. }
  1151. }
  1152. alert("Modified successfully");
  1153. location.reload();
  1154. };
  1155. }
  1156.  
  1157. function objIsArr(obj) {
  1158. return obj &&
  1159. typeof obj === 'object' &&
  1160. typeof obj.length === 'number' &&
  1161. !(obj.propertyIsEnumerable('length'));
  1162. }
  1163.  
  1164. function getFormatJSON(obj){
  1165. if(!objIsArr(obj))return "";
  1166. let ret="[\n";
  1167. let len=obj.length,i=0,isLast;
  1168. obj.forEach(item=>{
  1169. ret+=" {\n";
  1170. let iLen=Object.keys(item).length,j=0;
  1171. for(let key in item){
  1172. isLast=(++j)==iLen;
  1173. let value=item[key];
  1174. if(objIsArr(value)){
  1175. let vstr="[",v=0,vIsLast=false;
  1176. value.forEach(vi=>{
  1177. vIsLast=(++v)==value.length;
  1178. vstr+="\""+vi.replace(/\\/g,"\\\\")+"\""+(vIsLast?"":",");
  1179. });
  1180. vstr+="]";
  1181. ret+=" \""+key+"\":"+vstr+""+(isLast?"":",")+"\n";
  1182. }else{
  1183. if(typeof value=="string"){
  1184. value=value.replace(/\\/g,"\\\\");
  1185. }
  1186. ret+=" \""+key+"\":\""+value+"\""+(isLast?"":",")+"\n";
  1187. }
  1188. }
  1189. isLast=(++i)==len;
  1190. ret+=" }"+(isLast?"":",")+"\n";
  1191. });
  1192. ret+="]";
  1193. return ret;
  1194. }
  1195.  
  1196. function initRules(callback) {
  1197. /*0 wedata格式,1 pagetual格式*/
  1198. ruleUrls=[
  1199. {
  1200. id:0,
  1201. url:'https://raw.githubusercontent.com/hoothin/UserScripts/master/Pagetual/pagetualRules.json',
  1202. type:1
  1203. },
  1204. {
  1205. id:1,
  1206. url:'http://wedata.net/databases/AutoPagerize/items_all.json',
  1207. type:0,
  1208. }
  1209. ];var i=0,j=0;
  1210.  
  1211. ruleParser.initSavedRules(()=>{
  1212. storage.getItem("importRuleUrl", data=>{
  1213. if(data){
  1214. rulesData=data;
  1215. if(data.urls)ruleUrls=ruleUrls.concat(data.urls);
  1216. if(data.sort){
  1217. let urls=[];
  1218. data.sort.forEach(id=>{
  1219. for(let s=0;s<ruleUrls.length;s++){
  1220. if(id==ruleUrls[s].id){
  1221. urls.push(ruleUrls[s]);
  1222. break;
  1223. }
  1224. }
  1225. });
  1226. ruleUrls=urls;
  1227. }
  1228. }
  1229. if(typeof(rulesData.opacity)=="undefined"){
  1230. rulesData.opacity=0.3;
  1231. }
  1232. if(typeof(rulesData.hideBar)=="undefined"){
  1233. rulesData.hideBar=true;
  1234. }
  1235. storage.getItem("forceState_"+location.host, v=>{
  1236. storage.getItem("ruleLastUpdate", date=>{
  1237. forceState=v||0;
  1238. updateDate=date;
  1239. initConfig();
  1240. if(forceState==1)return;
  1241. let now=new Date().getTime();
  1242. if(!date || now-date>3*24*60*60*1000){
  1243. storage.setItem("ruleLastUpdate", now);
  1244. storage.setItem("hpRules", []);
  1245. let ruleIndex=0;
  1246. function addNextRule(){
  1247. if(ruleIndex>=ruleUrls.length){
  1248. callback();
  1249. }else{
  1250. let rule=ruleUrls[ruleIndex++];
  1251. ruleParser.addRuleByUrl(rule.url, rule.type, rule.id, ()=>{
  1252. addNextRule();
  1253. })
  1254. }
  1255. }
  1256. addNextRule();
  1257. }else{
  1258. callback();
  1259. }
  1260. });
  1261. });
  1262. });
  1263. });
  1264. }
  1265.  
  1266. function requestDoc(url, callback){
  1267. _GM_xmlhttpRequest({
  1268. url: url,
  1269. method: 'GET',
  1270. overrideMimeType:"text/html;charset="+document.charset,
  1271. onload: function(res) {
  1272. var doc=null;
  1273. try {
  1274. doc=document.implementation.createHTMLDocument('');
  1275. doc.documentElement.innerHTML=res.response;
  1276. }
  1277. catch (e) {
  1278. debug('parse error'+e.toString());
  1279. }
  1280. let pageElement=ruleParser.getPageElement(doc);
  1281. //只有1的話怕不是圖片哦
  1282. if(pageElement && (pageElement.length>1 || (pageElement.length==1 && pageElement[0].tagName!="IMG") )){
  1283. callback(pageElement);
  1284. ruleParser.insertPage(doc, pageElement, url);
  1285. }else{
  1286. requestFromIframe(url, (doc, eles)=>{
  1287. callback(eles);
  1288. if(eles){
  1289. ruleParser.insertPage(doc, eles, url);
  1290. }
  1291. });
  1292. }
  1293. },
  1294. onerror: function(e){
  1295. debug(e);
  1296. callback(false);
  1297. }
  1298. });
  1299. }
  1300.  
  1301. var failFromIframe=0;
  1302. function requestFromIframe(url, callback){
  1303. let orgPage,curPage;
  1304. let iframe = document.createElement('iframe');
  1305. iframe.name = 'pagetual-iframe';
  1306. iframe.width = '100%';
  1307. iframe.height = '0';
  1308. iframe.frameBorder = '0';
  1309. iframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  1310. iframe.style.cssText = 'margin:0!important;padding:0!important;visibility:hidden!important;';
  1311. iframe.addEventListener("load", e=>{
  1312. setTimeout(()=>{
  1313. let tryTimes=0;
  1314. function checkIframe(){
  1315. try{
  1316. let doc=iframe.contentWindow.document;
  1317. let eles=ruleParser.getPageElement(doc, iframe.contentWindow);
  1318. if(eles && eles.length>0){
  1319. callback(doc, eles);
  1320. }else if(tryTimes++ < 2){
  1321. setTimeout(()=>{
  1322. checkIframe();
  1323. },500);
  1324. return;
  1325. }else{
  1326. if(failFromIframe++ > 2){
  1327. failFromIframe=0;
  1328. isPause=true;
  1329. callback(false, false);
  1330. }else{
  1331. callback(false, false);
  1332. }
  1333. }
  1334. }catch(e){
  1335. isPause=true;
  1336. callback(false, false);
  1337. }
  1338. document.body.removeChild(iframe);
  1339. }
  1340. checkIframe();
  1341. },300);
  1342. });
  1343. iframe.src=url;
  1344. document.body.appendChild(iframe);
  1345. }
  1346.  
  1347. function initPage(){
  1348. ruleParser.initPage(()=>{
  1349. initListener();
  1350. nextPage();
  1351. });
  1352. }
  1353.  
  1354. function initView(){
  1355. _GM_addStyle(`
  1356. .pagetual_pageBar.stop {
  1357. -webkit-filter: invert(100%);
  1358. filter: invert(100%);
  1359. opacity: 1;
  1360. }
  1361. .pagetual_pageBar.hide {
  1362. display: none!important;
  1363. }
  1364. .pagetual_pageBar {
  1365. opacity: ${rulesData.opacity};
  1366. display: ${rulesData.opacity==0?"none!important":"unset"};
  1367. }
  1368. .pagetual_pageBar:hover {
  1369. opacity: 1;
  1370. }
  1371. .pagetual_pageBar>span {
  1372. vertical-align: super;
  1373. }
  1374.  
  1375. .pagetual_pageBar>span>svg:hover {
  1376. animation: touhouAni 1s infinite;
  1377. }
  1378.  
  1379. @keyframes touhouAni{
  1380. from {transform: rotate(0deg) scale3d(1.2, 1.2, 1.2);}
  1381. to {transform: rotate(360deg);}
  1382. }
  1383. @-webkit-keyframes touhouAni{
  1384. from {transform: rotate(0deg) scale3d(1.2, 1.2, 1.2);}
  1385. to {transform: rotate(360deg);}
  1386. }
  1387. `);
  1388. }
  1389. var loading=document.createElement("div");
  1390. loading.style.cssText="display:none;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;";
  1391. loading.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><svg width="120" height="30" viewBox="0 0 120 30" fill="#000000A0" style="display: block;position: initial;margin: auto;shape-rendering: auto;vertical-align: middle;visibility: visible;width: initial;height: initial;"><circle cx="15" cy="15" r="15"><animate attributeName="r" from="15" to="15" begin="0s" dur="0.8s" values="15;9;15" calcMode="linear" repeatCount="indefinite"></animate><animate attributeName="fill-opacity" from="1" to="1" begin="0s" dur="0.8s" values="1;.5;1" calcMode="linear" repeatCount="indefinite"></animate></circle><circle cx="60" cy="15" r="9" fill-opacity="0.3"><animate attributeName="r" from="9" to="9" begin="0s" dur="0.8s" values="9;15;9" calcMode="linear" repeatCount="indefinite"></animate><animate attributeName="fill-opacity" from="0.5" to="0.5" begin="0s" dur="0.8s" values=".5;1;.5" calcMode="linear" repeatCount="indefinite"></animate></circle><circle cx="105" cy="15" r="15"><animate attributeName="r" from="15" to="15" begin="0s" dur="0.8s" values="15;9;15" calcMode="linear" repeatCount="indefinite"></animate><animate attributeName="fill-opacity" from="1" to="1" begin="0s" dur="0.8s" values="1;.5;1" calcMode="linear" repeatCount="indefinite"></animate></circle></svg>`;
  1392. document.body.appendChild(loading);
  1393.  
  1394. var upSvg=`<svg 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>`;
  1395. var downSvg=`<svg 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>`;
  1396. var pageBarStyle=`padding:0;box-shadow: 0px 0px 10px 0px #000000aa;border-radius: 20px;background-color: rgb(240 240 240 / 80%);visibility: visible; position: initial; width: auto; height: 30px; float: none; clear: both; margin: 20px auto; text-align: center; display: block;`;
  1397. 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);`;
  1398.  
  1399. var isPause=false,isLoading=false,curPage=1,forceState=0;
  1400.  
  1401. function changeStop(stop, hide){
  1402. isPause=stop;
  1403. [].forEach.call(document.querySelectorAll(".pagetual_pageBar"), bar=>{
  1404. if(isPause){
  1405. bar.classList.add("stop");
  1406. if(hide)bar.classList.add("hide");
  1407. }else{
  1408. bar.classList.remove("stop");
  1409. if(hide)bar.classList.remove("hide");
  1410. }
  1411. });
  1412. }
  1413.  
  1414. function initListener(){
  1415. document.addEventListener('scroll', e=>{
  1416. setTimeout(()=>{
  1417. if(!isPause && !isLoading){
  1418. let scrolly=window.scrollY;
  1419. let windowHeight=window.innerHeight;
  1420. let scrollH=Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
  1421. if(scrollH-scrolly-windowHeight<1000){
  1422. nextPage();
  1423. }
  1424. }
  1425. },100);
  1426. }, false);
  1427. document.addEventListener('dblclick', e=>{
  1428. setTimeout(()=>{
  1429. changeStop(!isPause, rulesData.hideBar);
  1430. },200);
  1431. });
  1432. }
  1433.  
  1434. function createPageBar(url){
  1435. let insert=ruleParser.getInsert();
  1436. if(!insert || !insert.parentNode)return;
  1437. curPage++;
  1438. let inTable=insert.tagName=="TR" || insert.previousElementSibling.tagName=="TR" || insert.tagName=="TBODY" || insert.previousElementSibling.tagName=="TBODY";
  1439. let inLi=insert.tagName=="LI" || insert.previousElementSibling.tagName=="LI";
  1440. let pageBar=document.createElement(inTable?"tr":"div");
  1441. let upSpan=document.createElement("span");
  1442. let downSpan=document.createElement("span");
  1443. let pageText=document.createElement("a");
  1444. pageBar.className="pagetual_pageBar";
  1445. pageBar.id="pagetual_pageBar";
  1446. if(isPause){
  1447. pageBar.classList.add("stop");
  1448. }
  1449. pageBar.style.cssText=pageBarStyle;
  1450. pageBar.title=i18n(isPause?"enable":"disable");
  1451. upSpan.innerHTML=upSvg;
  1452. upSpan.title=i18n("toTop");
  1453. downSpan.innerHTML=downSvg;
  1454. downSpan.title=i18n("toBottom");
  1455. pageText.href=url;
  1456. pageText.style=pageTextStyle;
  1457. pageText.innerHTML="Page "+curPage;
  1458. pageText.title=i18n("current");
  1459. pageBar.appendChild(upSpan);
  1460. pageBar.appendChild(pageText);
  1461. pageBar.appendChild(downSpan);
  1462. if(inTable){
  1463. let example=(insert.tagName=="TR" || insert.tagName=="TBODY")?insert:insert.previousElementSibling;
  1464. let tdNum=example.tagName=="TR"?example.children.length:example.querySelector("tr").children.length;
  1465. pageBar.style.display="table-row";
  1466. let td=document.createElement("td");
  1467. td.colSpan=tdNum;
  1468. td.appendChild(upSpan);
  1469. td.appendChild(pageText);
  1470. td.appendChild(downSpan);
  1471. pageBar.appendChild(td);
  1472. }
  1473.  
  1474. upSpan.addEventListener("click", e=>{
  1475. changeStop(true);
  1476. document.body.scrollTop=0;
  1477. document.documentElement.scrollTop=0;
  1478. e.preventDefault();
  1479. e.stopPropagation();
  1480. });
  1481. downSpan.addEventListener("click", e=>{
  1482. changeStop(true);
  1483. document.body.scrollTop=9999999;
  1484. document.documentElement.scrollTop=9999999;
  1485. e.preventDefault();
  1486. e.stopPropagation();
  1487. });
  1488. pageBar.addEventListener("click", e=>{
  1489. changeStop(!isPause);
  1490. pageBar.title=i18n(isPause?"enable":"disable");
  1491. });
  1492. pageText.addEventListener("click", e=>{
  1493. e.stopPropagation();
  1494. });
  1495. pageBar.style.width=parseInt(_unsafeWindow.getComputedStyle(insert.parentNode).width)*.9+"px";
  1496. if(inLi){
  1497. pageBar.style.width="auto";
  1498. pageBar.style.minWidth="200px";
  1499. let line=document.createElement("li");
  1500. line.appendChild(pageBar);
  1501. if(ruleParser.curSiteRule.insertPos==2){
  1502. insert.appendChild(line);
  1503. }else{
  1504. insert.parentNode.insertBefore(line, insert);
  1505. }
  1506. }else{
  1507. if(ruleParser.curSiteRule.insertPos==2){
  1508. insert.appendChild(pageBar);
  1509. }else{
  1510. insert.parentNode.insertBefore(pageBar, insert);
  1511. }
  1512. }
  1513. return pageBar;
  1514. }
  1515.  
  1516. var emuIframe;
  1517. function emuPage(callback){
  1518. let orgPage,curPage,iframeDoc,times=0;
  1519. function checkPage(){
  1520. if(isPause)return;
  1521. try{
  1522. iframeDoc=emuIframe.contentDocument || emuIframe.contentWindow.document;
  1523. }catch(e){
  1524. isPause=true;
  1525. callback(false, false);
  1526. return;
  1527. }
  1528. if(!orgPage){
  1529. orgPage=ruleParser.getPageElement(iframeDoc, iframeDoc.defaultView, true);
  1530. if(orgPage)orgPage=orgPage[0];
  1531. let nextLink=ruleParser.getNextLink(iframeDoc);
  1532. if(orgPage && nextLink){
  1533. let display=iframeDoc.defaultView.getComputedStyle(nextLink).display;
  1534. if(display=="none"){
  1535. isPause=true;
  1536. callback(false, false);
  1537. }else{
  1538. nextLink.click();
  1539. setTimeout(()=>{
  1540. checkPage();
  1541. },1000);
  1542. }
  1543. }else{
  1544. isPause=true;
  1545. callback(false, false);
  1546. }
  1547. return;
  1548. }
  1549. if(times++ > 10){
  1550. isPause=true;
  1551. callback(false, false);
  1552. return;
  1553. }
  1554. let eles=ruleParser.getPageElement(iframeDoc, iframeDoc.defaultView, true);
  1555. if(!eles || eles.length==0){
  1556. isPause=true;
  1557. callback(false, false);
  1558. }else if(orgPage == eles[0]){
  1559. setTimeout(()=>{
  1560. checkPage();
  1561. },500);
  1562. }else{
  1563. callback(iframeDoc, eles);
  1564. }
  1565. }
  1566. if(!emuIframe){
  1567. emuIframe = document.createElement('iframe');
  1568. emuIframe.name = 'pagetual-iframe';
  1569. emuIframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  1570. emuIframe.width = '100%';
  1571. emuIframe.height = '0';
  1572. emuIframe.frameBorder = '0';
  1573. emuIframe.style.cssText = 'margin:0!important;padding:0!important;visibility:hidden!important;';
  1574. emuIframe.addEventListener("load", e=>{
  1575. setTimeout(()=>{
  1576. checkPage();
  1577. },300);
  1578. });
  1579. emuIframe.src=location.href;
  1580. document.body.appendChild(emuIframe);
  1581. }else{
  1582. checkPage();
  1583. }
  1584. }
  1585.  
  1586. function forceIframe(url, callback){
  1587. let curIframe = document.createElement('iframe');
  1588. curIframe.name = 'pagetual-iframe';
  1589. curIframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  1590. curIframe.frameBorder = '0';
  1591. curIframe.scrolling="no";
  1592. 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;';
  1593. curIframe.addEventListener("load", e=>{
  1594. let iframeDoc=curIframe.contentDocument || curIframe.contentWindow.document;
  1595. //let eles=ruleParser.getPageElement(iframeDoc, curIframe.contentWindow);
  1596. //if(eles && eles.length>0){
  1597. ruleParser.insertPage(iframeDoc, [], url);
  1598. callback(curIframe, true);
  1599. curIframe.style.height=iframeDoc.body.scrollHeight+"px";
  1600. curIframe.style.width=iframeDoc.body.scrollWidth+"px";
  1601. setTimeout(()=>{
  1602. curIframe.style.height=iframeDoc.body.scrollHeight+"px";
  1603. curIframe.style.width=iframeDoc.body.scrollWidth+"px";
  1604. },300);
  1605. //curIframe.scrollIntoView();
  1606. /*}else{
  1607. isPause=true;
  1608. callback(false, false);
  1609. curIframe.parentNode.removeChild(curIframe);
  1610. }*/
  1611. });
  1612. curIframe.src=url;
  1613. let insert=ruleParser.getInsert();
  1614. document.body.appendChild(curIframe);
  1615. return curIframe;
  1616. }
  1617.  
  1618. function nextPage(){
  1619. if(isPause || isLoading)return;
  1620. let nextLink=ruleParser.nextLinkHref;
  1621. let insert=ruleParser.getInsert();
  1622. if(nextLink && insert){
  1623. let isJs=/^(javascript|#)/.test(nextLink.replace(location.href,""));
  1624. if(location.protocol=="https:" && /^http:/.test(nextLink)){
  1625. nextLink=nextLink.replace(/^http/,"https");
  1626. }
  1627. isLoading=true;
  1628. loading.style.display="";
  1629. if(ruleParser.curSiteRule.pageElementByJs){
  1630. var over=ele=>{
  1631. isLoading=false;
  1632. loading.style.display="none";
  1633. if(ele){
  1634. createPageBar(nextLink);
  1635. ruleParser.insertPage(null, ele, null);
  1636. }else{
  1637. isPause=true;
  1638. }
  1639. };
  1640. try{
  1641. Function("over",'"use strict";' + ruleParser.curSiteRule.pageElementByJs)(over);
  1642. }catch(e){}
  1643. }else if(ruleParser.curSiteRule.action==1 && !isJs){
  1644. requestFromIframe(nextLink, (doc, eles)=>{
  1645. isLoading=false;
  1646. loading.style.display="none";
  1647. if(eles){
  1648. createPageBar(nextLink);
  1649. ruleParser.insertPage(doc, eles, nextLink);
  1650. }
  1651. });
  1652. }else if(forceState==2 && !isJs){
  1653. forceIframe(nextLink, (iframe, eles)=>{
  1654. isLoading=false;
  1655. loading.style.display="none";
  1656. //if(eles){
  1657. let pageBar=createPageBar(nextLink);
  1658. iframe.parentNode.insertBefore(pageBar, iframe);
  1659. //}
  1660. });
  1661. }else{
  1662. if(!isJs){
  1663. requestDoc(nextLink, (eles)=>{
  1664. isLoading=false;
  1665. loading.style.display="none";
  1666. if(eles){
  1667. createPageBar(nextLink);
  1668. }
  1669. });
  1670. }else{
  1671. emuPage((doc, eles)=>{
  1672. isLoading=false;
  1673. loading.style.display="none";
  1674. if(eles){
  1675. createPageBar(nextLink);
  1676. ruleParser.insertPage(doc, eles, "");
  1677. }
  1678. });
  1679. }
  1680. }
  1681. }
  1682. }
  1683.  
  1684. function init(){
  1685. initRules(()=>{
  1686. initView();
  1687. initPage();
  1688. });
  1689. }
  1690. init();
  1691. })();