东方永页机

自动翻页

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

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