Greasy Fork 还支持 简体中文。

東方永頁機

自動翻頁

目前為 2022-01-19 提交的版本,檢視 最新版本

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