东方永页机

自动翻页

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

  1. // ==UserScript==
  2. // @name Pagetual
  3. // @name:zh-CN 东方永页机
  4. // @name:zh-TW 東方永頁機
  5. // @namespace hoothin
  6. // @version 0.2
  7. // @description Simple Auto Page
  8. // @description:zh-CN 自动翻页
  9. // @description:zh-TW 自動翻頁
  10. // @author hoothin
  11. // @include http*
  12. // @license MIT
  13. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  14. // @grant GM_xmlhttpRequest
  15. // @grant GM_registerMenuCommand
  16. // @grant GM_notification
  17. // @grant GM_getValue
  18. // @grant GM_setValue
  19. // @grant GM_addStyle
  20. // @grant GM.xmlhttpRequest
  21. // @grant GM.registerMenuCommand
  22. // @grant GM.notification
  23. // @grant GM.getValue
  24. // @grant GM.setValue
  25. // @grant GM.addStyle
  26. // @connect wedata.net
  27. // @connect githubusercontent.com
  28. // ==/UserScript==
  29.  
  30. (function() {
  31. 'use strict';
  32.  
  33. if (window.name === 'pagetual-iframe') {
  34. var domloaded = function (){
  35. window.scroll(window.scrollX, 99999);
  36. window.parent.postMessage('pagetual-iframe:DOMLoaded', '*');
  37. };
  38. if(window.opera){
  39. document.addEventListener('DOMContentLoaded', domloaded, false);
  40. } else {
  41. domloaded();
  42. }
  43. return;
  44. }
  45.  
  46. if(window.top != window.self){
  47. return;
  48. }
  49.  
  50. const lang = navigator.appName=="Netscape"?navigator.language:navigator.userLanguage;
  51. var i18n=(name, param)=>{
  52. let config={};
  53. switch (lang){
  54. case "zh-CN":
  55. case "zh-SG":
  56. config={
  57. saveCurrent:"保存当前设置",
  58. disable:"暂时禁用",
  59. enable:"启用翻页",
  60. toTop:"回到顶部",
  61. toBottom:"前往页尾",
  62. current:"当前页",
  63. forceIframe:"强制拼接",
  64. configure:"打开配置页",
  65. update:"立即更新规则",
  66. passSec:"更新于 #t# 秒前",
  67. passMin:"更新于 #t# 分钟前",
  68. passHour:"更新于 #t# 小时前",
  69. passDay:"更新于 #t# 天前",
  70. cantDel:"无法删除内置规则",
  71. confirmDel:"是否确认要删除此规则?",
  72. updateSucc:"更新成功",
  73. beginUpdate:"正在更新,请稍候",
  74. customUrls:"导入规则url,一行一条,AutoPagerize 格式规则需要以\"0|\"开头",
  75. customRules:"输入【东方永页机】格式的自定义规则",
  76. save:"保存设置"
  77. };
  78. break;
  79. case "zh-TW":
  80. case "zh-HK":
  81. config={
  82. disable:"暫時禁用",
  83. enable:"啟用翻頁",
  84. toTop:"回到頂部",
  85. toBottom:"前往頁尾",
  86. current:"當前頁",
  87. forceIframe:"強制拼接",
  88. configure:"打開配置頁",
  89. update:"立即更新規則",
  90. passSec:"更新于 #t# 秒前",
  91. passMin:"更新于 #t# 分鐘前",
  92. passHour:"更新于 #t# 小時前",
  93. passDay:"更新于 #t# 天前",
  94. cantDel:"無法刪除内置規則",
  95. confirmDel:"是否確認要刪除此規則?",
  96. updateSucc:"更新成功",
  97. beginUpdate:"正在更新,請稍候",
  98. customUrls:"導入規則url,一行一條,AutoPagerize 格式規則需要以\"0|\"開頭",
  99. customRules:"輸入【東方永頁機】格式的自定義規則",
  100. save:"存儲設置"
  101. };
  102. break;
  103. default:
  104. config={
  105. disable:"Disable",
  106. enable:"Enable",
  107. toTop:"To Top",
  108. toBottom:"To Bottom",
  109. current:"Current Page",
  110. forceIframe:"Force to join",
  111. configure:"Configure",
  112. update:"Update rules from url now",
  113. passSec:"Updated #t# seconds ago",
  114. passMin:"Updated #t# minutes ago",
  115. passHour:"Updated #t# hours ago",
  116. passDay:"Updated #t# days ago",
  117. cantDel:"Can't delete buildin rules",
  118. confirmDel:"Are you sure you want to delete this rule?",
  119. updateSucc:"Update succeeded",
  120. beginUpdate:"Begin update, wait a minute please",
  121. customUrls:"Import rule url, One url per line, rules on AutoPagerize format need to start with \"0|\"",
  122. customRules:"Input custom rules with [Pagetual] format",
  123. save:"Save"
  124. };
  125. break;
  126. }
  127. return config[name]?config[name].replace("#t#",param):name;
  128. };
  129.  
  130. var enableDebug=true;
  131. var debug=str=>{
  132. if(enableDebug){
  133. console.debug(str);
  134. }
  135. };
  136.  
  137. var _GM_xmlhttpRequest,_GM_registerMenuCommand,_GM_notification,_GM_addStyle;
  138. if(typeof GM_xmlhttpRequest!='undefined'){
  139. _GM_xmlhttpRequest=GM_xmlhttpRequest;
  140. }else if(typeof GM!='undefined' && typeof GM.xmlhttpRequest!='undefined'){
  141. _GM_xmlhttpRequest=GM.xmlhttpRequest;
  142. }
  143. if(typeof GM_registerMenuCommand!='undefined'){
  144. _GM_registerMenuCommand=GM_registerMenuCommand;
  145. }else if(typeof GM!='undefined' && typeof GM.registerMenuCommand!='undefined'){
  146. _GM_registerMenuCommand=GM.registerMenuCommand;
  147. }
  148. if(typeof GM_notification!='undefined'){
  149. _GM_notification=GM_notification;
  150. }else if(typeof GM!='undefined' && typeof GM.notification!='undefined'){
  151. _GM_notification=GM.notification;
  152. }
  153. if(typeof GM_addStyle!='undefined'){
  154. _GM_addStyle=GM_addStyle;
  155. }else if(typeof GM!='undefined' && typeof GM.addStyle!='undefined'){
  156. _GM_addStyle=GM.addStyle;
  157. }
  158.  
  159. if(typeof _GM_xmlhttpRequest=='undefined')_GM_xmlhttpRequest=(f)=>{};
  160. if(typeof _GM_registerMenuCommand=='undefined')_GM_registerMenuCommand=(s,f)=>{};
  161. if(typeof _GM_notification=='undefined')_GM_notification=(s)=>{};
  162. var _unsafeWindow=(typeof unsafeWindow=='undefined')?window:unsafeWindow;
  163. var storage={
  164. supportGM: typeof GM_getValue=='function' && typeof GM_getValue('a','b')!='undefined',
  165. supportGMPromise: typeof GM!='undefined' && typeof GM.getValue=='function' && typeof GM.getValue('a','b')!='undefined',
  166. mxAppStorage:(function(){
  167. try{
  168. return window.external.mxGetRuntime().storage;
  169. }catch(e){
  170. }
  171. })(),
  172. operaUJSStorage:(function(){
  173. try{
  174. return window.opera.scriptStorage;
  175. }catch(e){
  176. }
  177. })(),
  178. setItem:function(key,value){
  179. if(this.operaUJSStorage){
  180. this.operaUJSStorage.setItem(key,value);
  181. }else if(this.mxAppStorage){
  182. this.mxAppStorage.setConfig(key,value);
  183. }else if(this.supportGM){
  184. GM_setValue(key,value);
  185. }else if(this.supportGMPromise){
  186. GM.setValue(key,value);
  187. }else if(window.localStorage){
  188. window.localStorage.setItem(key,value);
  189. }
  190. },
  191. getItem:function(key,cb){
  192. var value;
  193. if(this.operaUJSStorage){
  194. value=this.operaUJSStorage.getItem(key);
  195. }else if(this.mxAppStorage){
  196. value=this.mxAppStorage.getConfig(key);
  197. }else if(this.supportGM){
  198. value=GM_getValue(key);
  199. }else if(this.supportGMPromise){
  200. value=GM.getValue(key).then(v=>{cb(v)});
  201. return;
  202. }else if(window.localStorage){
  203. value=window.localStorage.getItem(key);
  204. };
  205. cb(value);
  206. }
  207. };
  208. _GM_registerMenuCommand(i18n("forceIframe"), ()=>{
  209. if(ruleParser.curSiteRule.action==2){
  210. ruleParser.curSiteRule.action=0;
  211. }else{
  212. ruleParser.curSiteRule.action=2;
  213. }
  214. ruleParser.saveCurSiteRule();
  215. location.reload();
  216. });
  217. _GM_registerMenuCommand(i18n("configure"), ()=>{
  218. location.href="https://github.com/hoothin/UserScripts/tree/master/Pagetual";
  219. });
  220.  
  221. function getElementByXpath(xpath, contextNode, doc){
  222. doc = doc || document;
  223. contextNode = contextNode || doc;
  224. try {
  225. var result = doc.evaluate(xpath, contextNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  226. return result.singleNodeValue && result.singleNodeValue.nodeType === 1 && result.singleNodeValue;
  227. } catch (err) {
  228. throw new Error(`Invalid xpath: ${xpath}`);
  229. }
  230. }
  231.  
  232. function getAllElementsByXpath(xpath, contextNode, doc){
  233. doc = doc || document;
  234. contextNode = contextNode || doc;
  235. var result = [];
  236. try {
  237. var query = doc.evaluate(xpath, contextNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  238. for (var i = 0; i < query.snapshotLength; i++) {
  239. var node = query.snapshotItem(i);
  240. if (node.nodeType === 1) result.push(node);
  241. }
  242. } catch (err) {
  243. throw new Error(`Invalid xpath: ${xpath}`);
  244. }
  245. return result;
  246. }
  247.  
  248. class RuleParser {
  249. /*
  250. name
  251. url
  252. enable
  253. type
  254. action 0 div 1 iframe 2強行塞入
  255. nextLink 下一頁的xpath或者selector
  256. pageElement //頁面主體的xpath或者selector
  257. lazyImgSrc //圖片延後加載的屬性直接賦值到src
  258. css //添加樣式
  259. insert: '//div[@id="res"]',
  260. insertPos: 1, 1 之前 2 裏面最後
  261. pageAction:(document, eles) //對每一個插入的頁面進行修剪
  262. init:(document) // 對主頁面進行處理
  263. */
  264. constructor() {
  265. this.customRules=[
  266. {
  267. from:2,
  268. name:"yande",
  269. type:1,
  270. action:0,
  271. url:"^https:\/\/yande\.re\/",
  272. pageElement:"ul#post-list-posts>li",
  273. nextLink:"a.next_page",
  274. css:".javascript-hide {display: inline-block !important;}"
  275. },
  276. {
  277. from:2,
  278. name:"tieba",
  279. type:1,
  280. action:1,
  281. url:"^https:\/\/tieba\.baidu.com\/f\?kw=",
  282. pageElement:"ul#thread_list>li",
  283. nextLink:".next.pagination-item "
  284. }
  285. ];
  286. this.rules=[];
  287. this.pageDoc=document;
  288. this.curUrl=location.href;
  289. this.curSiteRule={};
  290. }
  291.  
  292. initSavedRules(callback){
  293. var self=this;
  294. storage.getItem("customRules", customRules=>{
  295. if(customRules)self.customRules=customRules;
  296. storage.getItem("rules", rules=>{
  297. if(rules)this.rules=rules;
  298. callback();
  299. });
  300. });
  301. }
  302.  
  303. saveCurSiteRule(){
  304. if(!this.curSiteRule || !this.curSiteRule.url)return;
  305. this.customRules=this.customRules.filter(item=>{return item.url!=this.curSiteRule.url});
  306. this.customRules.push(this.curSiteRule);
  307. storage.setItem("customRules", this.customRules);
  308. }
  309.  
  310. requestJSON(url, callback){
  311. _GM_xmlhttpRequest({
  312. url: url,
  313. onload: function(res) {
  314. let json=null;
  315. try{
  316. json=JSON.parse(res.response);
  317. }catch(e){
  318. debug(e);
  319. }
  320. callback(json);
  321. }
  322. });
  323. }
  324.  
  325. formatRule(item, type, from){
  326. switch(type){
  327. case 0:
  328. return {
  329. from:from,
  330. type:type,
  331. name:item.name,
  332. action:item.data.forceIframe=="true"?1:0,
  333. url:item.data.url,
  334. pageElement:item.data.pageElement,
  335. nextLink:item.data.nextLink,
  336. insert:item.data.insertBefore,
  337. insertPos:1,
  338. updatedAt:item.data.updated_at,
  339. css:item.data.Stylus+item.data.CSS,
  340. pageAction:item.data.bookmarklet
  341. };
  342. break;
  343. case 1:
  344. default:
  345. item.from=from;
  346. item.type=type;
  347. return item;
  348. break;
  349. }
  350. return null;
  351. }
  352.  
  353. addRuleByUrl(url, type, from, callback) {
  354. this.requestJSON(url, json=>{
  355. this.addRules(json, type, from);
  356. storage.setItem("rules", this.rules);
  357. callback();
  358. });
  359. }
  360.  
  361. addRules(rules, type, from) {
  362. if(rules && rules.length>0){
  363. this.rules=this.rules.filter(item=>{return item.from!=from});
  364. rules.forEach(item=>{
  365. let rule=this.formatRule(item, type, from);
  366. if(rule){
  367. this.rules.push(rule);
  368. }
  369. });
  370. }
  371. }
  372.  
  373. getRule() {
  374. if(this.curSiteRule && this.curSiteRule.url){
  375. return this.curSiteRule;
  376. }
  377. var self=this;
  378. for(let i in this.customRules){
  379. let rule=this.customRules[i];
  380. if(rule.enable==0)continue;
  381. let urlReg=new RegExp(rule.url, "i");
  382. if(urlReg.test(location.href)){
  383. let pageElement,nextLink,insert;
  384. if(rule.pageElement)pageElement=document.querySelector(rule.pageElement);
  385. if(rule.nextLink)nextLink=document.querySelector(rule.nextLink);
  386. if(rule.insert)insert=document.querySelector(rule.insert);
  387. if((rule.pageElement && !pageElement) ||
  388. (rule.nextLink && !nextLink) ||
  389. (rule.insert && !insert)){
  390. continue;
  391. }
  392. this.curSiteRule=rule;
  393. debug(rule);
  394. return rule;
  395. }
  396. }
  397. for(let i in this.rules){
  398. let rule=this.rules[i];
  399. if(rule.enable==0)continue;
  400. let urlReg=new RegExp(rule.url, "i");
  401. if(urlReg.test(location.href)){
  402. let pageElement,nextLink,insert;
  403. if(rule.pageElement)pageElement=rule.type==0?getElementByXpath(rule.pageElement):document.querySelector(rule.pageElement);
  404. if(rule.nextLink)nextLink=rule.type==0?getElementByXpath(rule.nextLink):document.querySelector(rule.nextLink);
  405. if(rule.insert)insert=rule.type==0?getElementByXpath(rule.insert):document.querySelector(rule.insert);
  406. if((rule.pageElement && !pageElement) ||
  407. (rule.nextLink && !nextLink) ||
  408. (rule.insert && !insert)){
  409. continue;
  410. }
  411. this.curSiteRule=rule;
  412. debug(rule);
  413. return rule;
  414. }
  415. }
  416. this.curSiteRule={};
  417. this.curSiteRule.url=location.href.replace(/\./g,"\\.");
  418. return null;
  419. }
  420.  
  421. geneSelector(ele){
  422. let selector=ele.tagName;
  423. //Google id class都是隨機。百度更過分,style script順序都是隨機的
  424. //if(ele.id) selector += '#' + ele.id;
  425. //if(ele.classList) selector += [].map.call(ele.classList,d=>'.'+d).join('');
  426. let parent = ele.parentElement;
  427. if(parent){
  428. let i,j=0;
  429. for(i=0;i<parent.children.length;i++){
  430. if(parent.children[i].tagName==selector){
  431. j++;
  432. if(parent.children[i]==ele){
  433. break;
  434. }
  435. }
  436. }
  437. selector = this.geneSelector(parent) + ' > ' + selector + (parent.tagName=="HTML"?"":`:nth-of-type(${j})`);
  438. }
  439. return selector;
  440. }
  441.  
  442. getPageElement(doc) {
  443. let pageElement=null;
  444. let self=this;
  445. if(this.curSiteRule.pageElement){
  446. pageElement=this.curSiteRule.type==0?getAllElementsByXpath(this.curSiteRule.pageElement,doc):doc.querySelectorAll(this.curSiteRule.pageElement);
  447. }
  448. if(!pageElement && !this.curSiteRule.pageElement){
  449. let body=doc.body,bodyHeight=parseInt(_unsafeWindow.getComputedStyle(body).height);
  450. function checkElement(ele){
  451. let curHeight=parseInt(_unsafeWindow.getComputedStyle(ele).height);
  452. if(curHeight/bodyHeight<=0.6)return null;
  453. if(ele.children.length==0){
  454. self.curSiteRule.pageElement=self.geneSelector(ele.parentNode)+">"+ele.tagName;
  455. self.curSiteRule.type=1;
  456. debug(self.curSiteRule.pageElement);
  457. return [ele];
  458. }
  459. let i,maxHeight=curHeight*0.35,curMaxEle=null,curMaxArea=0;
  460. for(i=0;i<ele.children.length;i++){
  461. let curNode=ele.children[i];
  462. let h=parseInt(_unsafeWindow.getComputedStyle(curNode).height);
  463. let w=parseInt(_unsafeWindow.getComputedStyle(curNode).width);
  464. if(isNaN(h) || isNaN(w))continue;
  465. let a=h*w+h;
  466. if(curMaxEle==null || curMaxArea<a){
  467. curHeight=h;
  468. curMaxArea=a;
  469. curMaxEle=curNode;
  470. }
  471. }
  472. if(curHeight>maxHeight){
  473. return checkElement(curMaxEle);
  474. }
  475. self.curSiteRule.pageElement=self.geneSelector(ele)+">*";
  476. self.curSiteRule.type=1;
  477. debug(self.curSiteRule.pageElement);
  478. return ele.children;
  479. }
  480. pageElement=checkElement(body);
  481. if(pageElement)this.saveCurSiteRule();
  482. }
  483. return pageElement;
  484. }
  485.  
  486. getPage(){
  487. let url=this.curUrl;
  488. let doc=this.pageDoc;
  489. let pageNum=0,preStr="",afterStr="";
  490. let pageMatch1=url.match(/(.*[a-zA-Z0-9][\-_])(\d+)(\.html?$|$)/i);
  491. let pageMatch2=url.match(/(.*[\?&]p(?:age)?=)(\d+)($|#.*)/i);
  492. if(pageMatch1){
  493. preStr=pageMatch1[1];
  494. pageNum=pageMatch1[2];
  495. afterStr=pageMatch1[3];
  496. }else if(pageMatch2){
  497. preStr=pageMatch2[1];
  498. pageNum=pageMatch2[2];
  499. afterStr=pageMatch2[3];
  500. }
  501. var curPage=doc,i,cur;
  502. let next=curPage.querySelector("a.next");
  503. if(!next)next=curPage.querySelector("a#next");
  504. if(!next)next=curPage.querySelector('[rel="next"]');
  505. if(!next)next=curPage.querySelector("a#rightFix");
  506. if(next && (!next.href || /javascript:/.test(next.href)))next=null;
  507. if(!next){
  508. let aTags=curPage.querySelectorAll("a");
  509. let nextf,nexts,nextt,nextfo;
  510. for(i=0;i<aTags.length;i++){
  511. let aTag=aTags[i];
  512. if(nextf && nexts && nextt)break;
  513. if(!nextf){
  514. if(/下[一1]?[页頁张張]|next( page)?|次のページ/i.test(aTag.innerHTML)){
  515. if(!aTag.href || /javascript:/.test(aTag.href)){
  516. nextfo=aTag;
  517. }else{
  518. nextf=aTag;
  519. }
  520. }
  521. }
  522. if(!nexts){
  523. if(aTag.innerHTML=="&gt;"){
  524. if(!aTag.href || /javascript:/.test(aTag.href)){
  525. nextfo=aTag;
  526. }else{
  527. nexts=aTag;
  528. }
  529. }
  530. }
  531. if(!aTag.href || /javascript:/.test(aTag.href))continue;
  532. if(!nextt){
  533. if(aTag.innerHTML=="»"){
  534. nextt=aTag;
  535. }else if(aTag.href.replace(preStr,"").replace(afterStr,"")==parseInt(pageNum)+1){
  536. nextt=aTag;
  537. }else if(aTag.href.indexOf(url)!=-1 && /[\?&]p(age)?=/i.test(aTag.href.replace(url,""))){
  538. nextt=aTag;
  539. }
  540. }
  541. }
  542. next=nextf||nexts||nextt||nextfo;
  543. }
  544. if(!next)next=curPage.querySelector(".next>a");
  545. if(!next){
  546. let pageDiv=curPage.querySelector("div.wp-pagenavi");
  547. if(pageDiv){
  548. cur=pageDiv.querySelector("span.current");
  549. next=cur.nextSibling;
  550. }else{
  551. cur=curPage.querySelector("div.article-paging>span");
  552. if(cur){
  553. next=cur.nextElementSibling;
  554. }
  555. }
  556. }
  557. return next;
  558. }
  559.  
  560. getNextLink(doc) {
  561. let nextLink=null;
  562. let curDoc=doc||this.pageDoc;
  563. if(this.curSiteRule.nextLink)nextLink=this.curSiteRule.type==0?getElementByXpath(this.curSiteRule.nextLink,curDoc):curDoc.querySelector(this.curSiteRule.nextLink);
  564. if(!nextLink){
  565. nextLink=this.getPage();
  566. }
  567. if(nextLink && !this.curSiteRule.nextLink){
  568. this.curSiteRule.nextLink=this.geneSelector(nextLink);
  569. this.curSiteRule.type=1;
  570. this.saveCurSiteRule();
  571. }
  572. return nextLink;
  573. }
  574.  
  575. getInsert(refresh) {
  576. if(this.insert && !refresh && this.insert.parentNode)return this.insert;
  577. if(this.curSiteRule.insert){
  578. this.insert=this.curSiteRule.type==0?getElementByXpath(this.curSiteRule.insert,document):document.querySelector(this.curSiteRule.insert);
  579. }else{
  580. let pageElement=this.basePageElement;
  581. if(pageElement && pageElement.length>0){
  582. var pELast = pageElement[pageElement.length - 1];
  583. this.insert = pELast.nextSibling ? pELast.nextSibling : pELast.parentNode.appendChild(document.createTextNode(' '));
  584. }
  585. }
  586. return this.insert;
  587. }
  588.  
  589. pageAction(document,eles){
  590. let code=this.curSiteRule.pageAction;
  591. if(code){
  592. _unsafeWindow.eval(code);
  593. }
  594. let css=this.curSiteRule.css;
  595. if(css){
  596. _GM_addStyle(css);
  597. }
  598. let lazyImgSrc=this.curSiteRule.lazyImgSrc;
  599. if(lazyImgSrc){
  600. [].forEach.call(eles, ele=>{
  601. [].forEach.call(ele.querySelectorAll("img"), img=>{
  602. if(img[lazyImgSrc]){
  603. img.src=img[lazyImgSrc];
  604. }
  605. });
  606. });
  607. }
  608. }
  609.  
  610. initPage(){
  611. this.getRule();
  612. let code=this.curSiteRule.init;
  613. if(code){
  614. _unsafeWindow.eval(code);
  615. }
  616. this.basePageElement=this.getPageElement(document);
  617. }
  618.  
  619. insertPage(doc, eles, url){
  620. this.pageDoc=doc;
  621. this.curUrl=url;
  622. this.pageAction(doc, eles);
  623. this.getInsert();
  624. var self=this;
  625. if(!eles || eles.length==0 || !self.insert || !self.insert.parentNode){
  626. }else{
  627. [].forEach.call(eles, ele=>{
  628. if(self.curSiteRule.insertPos==2){
  629. self.insert.appendChild(ele);
  630. }else{
  631. self.insert.parentNode.insertBefore(ele, self.insert);
  632. }
  633. });
  634. }
  635. }
  636. }
  637. var ruleParser = new RuleParser();
  638.  
  639. var rulesDate={},ruleUrls,updateDate;
  640. function initConfig(){
  641. _GM_registerMenuCommand(i18n(isDisabled?"enable":"disable"), ()=>{
  642. storage.setItem("disable_"+location.host, !isDisabled);
  643. location.reload();
  644. });
  645. var configCon;
  646. if(location.href=="https://github.com/hoothin/UserScripts/tree/master/Pagetual"){
  647. _GM_addStyle(`
  648. p>span:nth-child(1),p>span:nth-child(2),p>span:nth-child(3){
  649. cursor: pointer;
  650. user-select: none;
  651. }
  652. p>span:nth-child(1):hover,p>span:nth-child(2):hover,p>span:nth-child(3):hover{
  653. color:red;
  654. }
  655. .updateDate{
  656. cursor: pointer;
  657. user-select: none;
  658. }
  659. .updateDate:hover{
  660. color:red;
  661. }
  662. `);
  663. configCon=document.querySelector(".markdown-body");
  664. }else if(location.href=="https://github.com/hoothin/UserScripts/tree/master/Pagetual"){
  665. }else return;
  666. class Rulebar {
  667. init(ruleUrl){
  668. this.ruleUrl=ruleUrl;
  669. this.item=document.createElement("p");
  670. this.item.title=ruleUrl.type==0?"AutoPagerize Rules":"Pagetual Rules";
  671. this.item.dataset.id=this.ruleUrl.id;
  672. let url=document.createElement("span");
  673. url.innerHTML=ruleUrl.url;
  674. let up=document.createElement("span");
  675. up.innerHTML="↑ ";
  676. let down=document.createElement("span");
  677. down.innerHTML="↓ ";
  678. let del=document.createElement("span");
  679. del.innerHTML="× ";
  680. up.onclick=e=>{
  681. this.moveUp();
  682. };
  683. down.onclick=e=>{
  684. this.moveDown();
  685. };
  686. del.onclick=e=>{
  687. this.del();
  688. };
  689. this.item.appendChild(up);
  690. this.item.appendChild(down);
  691. this.item.appendChild(del);
  692. this.item.appendChild(url);
  693. configCon.appendChild(this.item);
  694. }
  695. saveSort(){
  696. let sort=[];
  697. [].forEach.call(this.item.parentNode.querySelectorAll("p[data-id]"), i=>{
  698. sort.push(i.dataset.id);
  699. });
  700. rulesDate.sort=sort;
  701. storage.setItem("importRuleUrl", rulesDate);
  702. }
  703. moveUp(){
  704. let preE=this.item.previousElementSibling;
  705. if(preE.tagName=="P" && preE.children.length>1){
  706. this.item.parentNode.insertBefore(this.item,preE);
  707. this.saveSort();
  708. }
  709. }
  710. moveDown(){
  711. let nextE=this.item.nextElementSibling;
  712. if(nextE.tagName=="P" && nextE.children.length>1){
  713. this.item.parentNode.insertBefore(nextE,this.item);
  714. this.saveSort();
  715. }
  716. }
  717. del(){
  718. if(this.ruleUrl.id<2){
  719. alert(i18n("cantDel"));
  720. }else if(window.confirm(i18n("confirmDel"))){
  721. for(let u=0;u<rulesDate.urls.length;u++){
  722. if(this.ruleUrl.id==rulesDate.urls[u].id){
  723. rulesDate.urls.splice(u,1);
  724. break;
  725. }
  726. }
  727. for(let u=0;u<rulesDate.sort.length;u++){
  728. if(this.ruleUrl.id==rulesDate.sort[u]){
  729. rulesDate.sort.splice(u,1);
  730. break;
  731. }
  732. }
  733. storage.setItem("importRuleUrl", rulesDate);
  734. ruleParser.rules=ruleParser.rules.filter(item=>{return item.from!=this.ruleUrl.id});
  735. storage.setItem("rules", ruleParser.rules);
  736. location.reload();
  737. }
  738. }
  739. }
  740. let updateP=document.createElement("p"),i=0;
  741. let now=new Date().getTime(),inUpdate=false;
  742. updateP.className="updateDate";
  743. updateP.innerHTML=updateDate;
  744. updateP.title=i18n("update");
  745. updateP.onclick=e=>{
  746. if(inUpdate)return;
  747. inUpdate=true;
  748. ruleUrls.forEach(rule=>{
  749. ruleParser.addRuleByUrl(rule.url, rule.type, rule.id, ()=>{
  750. if(++i==ruleUrls.length){
  751. storage.setItem("ruleLastUpdate", now);
  752. alert(i18n("updateSucc"));
  753. inUpdate=false;
  754. }
  755. })
  756. });
  757. alert(i18n("beginUpdate"));
  758. };
  759. configCon.appendChild(updateP);
  760. if(ruleUrls){
  761. ruleUrls.forEach(ruleUrl=>{
  762. var rulebar=new Rulebar();
  763. rulebar.init(ruleUrl);
  764. });
  765. }
  766. let customUrlsTitle=document.createElement("h2");
  767. customUrlsTitle.innerHTML=i18n("customUrls")
  768. configCon.appendChild(customUrlsTitle);
  769. let customUrlsInput=document.createElement("textarea");
  770. customUrlsInput.style.width="100%";
  771. customUrlsInput.placeholder="0|http://wedata.net/databases/AutoPagerize/items_all.json";
  772. configCon.appendChild(customUrlsInput);
  773. let customRulesTitle=document.createElement("h2");
  774. customRulesTitle.innerHTML=i18n("customRules")
  775. configCon.appendChild(customRulesTitle);
  776. let customRulesInput=document.createElement("textarea");
  777. customRulesInput.style.width="100%";
  778. customRulesInput.style.height="500px";
  779. customRulesInput.placeholder=`[\n{\n "type":"1",\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 "type":"1",\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]`;
  780. customRulesInput.value=JSON.stringify(ruleParser.customRules);
  781. configCon.appendChild(customRulesInput);
  782. let saveBtn=document.createElement("button");
  783. saveBtn.innerHTML=i18n("save");
  784. saveBtn.style.width="100%";
  785. configCon.appendChild(saveBtn);
  786. saveBtn.onclick=e=>{
  787. try{
  788. let customRules=JSON.parse(customRulesInput.value);
  789. debug(customRules);
  790. storage.setItem("customRules", customRules);
  791. }catch(e){
  792. debug(e);
  793. alert("JSON error!");
  794. }
  795. let customUrls=customUrlsInput.value.trim();
  796. if(customUrls){
  797. customUrls=customUrls.split(/\n/);
  798. for(let c=0;c<customUrls.length;c++){
  799. let urlArr=customUrls[c].split("|"),url,type=1;
  800. if(urlArr.length==1){
  801. url=urlArr[0].trim();
  802. if(!/http/.test(url)){
  803. alert("Wrong url");
  804. break;
  805. }
  806. }else if(urlArr.length==2){
  807. type=urlArr[0].trim();
  808. url=urlArr[1].trim();
  809. }else{
  810. break;
  811. }
  812. let maxId=0,hasUrl=false;;
  813. if(!rulesDate.urls){
  814. rulesDate.urls=[];
  815. maxId=1;
  816. }else{
  817. rulesDate.urls.forEach(u=>{
  818. if(maxId<u.id){
  819. maxId=u.id;
  820. }
  821. if(u.url==url){
  822. hasUrl=true;
  823. }
  824. });
  825. if(hasUrl)break;
  826. }
  827. rulesDate.urls.push({id:maxId+1,url:url,type:type});
  828. rulesDate.sort.push(maxId+1);
  829. storage.setItem("importRuleUrl", rulesDate);
  830. }
  831. }
  832. alert("over");
  833. };
  834. }
  835.  
  836. function getTimeStr(date){
  837. let now=new Date().getTime();
  838. let passTime=(now-date)/1000;
  839. if(passTime<60){
  840. updateDate=i18n("passSec", passTime);
  841. }else if(passTime<60*60){
  842. updateDate=i18n("passMin", parseInt(passTime/60));
  843. }else if(passTime<60*60*24){
  844. updateDate=i18n("passHour", parseInt(passTime/3600));
  845. }else{
  846. updateDate=i18n("passDay", parseInt(passTime/86400));
  847. }
  848. }
  849.  
  850. function initRules(callback) {
  851. /*0 wedata格式,1 pagetual格式*/
  852. ruleUrls=[
  853. {
  854. id:0,
  855. url:'http://wedata.net/databases/AutoPagerize/items_all.json',
  856. type:0,
  857. },
  858. {
  859. id:1,
  860. url:'https://raw.githubusercontent.com/hoothin/UserScripts/master/Pagetual/pagetualRules.json',
  861. type:1
  862. }
  863. ];var i=0,j=0;
  864.  
  865. ruleParser.initSavedRules(()=>{
  866. storage.getItem("importRuleUrl", data=>{
  867. if(data){
  868. rulesDate=data;
  869. if(data.urls)ruleUrls=ruleUrls.concat(data.urls);
  870. if(data.sort){
  871. let urls=[];
  872. data.sort.forEach(id=>{
  873. for(let s=0;s<ruleUrls.length;s++){
  874. if(id==ruleUrls[s].id){
  875. urls.push(ruleUrls[s]);
  876. break;
  877. }
  878. }
  879. });
  880. ruleUrls=urls;
  881. }
  882. }
  883. storage.getItem("disable_"+location.host, v=>{
  884. storage.getItem("ruleLastUpdate", date=>{
  885. isDisabled=v==true;
  886. getTimeStr(date);
  887. initConfig();
  888. if(isDisabled)return;
  889. let now=new Date().getTime();
  890. if(!date || now-date>3*24*60*60*1000){
  891. ruleUrls.forEach(rule=>{
  892. ruleParser.addRuleByUrl(rule.url, rule.type, rule.id, ()=>{
  893. if(++i==ruleUrls.length){
  894. storage.setItem("ruleLastUpdate", now);
  895. callback();
  896. }
  897. })
  898. });
  899. }else{
  900. callback();
  901. }
  902. });
  903. });
  904. });
  905. });
  906. }
  907.  
  908. function requestDoc(url, callback){
  909. _GM_xmlhttpRequest({
  910. url: url,
  911. onload: function(res) {
  912. var doc=null;
  913. try {
  914. doc=document.implementation.createHTMLDocument('');
  915. doc.documentElement.innerHTML=res.response;
  916. }
  917. catch (e) {
  918. debug('parse error'+e.toString());
  919. }
  920. let pageElement=ruleParser.getPageElement(doc);
  921. //只有1的話怕不是圖片哦
  922. if(pageElement && pageElement.length>1){
  923. callback(pageElement);
  924. ruleParser.insertPage(doc, pageElement, url);
  925. }else{
  926. requestFromIframe(url, (doc, eles)=>{
  927. callback(eles);
  928. if(eles){
  929. ruleParser.insertPage(doc, eles, url);
  930. }
  931. });
  932. }
  933. }
  934. });
  935. }
  936.  
  937. function requestFromIframe(url, callback){
  938. let orgPage,curPage;
  939. let iframe = document.createElement('iframe');
  940. iframe.name = 'pagetual-iframe';
  941. iframe.width = '100%';
  942. iframe.height = '0';
  943. iframe.frameBorder = '0';
  944. iframe.style.cssText = 'margin:0!important;padding:0!important;visibility:hidden!important;';
  945. iframe.addEventListener("load", e=>{
  946. setTimeout(()=>{
  947. //可能會延遲加載
  948. let doc=iframe.contentWindow.document;
  949. let eles=ruleParser.getPageElement(doc);
  950. if(eles && eles.length>0){
  951. callback(doc, eles);
  952. }else{
  953. isPause=true;
  954. callback(false, false);
  955. }
  956. document.body.removeChild(iframe);
  957. },300);
  958. });
  959. iframe.src=url;
  960. document.body.appendChild(iframe);
  961. }
  962.  
  963. function initPage(){
  964. ruleParser.initPage()
  965. nextPage();
  966. initListener();
  967. }
  968.  
  969. function initView(){
  970. _GM_addStyle(`
  971. .pagetual_pageBar.stop {
  972. -webkit-filter: invert(100%);
  973. filter: invert(100%);
  974. opacity: 1;
  975. }
  976. .pagetual_pageBar {
  977. opacity: 0.1;
  978. }
  979. .pagetual_pageBar:hover {
  980. opacity: 1;
  981. }
  982. .pagetual_pageBar>span {
  983. vertical-align: super;
  984. }
  985.  
  986. .pagetual_pageBar>span>svg:hover {
  987. animation: touhouAni 1s infinite;
  988. }
  989.  
  990. @keyframes touhouAni{
  991. from {transform: rotate(0deg) scale3d(1.2, 1.2, 1.2);}
  992. to {transform: rotate(360deg);}
  993. }
  994. @-webkit-keyframes touhouAni{
  995. from {transform: rotate(0deg) scale3d(1.2, 1.2, 1.2);}
  996. to {transform: rotate(360deg);}
  997. }
  998. `);
  999. }
  1000. var loading=document.createElement("div");
  1001. 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;";
  1002. loading.innerHTML=`<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>`;
  1003. document.body.appendChild(loading);
  1004.  
  1005. 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>`;
  1006. 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>`;
  1007. var pageBarStyle=`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;`;
  1008. 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);`;
  1009.  
  1010. var isPause=false,isLoading=false,curPage=1,isDisabled=false;
  1011.  
  1012. function changeStop(stop){
  1013. isPause=stop;
  1014. [].forEach.call(document.querySelectorAll(".pagetual_pageBar"), bar=>{
  1015. if(isPause){
  1016. bar.classList.add("stop");
  1017. }else{
  1018. bar.classList.remove("stop");
  1019. }
  1020. });
  1021. }
  1022.  
  1023. function initListener(){
  1024. document.addEventListener('scroll', e=>{
  1025. setTimeout(()=>{
  1026. if(!isPause && !isLoading){
  1027. let scrolly=window.scrollY;
  1028. let windowHeight=window.innerHeight;
  1029. let scrollH=Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
  1030. if(scrollH-scrolly-windowHeight<500){
  1031. nextPage();
  1032. }
  1033. }
  1034. },100);
  1035. }, false);
  1036. document.addEventListener('dblclick', e=>{
  1037. changeStop(!isPause);
  1038. });
  1039. }
  1040.  
  1041. function createPageBar(url){
  1042. isLoading=false;
  1043. loading.style.display="none";
  1044. let insert=ruleParser.getInsert();
  1045. if(!insert || !insert.parentNode)return;
  1046. curPage++;
  1047. let inTable=insert.tagName=="TR" || insert.previousElementSibling.tagName=="TR";
  1048. let pageBar=document.createElement(inTable?"tr":"div");
  1049. let upSpan=document.createElement("span");
  1050. let downSpan=document.createElement("span");
  1051. let pageText=document.createElement("a");
  1052. pageBar.className="pagetual_pageBar";
  1053. pageBar.id="pagetual_pageBar";
  1054. if(isPause){
  1055. pageBar.classList.add("stop");
  1056. }
  1057. pageBar.style.cssText=pageBarStyle;
  1058. pageBar.title="Disable";
  1059. upSpan.innerHTML=upSvg;
  1060. upSpan.title="To Top";
  1061. downSpan.innerHTML=downSvg;
  1062. downSpan.title="To Bottom";
  1063. pageText.href=url;
  1064. pageText.style=pageTextStyle;
  1065. pageText.innerHTML="Page "+curPage;
  1066. pageText.title="Current Page";
  1067. pageBar.appendChild(upSpan);
  1068. pageBar.appendChild(pageText);
  1069. pageBar.appendChild(downSpan);
  1070. if(inTable){
  1071. pageBar.style.display="table-row";
  1072. let td=document.createElement("td");
  1073. td.colSpan=99;
  1074. td.appendChild(upSpan);
  1075. td.appendChild(pageText);
  1076. td.appendChild(downSpan);
  1077. pageBar.appendChild(td);
  1078. }
  1079.  
  1080. upSpan.addEventListener("click", e=>{
  1081. changeStop(true);
  1082. document.body.scrollTop=0;
  1083. document.documentElement.scrollTop=0;
  1084. e.preventDefault();
  1085. e.stopPropagation();
  1086. });
  1087. downSpan.addEventListener("click", e=>{
  1088. changeStop(true);
  1089. document.body.scrollTop=9999999;
  1090. document.documentElement.scrollTop=9999999;
  1091. e.preventDefault();
  1092. e.stopPropagation();
  1093. });
  1094. pageBar.addEventListener("click", e=>{
  1095. changeStop(!isPause);
  1096. });
  1097. pageText.addEventListener("click", e=>{
  1098. e.stopPropagation();
  1099. });
  1100. pageBar.style.width=parseInt(_unsafeWindow.getComputedStyle(insert.parentNode).width)*.9+"px";
  1101. insert.parentNode.insertBefore(pageBar, insert);
  1102. return pageBar;
  1103. }
  1104.  
  1105. var emuIframe;
  1106. function emuPage(callback){
  1107. let orgPage,curPage,iframeDoc;
  1108. function checkPage(doc){
  1109. let eles=ruleParser.getPageElement(doc);
  1110. if(orgPage == eles[0]){
  1111. setTimeout(()=>{
  1112. checkPage(doc);
  1113. },500);
  1114. }else{
  1115. if(eles && eles.length>0){
  1116. callback(doc, eles);
  1117. }else{
  1118. isPause=true;
  1119. callback(false, false);
  1120. }
  1121. }
  1122. }
  1123. if(!emuIframe){
  1124. emuIframe = document.createElement('iframe');
  1125. emuIframe.name = 'pagetual-iframe';
  1126. emuIframe.width = '100%';
  1127. emuIframe.height = '0';
  1128. emuIframe.frameBorder = '0';
  1129. emuIframe.style.cssText = 'margin:0!important;padding:0!important;visibility:hidden!important;';
  1130. emuIframe.addEventListener("load", e=>{
  1131. iframeDoc=emuIframe.contentDocument || emuIframe.contentWindow.document;
  1132. setTimeout(()=>{
  1133. orgPage=ruleParser.getPageElement(iframeDoc)[0];
  1134. ruleParser.getNextLink(iframeDoc).click();
  1135. checkPage(iframeDoc);
  1136. },300);
  1137. });
  1138. emuIframe.src=location.href;
  1139. document.body.appendChild(emuIframe);
  1140. }else{
  1141. iframeDoc=emuIframe.contentDocument || emuIframe.contentWindow.document;
  1142. orgPage=ruleParser.getPageElement(iframeDoc)[0];
  1143. ruleParser.getNextLink(iframeDoc).click();
  1144. checkPage(iframeDoc);
  1145. }
  1146. }
  1147.  
  1148. function forceIframe(url, callback){
  1149. let curIframe = document.createElement('iframe');
  1150. curIframe.name = 'pagetual-iframe';
  1151. curIframe.frameBorder = '0';
  1152. curIframe.scrolling="no";
  1153. 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;';
  1154. curIframe.addEventListener("load", e=>{
  1155. let iframeDoc=curIframe.contentDocument || curIframe.contentWindow.document;
  1156. let eles=ruleParser.getPageElement(iframeDoc);
  1157. if(eles && eles.length>0){
  1158. ruleParser.insertPage(iframeDoc, [], url);
  1159. callback(curIframe, eles);
  1160. curIframe.style.height=iframeDoc.body.scrollHeight+"px";
  1161. curIframe.style.width=iframeDoc.body.scrollWidth+"px";
  1162. curIframe.scrollIntoView();
  1163. }else{
  1164. isPause=true;
  1165. callback(false, false);
  1166. curIframe.parentNode.removeChild(curIframe);
  1167. }
  1168. });
  1169. curIframe.src=url;
  1170. let insert=ruleParser.getInsert();
  1171. document.body.appendChild(curIframe);
  1172. return curIframe;
  1173. }
  1174.  
  1175. function nextPage(){
  1176. if(isPause || isLoading)return;
  1177. let nextLink=ruleParser.getNextLink();
  1178. let insert=ruleParser.getInsert();
  1179. if(nextLink && insert){
  1180. isLoading=true;
  1181. loading.style.display="";
  1182. if(ruleParser.curSiteRule.action==1 && nextLink.href){
  1183. requestFromIframe(nextLink.href, (doc, eles)=>{
  1184. if(eles){
  1185. createPageBar(nextLink.href);
  1186. ruleParser.insertPage(doc, eles, nextLink.href);
  1187. }
  1188. });
  1189. }else if(ruleParser.curSiteRule.action==2 && nextLink.href){
  1190. forceIframe(nextLink.href, (iframe, eles)=>{
  1191. if(!eles)return;
  1192. if(eles){
  1193. let pageBar=createPageBar(nextLink.href);
  1194. iframe.parentNode.insertBefore(pageBar, iframe);
  1195. }
  1196. });
  1197. }else{
  1198. if(nextLink.href){
  1199. requestDoc(nextLink.href, (eles)=>{
  1200. if(!eles)return;
  1201. createPageBar(nextLink.href);
  1202. });
  1203. }else{
  1204. emuPage((doc, eles)=>{
  1205. if(!eles)return;
  1206. createPageBar(nextLink.href);
  1207. if(eles){
  1208. ruleParser.insertPage(doc, eles, "");
  1209. }
  1210. });
  1211. }
  1212. }
  1213. }
  1214. }
  1215.  
  1216. function init(){
  1217. initView();
  1218. initRules(()=>{
  1219. initPage();
  1220. });
  1221. }
  1222. init();
  1223. })();