东方永页机

自动翻页

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

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