东方永页机

自动翻页

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

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