东方永页机

自动翻页

当前为 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.9
  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=curPage.querySelector("a[alt=next]");
  724. if(next && (!next.href || /javascript:/.test(next.href))){
  725. jsNext=next;
  726. next=null;
  727. }
  728. if(!next){
  729. let pageDiv=curPage.querySelector("div.wp-pagenavi");
  730. if(pageDiv){
  731. cur=pageDiv.querySelector("span.current");
  732. next=cur.nextSibling;
  733. }else{
  734. cur=curPage.querySelector("div.article-paging>span");
  735. if(cur){
  736. next=cur.nextElementSibling;
  737. }
  738. }
  739. }
  740. if(!next){
  741. let pageDiv=curPage.querySelector("div.pages>ul");
  742. if(pageDiv){
  743. cur=pageDiv.querySelector("li>b");
  744. if(cur)next=cur.parentNode.nextElementSibling.querySelector("a");
  745. }
  746. }
  747. if(!next){
  748. let aTags=curPage.querySelectorAll("a");
  749. let nextf,nexts,nextt,nextfo;
  750. for(i=0;i<aTags.length;i++){
  751. let aTag=aTags[i];
  752. if(nextf && nexts && nextt)break;
  753. if(!nextf){
  754. if(/(\s|^)下[一1]?[页頁张張]|^next( page)?\s*$|次のページ/i.test(aTag.innerText)){
  755. if(!aTag.href || /javascript:/.test(aTag.href)){
  756. nextfo=aTag;
  757. }else{
  758. nextf=aTag;
  759. }
  760. }
  761. }
  762. if(!nexts){
  763. if(aTag.innerText=="&gt;"){
  764. if(!aTag.href || /javascript:/.test(aTag.href)){
  765. nextfo=aTag;
  766. }else{
  767. nexts=aTag;
  768. }
  769. }
  770. }
  771. if(!aTag.href || /javascript:/.test(aTag.href))continue;
  772. if(!nextt){
  773. aTag.href=aTag.href.replace(/\?&/,"?");
  774. if(aTag.innerText=="»"){
  775. nextt=aTag;
  776. }else if(aTag.href.replace("#!","").replace(preStr,"").replace(afterStr,"")==parseInt(pageNum)+1){
  777. nextt=aTag;
  778. }else if(aTag.href.indexOf(url)!=-1 && /^[\/\?&]?[_-]?(p|page)?=?\/?2(\/|\?|&|$)/i.test(aTag.href.replace(url,"").replace(/\.html?$/i,""))){
  779. nextt=aTag;
  780. }
  781. }
  782. }
  783. next=nextf||nexts||nextt||nextfo;
  784. }
  785. if(!next)next=jsNext||curPage.querySelector('a[rel="next"]');
  786. return {next:next,canSave:canSave};
  787. }
  788.  
  789. getNextLink(doc) {
  790. let nextLink=null,page;
  791. if(this.curSiteRule.pageElementByJs){
  792. this.nextLinkHref="#";
  793. return true;
  794. }else if(this.curSiteRule.nextLinkByJs){
  795. try{
  796. let targetUrl=Function("doc",'"use strict";' + this.curSiteRule.nextLinkByJs)(doc);;
  797. nextLink={href:targetUrl};
  798. }catch(e){}
  799. }else if(this.curSiteRule.nextLinkByUrl){
  800. let targetUrl=this.curUrl.replace(new RegExp(this.curSiteRule.nextLinkByUrl[0]), this.curSiteRule.nextLinkByUrl[1]);
  801. if(targetUrl != this.curUrl){
  802. let reps=targetUrl.match(/{.*?}/g);
  803. if(reps){
  804. reps.forEach(rep=>{
  805. let code=rep.replace("{","").replace("}","");
  806. let result=code.match(/^(\d+)\+1$/);
  807. if(result){
  808. result=parseInt(result[1])+1;
  809. }else{
  810. try{
  811. result=Function('"use strict";return ' + code)();;
  812. }catch(e){}
  813. }
  814. targetUrl=targetUrl.replace(rep, result);
  815. });
  816. }
  817. }
  818. nextLink={href:targetUrl};
  819. }else if(this.curSiteRule.nextLink){
  820. nextLink=this.curSiteRule.type==0?getElementByXpath(this.curSiteRule.nextLink,doc,doc):doc.querySelector(this.curSiteRule.nextLink);
  821. }
  822. if(!nextLink){
  823. page=this.getPage(doc);
  824. nextLink=page.next;
  825. if(nextLink && doc==document){
  826. let nextLinkCs=_unsafeWindow.getComputedStyle(nextLink);
  827. if(nextLinkCs.cursor=="not-allowed"){
  828. self.nextLinkHref=false;
  829. return null;
  830. }
  831. }
  832. }
  833. if(nextLink){
  834. if(!this.curSiteRule.nextLink && page && page.canSave){
  835. this.curSiteRule.nextLink=this.geneSelector(nextLink);
  836. this.curSiteRule.type=1;
  837. this.saveCurSiteRule();
  838. }
  839. }
  840. if(nextLink){
  841. this.nextLinkHref=nextLink.href?nextLink.href:"#";
  842. debug(nextLink);
  843. }else{
  844. this.nextLinkHref=false;
  845. }
  846. return nextLink;
  847. }
  848.  
  849. getInsert(refresh) {
  850. if(this.insert && !refresh){
  851. let parent=this.insert;
  852. while(parent && parent.nodeName!="BODY"){
  853. parent=parent.parentNode;
  854. }
  855. if(parent && parent.nodeName=="BODY"){
  856. return this.insert;
  857. }
  858. }
  859. if(this.curSiteRule.insert){
  860. this.insert=this.curSiteRule.type==0?getElementByXpath(this.curSiteRule.insert,document):document.querySelector(this.curSiteRule.insert);
  861. }else{
  862. let pageElement=this.getPageElement(document, _unsafeWindow);
  863. if(pageElement && pageElement.length>0){
  864. var pELast = pageElement[pageElement.length - 1];
  865. this.insert = pELast.nextSibling ? pELast.nextSibling : pELast.parentNode.appendChild(document.createTextNode(' '));
  866. }
  867. }
  868. return this.insert;
  869. }
  870.  
  871. pageAction(doc,eles){
  872. let code=this.curSiteRule.pageAction;
  873. if(code){
  874. try{
  875. Function("doc","eles",'"use strict";' + code)(doc,eles);
  876. }catch(e){}
  877. }
  878. let css=this.curSiteRule.css;
  879. if(css){
  880. _GM_addStyle(css);
  881. }
  882. [].forEach.call(eles, ele=>{
  883. [].forEach.call(ele.querySelectorAll("img"), img=>{
  884. //if(img.src==""){
  885. let realSrc;
  886. if(img.dataset && img.dataset.original){
  887. realSrc=img.dataset.original;
  888. }else if(img.dataset && img.dataset.src){
  889. realSrc=img.dataset.src;
  890. }else if(img._lazyrias && img._lazyrias.srcset){
  891. realSrc=img._lazyrias.srcset[img._lazyrias.srcset.length-1];
  892. }else if(img.dataset && img.dataset.origFile){
  893. realSrc=img.dataset.origFile;
  894. }else if(img.srcset){
  895. var srcs=img.srcset.split(","),largeSize=0;
  896. srcs.forEach(srci=>{
  897. let srcInfo=srci.trim().split(" "),curSize=parseInt(srcInfo[1]);
  898. if(srcInfo[1] && curSize>largeSize){
  899. largeSize=curSize;
  900. realSrc=srcInfo[0];
  901. }
  902. });
  903. }
  904. if(realSrc)img.src=realSrc;
  905. //}
  906. });
  907. [].forEach.call(ele.querySelectorAll("a.lazyload"), a=>{
  908. if(a.dataset.original){
  909. a.style.backgroundImage='url("'+a.dataset.original+'")';
  910. }
  911. });
  912. });
  913. let lazyImgSrc=this.curSiteRule.lazyImgSrc;
  914. if(lazyImgSrc){
  915. [].forEach.call(eles, ele=>{
  916. [].forEach.call(ele.querySelectorAll("img"), img=>{
  917. if(img[lazyImgSrc]){
  918. img.src=img[lazyImgSrc];
  919. }
  920. });
  921. });
  922. }
  923. }
  924.  
  925. initPage(callback){
  926. let self=this;
  927. curPage=1;
  928. //if(this.curSiteRule.url && !this.curSiteRule.singleUrl)return;
  929. this.curSiteRule={};
  930. this.pageDoc=document;
  931. this.nextLinkHref=null;
  932. this.curUrl=location.href;
  933. this.getRule(()=>{
  934. if(self.curSiteRule.enable==0){
  935. isPause=true;
  936. return;
  937. }
  938. if(self.curSiteRule && !self.curSiteRule.singleUrl){
  939. self.hpRules=self.hpRules.filter(item=>{return item&&item.url!=self.curSiteRule.url});
  940. self.hpRules.unshift(self.curSiteRule);
  941. if(self.hpRules.length>30){
  942. self.hpRules.pop();
  943. }
  944. storage.setItem("hpRules", self.hpRules);
  945. }
  946. let code=self.curSiteRule.init;
  947. if(code){
  948. try{
  949. Function('"use strict";' + code)();
  950. }catch(e){}
  951. }
  952. self.getNextLink(document);
  953. callback();
  954. });
  955. }
  956.  
  957. insertPage(doc, eles, url, callback){
  958. this.pageDoc=doc;
  959. this.curUrl=url;
  960. this.pageAction(doc, eles);
  961. this.getNextLink(doc);
  962. if(!this.nextLinkHref && this.curSiteRule.singleUrl && this.curSiteRule.pageElement){
  963. return false;
  964. }
  965. if(callback)callback(eles);
  966. this.getInsert();
  967. var self=this;
  968. if(!eles || eles.length==0 || !self.insert || !self.insert.parentNode){
  969. }else{
  970. [].forEach.call(eles, ele=>{
  971. if(self.curSiteRule.insertPos==2){
  972. self.insert.appendChild(ele.cloneNode(true));
  973. }else{
  974. self.insert.parentNode.insertBefore(ele.cloneNode(true), self.insert);
  975. }
  976. });
  977. }
  978. return true;
  979. }
  980. }
  981. var ruleParser = new RuleParser();
  982.  
  983. var rulesData={},ruleUrls,updateDate;
  984. function initConfig(){
  985. _GM_registerMenuCommand(i18n(forceState==1?"enable":"disableSite"), ()=>{
  986. storage.setItem("forceState_"+location.host, (forceState==1?"":1));
  987. location.reload();
  988. });
  989.  
  990. _GM_registerMenuCommand(i18n(forceState==2?"cancelForceIframe":"forceIframe"), ()=>{
  991. storage.setItem("forceState_"+location.host, (forceState==2?"":2));
  992. location.reload();
  993. });
  994. var configCon,insertPos;
  995. if(location.href=="https://github.com/hoothin/UserScripts/tree/master/Pagetual"){
  996. _GM_addStyle(`
  997. p>span:nth-child(1),p>span:nth-child(2),p>span:nth-child(3){
  998. cursor: pointer;
  999. user-select: none;
  1000. }
  1001. p>span:nth-child(1):hover,p>span:nth-child(2):hover,p>span:nth-child(3):hover{
  1002. color:red;
  1003. }
  1004. .updateDate{
  1005. cursor: pointer;
  1006. user-select: none;
  1007. }
  1008. .updateDate:hover{
  1009. color:red;
  1010. }
  1011. `);
  1012. configCon=document.querySelector(".markdown-body");
  1013. insertPos=configCon.querySelector("hr");
  1014. }else if(location.href=="https://github.com/hoothin/UserScripts/tree/master/Pagetual"){
  1015. }else return;
  1016. class Rulebar {
  1017. init(ruleUrl){
  1018. this.ruleUrl=ruleUrl;
  1019. this.item=document.createElement("p");
  1020. this.item.title=ruleUrl.type==0?"AutoPagerize Rules":"Pagetual Rules";
  1021. this.item.dataset.id=this.ruleUrl.id;
  1022. let url=document.createElement("span");
  1023. url.innerHTML=ruleUrl.url;
  1024. let up=document.createElement("span");
  1025. up.innerHTML="↑ ";
  1026. up.title=i18n("sortTitle");
  1027. let down=document.createElement("span");
  1028. down.innerHTML="↓ ";
  1029. down.title=i18n("sortTitle");
  1030. let del=document.createElement("span");
  1031. del.innerHTML="× ";
  1032. up.onclick=e=>{
  1033. this.moveUp();
  1034. };
  1035. down.onclick=e=>{
  1036. this.moveDown();
  1037. };
  1038. del.onclick=e=>{
  1039. this.del();
  1040. };
  1041. this.item.appendChild(up);
  1042. this.item.appendChild(down);
  1043. this.item.appendChild(del);
  1044. this.item.appendChild(url);
  1045. configCon.insertBefore(this.item, insertPos);
  1046. }
  1047. saveSort(){
  1048. let sort=[];
  1049. [].forEach.call(this.item.parentNode.querySelectorAll("p[data-id]"), i=>{
  1050. sort.push(i.dataset.id);
  1051. });
  1052. rulesData.sort=sort;
  1053. storage.setItem("rulesData", rulesData);
  1054. }
  1055. moveUp(){
  1056. let preE=this.item.previousElementSibling;
  1057. if(preE.tagName=="P" && preE.children.length>1){
  1058. this.item.parentNode.insertBefore(this.item,preE);
  1059. this.saveSort();
  1060. }
  1061. }
  1062. moveDown(){
  1063. let nextE=this.item.nextElementSibling;
  1064. if(nextE.tagName=="P" && nextE.children.length>1){
  1065. this.item.parentNode.insertBefore(nextE,this.item);
  1066. this.saveSort();
  1067. }
  1068. }
  1069. del(){
  1070. if(this.ruleUrl.id<2){
  1071. alert(i18n("cantDel"));
  1072. }else if(window.confirm(i18n("confirmDel"))){
  1073. for(let u=0;u<rulesData.urls.length;u++){
  1074. if(this.ruleUrl.id==rulesData.urls[u].id){
  1075. rulesData.urls.splice(u,1);
  1076. break;
  1077. }
  1078. }
  1079. for(let u=0;u<rulesData.sort.length;u++){
  1080. if(this.ruleUrl.id==rulesData.sort[u]){
  1081. rulesData.sort.splice(u,1);
  1082. break;
  1083. }
  1084. }
  1085. storage.setItem("rulesData", rulesData);
  1086. ruleParser.rules=ruleParser.rules.filter(item=>{return item.from!=this.ruleUrl.id});
  1087. storage.setItem("rules", ruleParser.rules);
  1088. this.item.parentNode.removeChild(this.item);
  1089. //location.reload();
  1090. }
  1091. }
  1092. }
  1093. let updateP=document.createElement("p"),i=0;
  1094. let now=new Date().getTime(),inUpdate=false;
  1095.  
  1096.  
  1097. let pastDate=(new Date(updateDate)).toString(),passStr;
  1098. let passTime=(now-updateDate)/1000;
  1099. if(passTime<60){
  1100. passStr=i18n("passSec", passTime);
  1101. }else if(passTime<60*60){
  1102. passStr=i18n("passMin", parseInt(passTime/60));
  1103. }else if(passTime<60*60*24){
  1104. passStr=i18n("passHour", parseInt(passTime/3600));
  1105. }else{
  1106. passStr=i18n("passDay", parseInt(passTime/86400));
  1107. }
  1108.  
  1109.  
  1110. updateP.className="updateDate";
  1111. updateP.innerHTML=passStr;
  1112. updateP.title=i18n("update")+" - "+pastDate;
  1113. updateP.onclick=e=>{
  1114. if(inUpdate)return;
  1115. inUpdate=true;
  1116. let ruleIndex=0;
  1117. storage.setItem("hpRules", []);
  1118. function addNextRule(){
  1119. if(ruleIndex>=ruleUrls.length){
  1120. storage.setItem("ruleLastUpdate", now);
  1121. alert(i18n("updateSucc"));
  1122. inUpdate=false;
  1123. updateP.innerHTML=i18n("passSec", 0);
  1124. updateP.title=i18n("update");
  1125. }else{
  1126. let rule=ruleUrls[ruleIndex++];
  1127. ruleParser.addRuleByUrl(rule.url, rule.type, rule.id, ()=>{
  1128. addNextRule();
  1129. })
  1130. }
  1131. }
  1132. addNextRule();
  1133. alert(i18n("beginUpdate"));
  1134. };
  1135. configCon.insertBefore(updateP, insertPos);
  1136. if(ruleUrls){
  1137. ruleUrls.forEach(ruleUrl=>{
  1138. var rulebar=new Rulebar();
  1139. rulebar.init(ruleUrl);
  1140. });
  1141. }
  1142. let customUrlsTitle=document.createElement("h2");
  1143. customUrlsTitle.innerHTML=i18n("customUrls");
  1144. configCon.insertBefore(customUrlsTitle, insertPos);
  1145. let customUrlsInput=document.createElement("textarea");
  1146. customUrlsInput.style.width="100%";
  1147. customUrlsInput.placeholder="0|http://wedata.net/databases/AutoPagerize/items_all.json";
  1148. configCon.insertBefore(customUrlsInput, insertPos);
  1149.  
  1150. let opacityTitle=document.createElement("h2");
  1151. opacityTitle.innerHTML=i18n("opacity");
  1152. let opacityInput=document.createElement("input");
  1153. opacityInput.value=rulesData.opacity*100;
  1154. opacityInput.type="number";
  1155. opacityInput.style.width="50px";
  1156. opacityInput.style.margin="0 0 0 10px";
  1157. opacityTitle.appendChild(opacityInput);
  1158. configCon.insertBefore(opacityTitle, insertPos);
  1159.  
  1160.  
  1161. let hideBarTitle=document.createElement("h2");
  1162. hideBarTitle.innerHTML=i18n("hideBar");
  1163. let hideBarInput=document.createElement("input");
  1164. hideBarInput.type="checkbox";
  1165. hideBarInput.style.width="50px";
  1166. hideBarInput.style.height="20px";
  1167. hideBarInput.checked=rulesData.hideBar;
  1168. hideBarTitle.appendChild(hideBarInput);
  1169. configCon.insertBefore(hideBarTitle, insertPos);
  1170.  
  1171. let customRulesTitle=document.createElement("h2");
  1172. customRulesTitle.innerHTML=i18n("customRules")
  1173. configCon.insertBefore(customRulesTitle, insertPos);
  1174. let customRulesInput=document.createElement("textarea");
  1175. customRulesInput.style.width="100%";
  1176. customRulesInput.style.height="500px";
  1177. 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]`;
  1178. customRulesInput.value=getFormatJSON(ruleParser.customRules);
  1179. configCon.insertBefore(customRulesInput, insertPos);
  1180. let saveBtn=document.createElement("button");
  1181. saveBtn.innerHTML=i18n("save");
  1182. saveBtn.style.width="100%";
  1183. configCon.insertBefore(saveBtn, insertPos);
  1184. saveBtn.onclick=e=>{
  1185. try{
  1186. storage.setItem("hpRules", []);
  1187. if(customRulesInput.value==""){
  1188. storage.setItem("customRules", "");
  1189. }else{
  1190. let customRules=JSON.parse(customRulesInput.value);
  1191. debug(customRules);
  1192. storage.setItem("customRules", customRules);
  1193. }
  1194. }catch(e){
  1195. debug(e);
  1196. alert("JSON error, check again!");
  1197. return;
  1198. }
  1199. rulesData.opacity=opacityInput.value/100;
  1200. rulesData.hideBar=hideBarInput.checked;
  1201. storage.setItem("rulesData", rulesData);
  1202. let customUrls=customUrlsInput.value.trim();
  1203. if(customUrls){
  1204. customUrls=customUrls.split(/\n/);
  1205. for(let c=0;c<customUrls.length;c++){
  1206. let urlArr=customUrls[c].split("|"),url,type=1;
  1207. if(urlArr.length==1){
  1208. url=urlArr[0].trim();
  1209. if(!/^http/.test(url)){
  1210. alert("Wrong url, check again!");
  1211. return;
  1212. }
  1213. }else if(urlArr.length==2){
  1214. type=urlArr[0].trim();
  1215. url=urlArr[1].trim();
  1216. if(!/^http/.test(url)){
  1217. alert("Wrong url, check again!");
  1218. return;
  1219. }
  1220. }else{
  1221. break;
  1222. }
  1223. let maxId=0,hasUrl=false;;
  1224. if(!rulesData.urls){
  1225. rulesData.urls=[];
  1226. maxId=1;
  1227. }else{
  1228. rulesData.urls.forEach(u=>{
  1229. if(maxId<u.id){
  1230. maxId=u.id;
  1231. }
  1232. if(u.url==url){
  1233. hasUrl=true;
  1234. }
  1235. });
  1236. if(hasUrl)break;
  1237. }
  1238. rulesData.urls.push({id:maxId+1,url:url,type:type});
  1239. rulesData.sort.push(maxId+1);
  1240. storage.setItem("rulesData", rulesData);
  1241. }
  1242. }
  1243. alert("Modified successfully");
  1244. location.reload();
  1245. };
  1246. }
  1247.  
  1248. function objIsArr(obj) {
  1249. return obj &&
  1250. typeof obj === 'object' &&
  1251. typeof obj.length === 'number' &&
  1252. !(obj.propertyIsEnumerable('length'));
  1253. }
  1254.  
  1255. function getFormatJSON(obj){
  1256. if(!objIsArr(obj))return "";
  1257. let ret="[\n";
  1258. let len=obj.length,i=0,isLast;
  1259. obj.forEach(item=>{
  1260. ret+=" {\n";
  1261. let iLen=Object.keys(item).length,j=0;
  1262. for(let key in item){
  1263. isLast=(++j)==iLen;
  1264. let value=item[key];
  1265. if(objIsArr(value)){
  1266. let vstr="[",v=0,vIsLast=false;
  1267. value.forEach(vi=>{
  1268. vIsLast=(++v)==value.length;
  1269. vstr+="\""+vi.replace(/\\/g,"\\\\")+"\""+(vIsLast?"":",");
  1270. });
  1271. vstr+="]";
  1272. ret+=" \""+key+"\":"+vstr+""+(isLast?"":",")+"\n";
  1273. }else{
  1274. if(typeof value=="string"){
  1275. value=value.replace(/\\/g,"\\\\");
  1276. }
  1277. ret+=" \""+key+"\":\""+value+"\""+(isLast?"":",")+"\n";
  1278. }
  1279. }
  1280. isLast=(++i)==len;
  1281. ret+=" }"+(isLast?"":",")+"\n";
  1282. });
  1283. ret+="]";
  1284. return ret;
  1285. }
  1286.  
  1287. function initRules(callback) {
  1288. /*0 wedata格式,1 pagetual格式*/
  1289. ruleUrls=[
  1290. {
  1291. id:0,
  1292. url:'https://raw.githubusercontent.com/hoothin/UserScripts/master/Pagetual/pagetualRules.json',
  1293. type:1
  1294. },
  1295. {
  1296. id:1,
  1297. url:'http://wedata.net/databases/AutoPagerize/items_all.json',
  1298. type:0,
  1299. }
  1300. ];var i=0,j=0;
  1301.  
  1302. ruleParser.initSavedRules(()=>{
  1303. storage.getItem("rulesData", data=>{
  1304. if(data){
  1305. rulesData=data;
  1306. if(data.urls)ruleUrls=ruleUrls.concat(data.urls);
  1307. if(data.sort){
  1308. let urls=[];
  1309. data.sort.forEach(id=>{
  1310. for(let s=0;s<ruleUrls.length;s++){
  1311. if(id==ruleUrls[s].id){
  1312. urls.push(ruleUrls[s]);
  1313. break;
  1314. }
  1315. }
  1316. });
  1317. ruleUrls=urls;
  1318. }
  1319. }
  1320. if(typeof(rulesData.opacity)=="undefined"){
  1321. rulesData.opacity=0.3;
  1322. }
  1323. if(typeof(rulesData.hideBar)=="undefined"){
  1324. rulesData.hideBar=true;
  1325. }
  1326. storage.getItem("forceState_"+location.host, v=>{
  1327. storage.getItem("ruleLastUpdate", date=>{
  1328. forceState=v||0;
  1329. updateDate=date;
  1330. initConfig();
  1331. if(forceState==1)return;
  1332. let now=new Date().getTime();
  1333. if(!date || now-date>2*24*60*60*1000){
  1334. storage.setItem("ruleLastUpdate", now);
  1335. storage.setItem("hpRules", []);
  1336. let ruleIndex=0;
  1337. function addNextRule(){
  1338. if(ruleIndex>=ruleUrls.length){
  1339. callback();
  1340. }else{
  1341. let rule=ruleUrls[ruleIndex++];
  1342. ruleParser.addRuleByUrl(rule.url, rule.type, rule.id, ()=>{
  1343. addNextRule();
  1344. })
  1345. }
  1346. }
  1347. addNextRule();
  1348. }else{
  1349. callback();
  1350. }
  1351. });
  1352. });
  1353. });
  1354. });
  1355. }
  1356.  
  1357. function requestDoc(url, callback){
  1358. _GM_xmlhttpRequest({
  1359. url: url,
  1360. method: 'GET',
  1361. overrideMimeType:"text/html;charset="+document.charset,
  1362. onload: function(res) {
  1363. var doc=null;
  1364. try {
  1365. doc=document.implementation.createHTMLDocument('');
  1366. doc.documentElement.innerHTML=res.response;
  1367. }
  1368. catch (e) {
  1369. debug('parse error'+e.toString());
  1370. }
  1371. let pageElement=ruleParser.getPageElement(doc);
  1372. //只有1的話怕不是圖片哦
  1373. if(pageElement && (pageElement.length>1 || (pageElement.length==1 && pageElement[0].tagName!="IMG") )){
  1374. let result=ruleParser.insertPage(doc, pageElement, url, callback);
  1375. if(!result){
  1376. requestFromIframe(url, (doc, eles)=>{
  1377. if(eles){
  1378. ruleParser.insertPage(doc, eles, url, callback);
  1379. }
  1380. });
  1381. }
  1382. }else{
  1383. requestFromIframe(url, (doc, eles)=>{
  1384. if(eles){
  1385. ruleParser.insertPage(doc, eles, url, callback);
  1386. }
  1387. });
  1388. }
  1389. },
  1390. onerror: function(e){
  1391. debug(e);
  1392. callback(false);
  1393. }
  1394. });
  1395. }
  1396.  
  1397. var failFromIframe=0;
  1398. function requestFromIframe(url, callback){
  1399. let orgPage,curPage;
  1400. let iframe = document.createElement('iframe');
  1401. iframe.name = 'pagetual-iframe';
  1402. iframe.width = '100%';
  1403. iframe.height = '0';
  1404. iframe.frameBorder = '0';
  1405. iframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  1406. iframe.style.cssText = 'margin:0!important;padding:0!important;visibility:hidden!important;';
  1407. iframe.addEventListener("load", e=>{
  1408. setTimeout(()=>{
  1409. let tryTimes=0;
  1410. function checkIframe(){
  1411. try{
  1412. let doc=iframe.contentWindow.document;
  1413. let eles=ruleParser.getPageElement(doc, iframe.contentWindow);
  1414. if(eles && eles.length>0){
  1415. callback(doc, eles);
  1416. }else if(tryTimes++ < 2){
  1417. setTimeout(()=>{
  1418. checkIframe();
  1419. },500);
  1420. return;
  1421. }else{
  1422. if(failFromIframe++ > 2){
  1423. failFromIframe=0;
  1424. isPause=true;
  1425. callback(false, false);
  1426. }else{
  1427. callback(false, false);
  1428. }
  1429. }
  1430. }catch(e){
  1431. isPause=true;
  1432. callback(false, false);
  1433. }
  1434. document.body.removeChild(iframe);
  1435. }
  1436. checkIframe();
  1437. },300);
  1438. });
  1439. iframe.src=url;
  1440. document.body.appendChild(iframe);
  1441. }
  1442.  
  1443. function initPage(){
  1444. ruleParser.initPage(()=>{
  1445. initListener();
  1446. nextPage();
  1447. });
  1448. }
  1449.  
  1450. function initView(){
  1451. _GM_addStyle(`
  1452. .pagetual_pageBar.stop {
  1453. -webkit-filter: invert(100%);
  1454. filter: invert(100%);
  1455. opacity: 1!important;
  1456. }
  1457. .pagetual_pageBar.hide {
  1458. display: none!important;
  1459. }
  1460. .pagetual_pageBar:hover {
  1461. opacity: 1!important;
  1462. }
  1463. .pagetual_pageBar>span {
  1464. vertical-align: super;
  1465. }
  1466.  
  1467. .pagetual_pageBar>span>svg:hover {
  1468. animation: touhouAni 1s infinite;
  1469. }
  1470.  
  1471. @keyframes touhouAni{
  1472. from {transform: rotate(0deg) scale3d(1.2, 1.2, 1.2);}
  1473. to {transform: rotate(360deg);}
  1474. }
  1475. @-webkit-keyframes touhouAni{
  1476. from {transform: rotate(0deg) scale3d(1.2, 1.2, 1.2);}
  1477. to {transform: rotate(360deg);}
  1478. }
  1479. `);
  1480. 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;`;
  1481. }
  1482. var loading=document.createElement("div");
  1483. 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;";
  1484. 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>`;
  1485. document.body.appendChild(loading);
  1486.  
  1487. 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>`;
  1488. 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>`;
  1489. var pageBarStyle;
  1490. 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);`;
  1491.  
  1492. var isPause=false,isLoading=false,curPage=1,forceState=0,bottomGap=1000;
  1493.  
  1494. function changeStop(stop, hide){
  1495. isPause=stop;
  1496. [].forEach.call(document.querySelectorAll(".pagetual_pageBar"), bar=>{
  1497. if(isPause){
  1498. bar.classList.add("stop");
  1499. if(hide)bar.classList.add("hide");
  1500. }else{
  1501. bar.classList.remove("stop");
  1502. if(hide)bar.classList.remove("hide");
  1503. }
  1504. });
  1505. }
  1506.  
  1507. function isInViewPort(element) {
  1508. const viewWidth = window.innerWidth || document.documentElement.clientWidth;
  1509. const viewHeight = window.innerHeight || document.documentElement.clientHeight;
  1510. const {
  1511. top,
  1512. right,
  1513. bottom,
  1514. left,
  1515. } = element.getBoundingClientRect();
  1516.  
  1517. return (
  1518. top >= 0 &&
  1519. left >= 0 &&
  1520. right <= viewWidth &&
  1521. bottom <= viewHeight
  1522. );
  1523. }
  1524.  
  1525. function initListener(){
  1526. var urlChanged=false;
  1527. var _wr = function(type) {
  1528. var orig = history[type];
  1529. return function() {
  1530. var rv = orig.apply(this, arguments);
  1531. var e = new Event(type);
  1532. e.arguments = arguments;
  1533. window.dispatchEvent(e);
  1534. return rv;
  1535. };
  1536. };
  1537. history.pushState = _wr('pushState');
  1538. window.addEventListener('pushState', function(e) {
  1539. urlChanged=true;
  1540. });
  1541. let loadmoreBtn,buttons=document.querySelectorAll("input,button,a"),loadmoreReg=/^\s*(加载更多|加載更多|load\s*more|もっと読み込む)\s*$/i,loading=true;
  1542. for(let i=0;i<buttons.length;i++){
  1543. let button=buttons[i];
  1544. if(button && loadmoreReg.test(button.innerText)){
  1545. loadmoreBtn=button;
  1546. loadmoreBtn.click();
  1547. break;
  1548. }
  1549. }
  1550. if(loadmoreBtn)setTimeout(()=>{loading=false},2000);
  1551. document.addEventListener('scroll', e=>{
  1552. if(urlChanged){
  1553. ruleParser.initPage(()=>{});
  1554. initView();
  1555. urlChanged=false;
  1556. }
  1557. if(!loading && loadmoreBtn && isInViewPort(loadmoreBtn)){
  1558. loadmoreBtn.click();
  1559. loading=true;
  1560. setTimeout(()=>{loading=false},2000);
  1561. }
  1562. setTimeout(()=>{
  1563. if(!isPause && !isLoading){
  1564. let scrolly=window.scrollY;
  1565. let windowHeight=window.innerHeight;
  1566. let scrollH=Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
  1567. if(scrollH-scrolly-windowHeight<bottomGap){
  1568. nextPage();
  1569. }
  1570. }
  1571. },100);
  1572. }, true);
  1573. document.addEventListener('dblclick', e=>{
  1574. setTimeout(()=>{
  1575. changeStop(!isPause, rulesData.hideBar);
  1576. if(!isPause){
  1577. nextPage();
  1578. }
  1579. },200);
  1580. });
  1581. }
  1582.  
  1583. function createPageBar(url){
  1584. let insert=ruleParser.getInsert();
  1585. if(!insert || !insert.parentNode)return;
  1586. curPage++;
  1587. let inTable=insert.tagName=="TR" ||
  1588. (insert.previousElementSibling && insert.previousElementSibling.tagName=="TR") ||
  1589. insert.tagName=="TBODY" || (insert.previousElementSibling && insert.previousElementSibling.tagName=="TBODY");
  1590. let inLi=insert.tagName=="LI" || (insert.previousElementSibling && insert.previousElementSibling.tagName=="LI");
  1591. let pageBar=document.createElement(inTable?"tr":"div");
  1592. let upSpan=document.createElement("span");
  1593. let downSpan=document.createElement("span");
  1594. let pageText=document.createElement("a");
  1595. pageBar.className="pagetual_pageBar";
  1596. pageBar.id="pagetual_pageBar";
  1597. if(isPause){
  1598. pageBar.classList.add("stop");
  1599. }
  1600. pageBar.style.cssText=pageBarStyle;
  1601. pageBar.title=i18n(isPause?"enable":"disable");
  1602. upSpan.innerHTML=upSvg;
  1603. upSpan.title=i18n("toTop");
  1604. downSpan.innerHTML=downSvg;
  1605. downSpan.title=i18n("toBottom");
  1606. upSpan.style.right=upSpan.style.left=upSpan.style.top=upSpan.style.bottom="unset";
  1607. downSpan.style.right=downSpan.style.left=downSpan.style.top=downSpan.style.bottom="unset";
  1608. pageText.href=url;
  1609. pageText.style=pageTextStyle;
  1610. pageText.innerHTML="Page "+curPage;
  1611. pageText.title=i18n("current");
  1612. pageBar.appendChild(upSpan);
  1613. pageBar.appendChild(pageText);
  1614. pageBar.appendChild(downSpan);
  1615. if(inTable){
  1616. let example=(insert.tagName=="TR" || insert.tagName=="TBODY")?insert:insert.previousElementSibling;
  1617. let tdNum=example.tagName=="TR"?example.children.length:example.querySelector("tr").children.length;
  1618. pageBar.style.display="table-row";
  1619. let td=document.createElement("td");
  1620. td.colSpan=tdNum;
  1621. td.style.textAlign="center";
  1622. td.appendChild(upSpan);
  1623. td.appendChild(pageText);
  1624. td.appendChild(downSpan);
  1625. pageBar.appendChild(td);
  1626. }
  1627.  
  1628. upSpan.addEventListener("click", e=>{
  1629. changeStop(true);
  1630. pageBar.title=i18n(isPause?"enable":"disable");
  1631. document.body.scrollTop=0;
  1632. document.documentElement.scrollTop=0;
  1633. e.preventDefault();
  1634. e.stopPropagation();
  1635. });
  1636. downSpan.addEventListener("click", e=>{
  1637. changeStop(true);
  1638. pageBar.title=i18n(isPause?"enable":"disable");
  1639. document.body.scrollTop=9999999;
  1640. document.documentElement.scrollTop=9999999;
  1641. e.preventDefault();
  1642. e.stopPropagation();
  1643. });
  1644. pageBar.addEventListener("click", e=>{
  1645. changeStop(!isPause);
  1646. pageBar.title=i18n(isPause?"enable":"disable");
  1647. });
  1648. pageText.addEventListener("click", e=>{
  1649. e.stopPropagation();
  1650. });
  1651. pageBar.style.width=parseInt(_unsafeWindow.getComputedStyle(insert.parentNode).width)*.99+"px";
  1652. if(inLi){
  1653. pageBar.style.width="auto";
  1654. pageBar.style.minWidth="150px";
  1655. let line=document.createElement("li");
  1656. line.appendChild(pageBar);
  1657. if(ruleParser.curSiteRule.insertPos==2){
  1658. insert.appendChild(line);
  1659. }else{
  1660. insert.parentNode.insertBefore(line, insert);
  1661. }
  1662. }else{
  1663. if(ruleParser.curSiteRule.insertPos==2){
  1664. insert.appendChild(pageBar);
  1665. }else{
  1666. insert.parentNode.insertBefore(pageBar, insert);
  1667. }
  1668. }
  1669.  
  1670. let scrollH=Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
  1671. let actualTop = pageBar.offsetTop;
  1672. let current = pageBar.parentNode.offsetParent;
  1673. while (current !== null){
  1674. actualTop += current.offsetTop;
  1675. current = current.offsetParent;
  1676. }
  1677. bottomGap=scrollH-actualTop+800;
  1678. return pageBar;
  1679. }
  1680.  
  1681. var emuIframe;
  1682. function emuPage(callback){
  1683. let orgPage,curPage,iframeDoc,times=0;
  1684. function checkPage(){
  1685. if(isPause)return;
  1686. try{
  1687. iframeDoc=emuIframe.contentDocument || emuIframe.contentWindow.document;
  1688. }catch(e){
  1689. isPause=true;
  1690. callback(false, false);
  1691. return;
  1692. }
  1693. if(!orgPage){
  1694. orgPage=ruleParser.getPageElement(iframeDoc, iframeDoc.defaultView, true);
  1695. if(orgPage)orgPage=orgPage[0];
  1696. let nextLink=ruleParser.getNextLink(iframeDoc);
  1697. if(orgPage && nextLink){
  1698. let display=iframeDoc.defaultView.getComputedStyle(nextLink).display;
  1699. if(display=="none"){
  1700. isPause=true;
  1701. callback(false, false);
  1702. }else{
  1703. nextLink.click();
  1704. setTimeout(()=>{
  1705. checkPage();
  1706. },500);
  1707. }
  1708. }else{
  1709. isPause=true;
  1710. callback(false, false);
  1711. }
  1712. return;
  1713. }
  1714. if(times++ > 10){
  1715. isPause=true;
  1716. callback(false, false);
  1717. return;
  1718. }
  1719. let eles=ruleParser.getPageElement(iframeDoc, iframeDoc.defaultView, true);
  1720. if(!eles || eles.length==0 || orgPage == eles[0]){
  1721. setTimeout(()=>{
  1722. checkPage();
  1723. },500);
  1724. }else{
  1725. callback(iframeDoc, eles);
  1726. }
  1727. }
  1728. if(!emuIframe){
  1729. emuIframe = document.createElement('iframe');
  1730. emuIframe.name = 'pagetual-iframe';
  1731. emuIframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  1732. emuIframe.width = '100%';
  1733. emuIframe.height = '0';
  1734. emuIframe.frameBorder = '0';
  1735. emuIframe.style.cssText = 'margin:0!important;padding:0!important;visibility:hidden!important;';
  1736. emuIframe.addEventListener("load", e=>{
  1737. setTimeout(()=>{
  1738. checkPage();
  1739. },300);
  1740. });
  1741. emuIframe.src=location.href;
  1742. document.body.appendChild(emuIframe);
  1743. }else{
  1744. checkPage();
  1745. }
  1746. }
  1747.  
  1748. function forceIframe(url, callback){
  1749. let curIframe = document.createElement('iframe');
  1750. curIframe.name = 'pagetual-iframe';
  1751. curIframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  1752. curIframe.frameBorder = '0';
  1753. curIframe.scrolling="no";
  1754. 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;';
  1755. curIframe.addEventListener("load", e=>{
  1756. let iframeDoc=curIframe.contentDocument || curIframe.contentWindow.document;
  1757. ruleParser.insertPage(iframeDoc, [], url);
  1758. callback(curIframe, true);
  1759. curIframe.style.height=iframeDoc.body.scrollHeight+"px";
  1760. curIframe.style.width=iframeDoc.body.scrollWidth+"px";
  1761. setTimeout(()=>{
  1762. curIframe.style.height=iframeDoc.body.scrollHeight+"px";
  1763. curIframe.style.width=iframeDoc.body.scrollWidth+"px";
  1764. },300);
  1765. });
  1766. curIframe.src=url;
  1767. let insert=ruleParser.getInsert();
  1768. document.body.insertBefore(curIframe, loading);
  1769. return curIframe;
  1770. }
  1771.  
  1772. function nextPage(){
  1773. if(isPause || isLoading)return;
  1774. let nextLink=ruleParser.nextLinkHref;
  1775. let insert;
  1776. if(nextLink)insert=ruleParser.getInsert();
  1777. if(nextLink && insert){
  1778. let isJs=/^(javascript|#)/.test(nextLink.replace(location.href,""));
  1779. if(location.protocol=="https:" && /^http:/.test(nextLink)){
  1780. nextLink=nextLink.replace(/^http/,"https");
  1781. }
  1782. isLoading=true;
  1783. loading.style.display="";
  1784. if(ruleParser.curSiteRule.pageElementByJs){
  1785. var over=ele=>{
  1786. isLoading=false;
  1787. loading.style.display="none";
  1788. if(ele){
  1789. createPageBar(nextLink);
  1790. ruleParser.insertPage(null, ele, null);
  1791. }else{
  1792. isPause=true;
  1793. }
  1794. };
  1795. try{
  1796. Function("over",'"use strict";' + ruleParser.curSiteRule.pageElementByJs)(over);
  1797. }catch(e){}
  1798. }else if(ruleParser.curSiteRule.action==1 && !isJs){
  1799. requestFromIframe(nextLink, (doc, eles)=>{
  1800. isLoading=false;
  1801. loading.style.display="none";
  1802. if(eles){
  1803. createPageBar(nextLink);
  1804. ruleParser.insertPage(doc, eles, nextLink);
  1805. }
  1806. });
  1807. }else if(forceState==2 && !isJs){
  1808. forceIframe(nextLink, (iframe, eles)=>{
  1809. isLoading=false;
  1810. loading.style.display="none";
  1811. let pageBar=createPageBar(nextLink);
  1812. iframe.parentNode.insertBefore(pageBar, iframe);
  1813. });
  1814. }else{
  1815. if(!isJs){
  1816. requestDoc(nextLink, (eles)=>{
  1817. isLoading=false;
  1818. loading.style.display="none";
  1819. if(eles){
  1820. createPageBar(nextLink);
  1821. }
  1822. });
  1823. }else{
  1824. emuPage((doc, eles)=>{
  1825. isLoading=false;
  1826. loading.style.display="none";
  1827. if(eles){
  1828. createPageBar(nextLink);
  1829. ruleParser.insertPage(doc, eles, "");
  1830. }
  1831. });
  1832. }
  1833. }
  1834. }
  1835. }
  1836.  
  1837. function init(){
  1838. initRules(()=>{
  1839. initView();
  1840. initPage();
  1841. });
  1842. }
  1843. setTimeout(()=>{
  1844. init();
  1845. },300);
  1846. })();