东方永页机

自动加载并拼接下一分页内容(适用于论坛、漫画站、小说站、资讯站、博客等),无需规则支持所有网页

当前为 2022-02-28 提交的版本,查看 最新版本

  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 1.3.9
  14. // @description Most compatible Auto pager script ever! 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,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAdVBMVEUAAAD3VU33VU32VEz8U073VU32VU33VU32VUz0Uk/3VE32VU32VUz2VU32VU32VU32VU33VU33U0z2VU34Wkv3VE32VUz/mpj/nJj2VUz2VU32VE33VEz2VU32VU32VUz3VE32VEz3VE3/mZf2Vkz2VU3/mpilFFolAAAAJXRSTlMA3Lp/GvTBT5YQLuawZ/DOyZwlPQeKc21N04+FX1bqpm9DNoB4T68ePwAAAitJREFUWMPt1tuasiAUBuCFCG5Rs3QybTPV1/1f4v/3PDkyIojn8x5qBrI+ltAfh32/yysmBKvyXb+njb6bDL9kzTd5SzjDAsYT8nFoGSxYe6BVqoNDp2jFDit25BRgVUAODB4YWcETWVTwVNGiGN5iWtBgg4YMCpsoI38dNunmmWyxUTvbPwwbsYR0fIzZLQ0pTG8eieRmBLMmpdH9uimQEf6TNRnXXKLZHixpJtywLzOgMHtFCqdM64DahHRnOE1dsrekm9wr2WtLcAlpdHwcp1pAJySXYnERclzp4+v19jXdmcTvQUJtz+ZaI4i05/V/UGYrCxbaAsOYoNfIKEQxpqQuzCgJJJ/3f42O8ywEZuMVWi/8hODxGj3GW2b0udkbGULLDOjimAG0S3fLGlBnXQM9irG1CiQdVQi0dqQsOSDlyEEz7Vy9OxxfR71VCXsSB23jMrKJYZXSjw57sqgLn5Z0wolsOCz0RyJkyeYjgz7pwwVq20eboZwtVUl2EnN5gJ50dQZFdryATvABRTr/tJXkUMdaAK5pwtCapwtFLskguwuyMh/Sd9WChQ4sIvIUYSk3PYqQvCQlOC04IfN7PkdjOyRKWhdKXMmiAFt9i3sJ5jxoRuR0vqAghxxwHuqfQE5OHGDKOrwEnqs1DgAZ2e4Eev1d45TN7JfhrQLKgfwMFYAsvp33dXII073aVQLI2gN5S58lfmGnKKFtah7nkgnBZB7zlP7Y/QNiTM6sYNzawwAAAABJRU5ErkJggg==
  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. // @supportURL https://github.com/hoothin/UserScripts/issues
  46. // @connect wedata.net
  47. // @connect githubusercontent.com
  48. // @run-at document-idle
  49. // @exclude http://www.toodledo.com/tasks/*
  50. // @exclude http*://maps.google.com*/*
  51. // @exclude *://www.google.*/_/chrome/newtab*
  52. // @exclude *://mega.*/*
  53. // @exclude *://*.mega.*/*
  54. // @exclude *://*.youku.com/v_*
  55. // @exclude *://*pan.baidu.com
  56. // @exclude *://*.iqiyi.com/v_*
  57. // @exclude *://*.iqiyi.com/w_*
  58. // @exclude *://*.iqiyi.com/a_*
  59. // @exclude *://*.le.com/ptv/vplay/*
  60. // @exclude *://v.qq.com/x/cover/*
  61. // @exclude *://v.qq.com/x/page/*
  62. // @exclude *://v.qq.com/tv/*
  63. // @exclude *://*.tudou.com/listplay/*
  64. // @exclude *://*.tudou.com/albumplay/*
  65. // @exclude *://*.tudou.com/programs/view/*
  66. // @exclude *://*.mgtv.com/b/*
  67. // @exclude *://film.sohu.com/album/*
  68. // @exclude *://tv.sohu.com/v/*
  69. // @exclude *://*.bilibili.com/video/*
  70. // @exclude *://*.bilibili.com/bangumi/play/*
  71. // @exclude *://*.baofeng.com/play/*
  72. // @exclude *://vip.pptv.com/show/*
  73. // @exclude *://v.pptv.com/show/*
  74. // @exclude *://www.le.com/ptv/vplay/*
  75. // @exclude *://www.wasu.cn/Play/show/*
  76. // @exclude *://m.v.qq.com/*
  77. // @exclude *://m.iqiyi.com/*
  78. // @exclude *://m.youku.com/alipay_video/*
  79. // @exclude *://m.mgtv.com/b/*
  80. // @exclude *://m.tv.sohu.com/v/*
  81. // @exclude *://m.film.sohu.com/album/*
  82. // @exclude *://m.le.com/ptv/vplay/*
  83. // @exclude *://m.pptv.com/show/*
  84. // @exclude *://m.acfun.cn/v/*
  85. // @exclude *://m.bilibili.com/video/*
  86. // @exclude *://m.bilibili.com/anime/*
  87. // @exclude *://m.bilibili.com/bangumi/play/*
  88. // @exclude *://m.wasu.cn/Play/show/*
  89. // @exclude *://www.youtube.com
  90. // @exclude *://www.youtube.com/
  91. // @exclude *://www.youtube.com/watch*
  92. // @exclude *://baike.baidu.com/*
  93. // @exclude *://*.instagram.com/*
  94. // @exclude *://order.jd.com/*
  95. // @exclude *://pagetual.hoothin.com/*
  96. // @exclude *://hoothin.github.io/PagetualGuide/*
  97. // @connect *
  98. // ==/UserScript==
  99.  
  100. (function() {
  101. 'use strict';
  102.  
  103. if (window.name === 'pagetual-iframe') {
  104. var domloaded = function (){
  105. window.scroll(window.scrollX, 999999);
  106. //window.parent.postMessage('pagetual-iframe:DOMLoaded', '*');
  107. };
  108. if(window.opera){
  109. document.addEventListener('DOMContentLoaded', domloaded, false);
  110. } else {
  111. domloaded();
  112. }
  113. return;
  114. }
  115.  
  116. if(window.top != window.self){
  117. //return;
  118. }
  119.  
  120. const lang = navigator.appName=="Netscape"?navigator.language:navigator.userLanguage;
  121. var i18n=(name, param)=>{
  122. let config={};
  123. switch (lang){
  124. case "zh-CN":
  125. case "zh-SG":
  126. config={
  127. disable:"暂时禁用",
  128. disableSite:"在此站禁用",
  129. enable:"启用翻页",
  130. toTop:"回到顶部",
  131. toBottom:"前往页尾",
  132. current:"当前页",
  133. forceIframe:"强制拼接",
  134. cancelForceIframe:"取消强制拼接",
  135. configure:"打开配置页",
  136. firstUpdate:"点击此处初始化规则",
  137. update:"更新规则",
  138. click2update:"点击立即更新规则",
  139. loadNow:"立即翻页",
  140. loadConfirm:"要翻几页?(0为不间断)",
  141. noNext:"没有找到下一页,请新建规则",
  142. passSec:"更新于 #t# 秒前",
  143. passMin:"更新于 #t# 分钟前",
  144. passHour:"更新于 #t# 小时前",
  145. passDay:"更新于 #t# 天前",
  146. cantDel:"无法删除内置规则",
  147. confirmDel:"是否确认要删除此规则?",
  148. updateSucc:"更新成功",
  149. beginUpdate:"正在更新,请稍候",
  150. customUrls:"导入规则url,一行一条,AutoPagerize 格式规则需要以\"0|\"开头",
  151. customRules:"输入【东方永页机】格式的自定义规则",
  152. save:"保存设置",
  153. loadingText:"少女祈祷中...",
  154. opacity:"分页隔条不透明值",
  155. hideBar:"空白处双击隐藏分页隔条",
  156. dbClick2Stop:"空白处双击暂停翻页",
  157. sortTitle:"排序在下次更新规则后生效",
  158. autoRun:"自动启用",
  159. inputPageNum:"输入页码跳转"
  160. };
  161. break;
  162. case "zh-TW":
  163. case "zh-HK":
  164. config={
  165. disable:"暫時禁用",
  166. disableSite:"在此站禁用",
  167. enable:"啟用翻頁",
  168. toTop:"回到頂部",
  169. toBottom:"前往頁尾",
  170. current:"當前頁",
  171. forceIframe:"強制拼接",
  172. cancelForceIframe:"取消强制拼接",
  173. configure:"打開配置頁",
  174. firstUpdate:"點擊此處初始化規則",
  175. update:"更新規則",
  176. click2update:"點擊立即更新規則",
  177. loadNow:"立即翻頁",
  178. loadConfirm:"要翻几頁?(0為不間斷)",
  179. noNext:"沒有找到下一頁,請新建規則",
  180. passSec:"更新于 #t# 秒前",
  181. passMin:"更新于 #t# 分鐘前",
  182. passHour:"更新于 #t# 小時前",
  183. passDay:"更新于 #t# 天前",
  184. cantDel:"無法刪除内置規則",
  185. confirmDel:"是否確認要刪除此規則?",
  186. updateSucc:"更新成功",
  187. beginUpdate:"正在更新,請稍候",
  188. customUrls:"導入規則url,一行一條,AutoPagerize 格式規則需要以\"0|\"開頭",
  189. customRules:"輸入【東方永頁機】格式的自定義規則",
  190. save:"存儲設置",
  191. loadingText:"少女祈禱中...",
  192. opacity:"分頁隔條不透明值",
  193. hideBar:"空白處雙擊隱藏分頁隔條",
  194. dbClick2Stop:"空白處雙擊暫停翻頁",
  195. sortTitle:"排序在下次更新規則後生效",
  196. autoRun:"自動啓用",
  197. inputPageNum:"輸入頁碼跳轉"
  198. };
  199. break;
  200. case "ja":
  201. config = {
  202. disable: "一時的に無効にする",
  203. disableSite:"このサイト無効",
  204. enable: "ページめくりを有効にする",
  205. toTop: "トップに戻る",
  206. toBottom: "ページの下部に移動",
  207. current: "現在のページ",
  208. forceIframe: "強制ステッチ",
  209. cancelForceIframe: "強制ステッチをキャンセル",
  210. configure: "設定ページを開く",
  211. firstUpdate:"ここをクリックしてルールを初期化します",
  212. update: "更新ルール",
  213. click2update:"今すぐルールを更新してください",
  214. loadNow:"今すぐページをめくる",
  215. loadConfirm:"数ページめくりたいですか?(0は途切れない)",
  216. noNext:"次のページが見つかりません、新しいルールを作成してください",
  217. passSec: "#t#秒前に更新",
  218. passMin: "#t#分前に更新",
  219. passHour: "#t#時間前に更新",
  220. passDay: "#t#日前に更新",
  221. cantDel: "組み込みルールを削除できません",
  222. confirmDel: "このルールを削除してもよろしいですか?",
  223. updateSucc: "更新に成功しました",
  224. beginUpdate: "更新中、お待ちください",
  225. customUrls: "インポートルールのURL、1行に1つ、AutoPagerizeフォーマットルールは\"0|\"で始まる必要があります",
  226. customRules: "【東方永頁機】の形式でカスタムルールを入力してください",
  227. save: "設定を保存",
  228. loadingText: "少女祈祷中...",
  229. opacity:"ページネーションバーの不透明値",
  230. hideBar:"空白部分をダブルクリックして、ページ区切り文字を非表示にします",
  231. dbClick2Stop:"空白部分をダブルクリックしてページめくりを一時停止します",
  232. sortTitle:"並べ替えは、次のルールの更新後に有効になります",
  233. autoRun:"自動的に有効",
  234. inputPageNum:"ジャンプするページ番号を入力"
  235. };
  236. break;
  237. default:
  238. config={
  239. disable:"Disable",
  240. disableSite:"Disable on the site",
  241. enable:"Enable",
  242. toTop:"To Top",
  243. toBottom:"To Bottom",
  244. current:"Current Page",
  245. forceIframe:"Force to join",
  246. cancelForceIframe:"Cancel Force join",
  247. configure:"Configure",
  248. firstUpdate:"Click here to initialize the rules",
  249. update:"Update rules",
  250. click2update:"Click to update rules from url now",
  251. loadNow:"Load next now",
  252. loadConfirm:"How much pages do you want to load? (0 means infinite)",
  253. noNext:"No next link found, please create a new rule",
  254. passSec:"Updated #t# seconds ago",
  255. passMin:"Updated #t# minutes ago",
  256. passHour:"Updated #t# hours ago",
  257. passDay:"Updated #t# days ago",
  258. cantDel:"Can't delete buildin rules",
  259. confirmDel:"Are you sure you want to delete this rule?",
  260. updateSucc:"Update succeeded",
  261. beginUpdate:"Begin update, wait a minute please",
  262. customUrls:"Import rule url, One url per line, rules on AutoPagerize format need to start with \"0|\"",
  263. customRules:"Input custom rules with [Pagetual] format",
  264. save:"Save",
  265. loadingText:"Shojo Now Loading...",
  266. opacity:"Pagination spacer opacity",
  267. hideBar:"Double-click on the blank space to hide the paging spacer",
  268. dbClick2Stop:"Double-click on the blank space to stop",
  269. sortTitle:"Sorting takes effect after the next rule update",
  270. autoRun:"Auto run",
  271. inputPageNum:"Enter page number to jump"
  272. };
  273. break;
  274. }
  275. return config[name]?config[name].replace("#t#",param):name;
  276. };
  277.  
  278. var enableDebug=true;
  279. var debug=str=>{
  280. if(enableDebug){
  281. console.debug(str);
  282. }
  283. };
  284.  
  285. var _GM_xmlhttpRequest,_GM_registerMenuCommand,_GM_notification,_GM_addStyle,_GM_openInTab;
  286. if(typeof GM_xmlhttpRequest!='undefined'){
  287. _GM_xmlhttpRequest=GM_xmlhttpRequest;
  288. }else if(typeof GM!='undefined' && typeof GM.xmlHttpRequest!='undefined'){
  289. _GM_xmlhttpRequest=GM.xmlHttpRequest;
  290. }else{
  291. _GM_xmlhttpRequest=(f)=>{fetch(f.url).then(response=>response.text()).then(data=>{let res={response:data};f.onload(res)}).catch(f.onerror())};
  292. }
  293. if(typeof GM_registerMenuCommand!='undefined'){
  294. _GM_registerMenuCommand=GM_registerMenuCommand;
  295. }else if(typeof GM!='undefined' && typeof GM.registerMenuCommand!='undefined'){
  296. _GM_registerMenuCommand=GM.registerMenuCommand;
  297. }else{
  298. _GM_registerMenuCommand=(s,f)=>{};
  299. }
  300. if(typeof GM_notification!='undefined'){
  301. _GM_notification=GM_notification;
  302. }else if(typeof GM!='undefined' && typeof GM.notification!='undefined'){
  303. _GM_notification=GM.notification;
  304. }else{
  305. _GM_notification=(s)=>{alert(s)};
  306. }
  307. if(typeof GM_openInTab!='undefined'){
  308. _GM_openInTab=GM_openInTab;
  309. }else if(typeof GM!='undefined' && typeof GM.openInTab!='undefined'){
  310. _GM_openInTab=GM.openInTab;
  311. }else{
  312. _GM_openInTab=(s,t)=>{window.open(s)};
  313. }
  314. if(typeof GM_addStyle!='undefined'){
  315. _GM_addStyle=GM_addStyle;
  316. }else if(typeof GM!='undefined' && typeof GM.addStyle!='undefined'){
  317. _GM_addStyle=GM.addStyle;
  318. }else{
  319. _GM_addStyle=cssStr=>{
  320. let styleEle=document.createElement("style");
  321. styleEle.innerHTML=cssStr;
  322. document.head.appendChild(styleEle);
  323. };
  324. }
  325. var _unsafeWindow=(typeof unsafeWindow=='undefined')?window:unsafeWindow;//兼容 ios userscripts 的寫法
  326. var storage={
  327. supportGM: typeof GM_getValue=='function' && typeof GM_getValue('a','b')!='undefined',
  328. supportGMPromise: typeof GM!='undefined' && typeof GM.getValue=='function' && typeof GM.getValue('a','b')!='undefined',
  329. mxAppStorage:(function(){
  330. try{
  331. return window.external.mxGetRuntime().storage;
  332. }catch(e){
  333. }
  334. })(),
  335. operaUJSStorage:(function(){
  336. try{
  337. return window.opera.scriptStorage;
  338. }catch(e){
  339. }
  340. })(),
  341. setItem:function(key,value){
  342. if(this.operaUJSStorage){
  343. this.operaUJSStorage.setItem(key,value);
  344. }else if(this.mxAppStorage){
  345. this.mxAppStorage.setConfig(key,value);
  346. }else if(this.supportGM){
  347. GM_setValue(key,value);
  348. if(value==="" && typeof GM_deleteValue!='undefined'){
  349. GM_deleteValue(key);
  350. }
  351. }else if(this.supportGMPromise){
  352. GM.setValue(key,value);
  353. if(value==="" && typeof GM!='undefined' && typeof GM.deleteValue!='undefined'){
  354. GM.deleteValue(key);
  355. }
  356. }else if(window.localStorage){
  357. window.localStorage.setItem(key,value);
  358. }
  359. },
  360. getItem:function(key,cb){
  361. var value;
  362. if(this.operaUJSStorage){
  363. value=this.operaUJSStorage.getItem(key);
  364. }else if(this.mxAppStorage){
  365. value=this.mxAppStorage.getConfig(key);
  366. }else if(this.supportGM){
  367. value=GM_getValue(key);
  368. }else if(this.supportGMPromise){
  369. value=GM.getValue(key).then(v=>{cb(v)});
  370. return;
  371. }else if(window.localStorage){
  372. value=window.localStorage.getItem(key);
  373. };
  374. cb(value);
  375. }
  376. };
  377. var rulesData={},ruleUrls,updateDate,configPage="https://github.com/hoothin/UserScripts/tree/master/Pagetual";
  378. var allOfBody="body>*";
  379. _GM_registerMenuCommand(i18n("configure"), ()=>{
  380. _GM_openInTab(configPage,{active:true});
  381. });
  382.  
  383. function getElementByXpath(xpath, contextNode, doc){
  384. doc = doc || document;
  385. contextNode = contextNode || doc;
  386. try {
  387. var result = doc.evaluate(xpath, contextNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  388. return result.singleNodeValue && result.singleNodeValue.nodeType === 1 && result.singleNodeValue;
  389. } catch (err) {
  390. throw new Error(`Invalid xpath: ${xpath}`);
  391. }
  392. }
  393.  
  394. function getAllElementsByXpath(xpath, contextNode, doc){
  395. doc = doc || document;
  396. contextNode = contextNode || doc;
  397. var result = [];
  398. try {
  399. var query = doc.evaluate(xpath, contextNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  400. for (var i = 0; i < query.snapshotLength; i++) {
  401. var node = query.snapshotItem(i);
  402. if (node.nodeType === 1) result.push(node);
  403. }
  404. } catch (err) {
  405. throw new Error(`Invalid xpath: ${xpath}`);
  406. }
  407. return result;
  408. }
  409.  
  410. class RuleParser {
  411. constructor() {
  412. this.hpRules=[];
  413. this.customRules=[];
  414. this.rules=[];
  415. this.pageDoc=document;
  416. this.nextLinkHref=null;
  417. this.curUrl=location.href;
  418. this.curSiteRule={};
  419. }
  420.  
  421. initSavedRules(callback){
  422. var self=this;
  423. storage.getItem("hpRules", hpRules=>{
  424. if(hpRules)self.hpRules=hpRules;
  425. storage.getItem("customRules", customRules=>{
  426. if(customRules)self.customRules=customRules;
  427. storage.getItem("rules", rules=>{
  428. if(rules)this.rules=rules;
  429. callback();
  430. });
  431. });
  432. });
  433. }
  434.  
  435. saveCurSiteRule(){
  436. if(!this.curSiteRule || !this.curSiteRule.url || this.curSiteRule.singleUrl)return;
  437. this.hpRules=this.hpRules.filter(item=>{return item&&item.url!=this.curSiteRule.url});
  438. this.hpRules.unshift(this.curSiteRule);
  439. if(this.hpRules.length>30){
  440. this.hpRules.pop();
  441. }
  442. storage.setItem("hpRules", this.hpRules);
  443. }
  444.  
  445. requestJSON(url, callback){
  446. _GM_xmlhttpRequest({
  447. url: url,
  448. method: 'GET',
  449. timeout: 20000,
  450. onload: function(res) {
  451. let json=null;
  452. try{
  453. json=JSON.parse(res.response||res.responseText);
  454. }catch(e){
  455. debug(e);
  456. }
  457. callback(json);
  458. },
  459. onerror: function(e) {
  460. callback(null, e);
  461. },
  462. ontimeout: function(e) {
  463. callback(null, e);
  464. }
  465. });
  466. }
  467.  
  468. formatRule(item, type, from){
  469. switch(type){
  470. case 0:
  471. return {
  472. from:from,
  473. type:type,
  474. name:item.name,
  475. action:item.data.forceIframe=="true"?1:0,
  476. url:item.data.url,
  477. pageElement:item.data.pageElement,
  478. nextLink:item.data.nextLink,
  479. insert:item.data.insertBefore,
  480. insertPos:1,
  481. updatedAt:item.data.updated_at,
  482. css:item.data.Stylus+item.data.CSS,
  483. pageAction:item.data.bookmarklet
  484. };
  485. break;
  486. case 1:
  487. default:
  488. item.from=from;
  489. if(typeof(item.type) == "undefined")item.type=type;
  490. return item;
  491. break;
  492. }
  493. return null;
  494. }
  495.  
  496. addRuleByUrl(url, type, from, callback) {
  497. if(url.indexOf("?")==-1){
  498. url=url+"?"+Date.now();
  499. }else{
  500. url=url+"&"+Date.now();
  501. }
  502. this.requestJSON(url, (json,err)=>{
  503. if(!json){
  504. debug("Update "+url+" rules fail!");
  505. debug(err);
  506. }
  507. this.addRules(json, type, from);
  508. callback(json, err);
  509. });
  510. }
  511.  
  512. addRules(rules, type, from) {
  513. if(rules && rules.length>0){
  514. this.rules=this.rules.filter(item=>{return item.from!=from});
  515. rules.forEach(item=>{
  516. let rule=this.formatRule(item, type, from);
  517. if(rule){
  518. this.rules.push(rule);
  519. }
  520. });
  521. }
  522. }
  523.  
  524. getRule(callback) {
  525. if(this.curSiteRule && this.curSiteRule.url){
  526. return this.curSiteRule;
  527. }
  528. var self=this;
  529.  
  530. function setRule(r){
  531. self.curSiteRule=r;
  532. debug(r);
  533. callback();
  534. }
  535.  
  536. function ruleMatch(r){
  537. let pageElement,nextLink,insert;
  538. if(r.pageElement)pageElement=r.type==0?getElementByXpath(r.pageElement):document.querySelector(r.pageElement);
  539. if(r.nextLink)nextLink=r.type==0?getElementByXpath(r.nextLink):document.querySelector(r.nextLink);
  540. if(r.insert)insert=r.type==0?getElementByXpath(r.insert):document.querySelector(r.insert);
  541. return !((r.pageElement && !pageElement) ||
  542. (r.nextLink && !nextLink) ||
  543. (r.insert && !insert));
  544. }
  545.  
  546. function checkRule(r){
  547. let urlReg=new RegExp(r.url, "i");
  548. if(urlReg.test(location.href)){
  549. if(r.wait){
  550. let waitTime=500,checkEval;
  551. if(isNaN(r.wait)){
  552. try{
  553. checkEval=Function("doc",'"use strict";' + r.wait);
  554. }catch(e){
  555. debug(e);
  556. }
  557. }else{
  558. waitTime=r.wait;
  559. }
  560. let checkReady=()=>{
  561. setTimeout(()=>{
  562. if(!ruleMatch(r) || (checkEval && !checkEval(document))){
  563. checkReady();
  564. }else{
  565. setRule(r);
  566. }
  567. },parseInt(waitTime));
  568. };
  569. checkReady();
  570. return true;
  571. }
  572. if(r.pinUrl){
  573. setRule(r);
  574. return true;
  575. }
  576. if(!ruleMatch(r)){
  577. return false;
  578. }
  579. setRule(r);
  580. return true;
  581. }
  582. return false;
  583. }
  584.  
  585. for(let i in this.hpRules){
  586. let rule=this.hpRules[i];
  587. if(!rule || !rule.url)continue;
  588. if(rule.singleUrl){
  589. if(location.origin+location.pathname==rule.url){
  590. setRule(rule);
  591. return;
  592. }
  593. continue;
  594. }
  595. if(checkRule(rule))return;
  596. }
  597. for(let i in this.customRules){
  598. let rule=this.customRules[i];
  599. if(!rule || !rule.url)continue;
  600. if(checkRule(rule))return;
  601. }
  602. let r=0;
  603. function searchByTime(){
  604. setTimeout(()=>{
  605. let end=r+80;
  606. end=end>self.rules.length?self.rules.length:end;
  607. for(;r<end;r++){
  608. let rule=self.rules[r];
  609. if(checkRule(rule))return;
  610. }
  611. if(end>=self.rules.length){
  612. self.curSiteRule={};
  613. self.curSiteRule.url=location.origin+location.pathname;
  614. self.curSiteRule.singleUrl=true;
  615. callback();
  616. return;
  617. }else{
  618. searchByTime();
  619. }
  620. },20);
  621. }
  622. searchByTime();
  623. }
  624.  
  625. geneSelector(ele){
  626. let selector=ele.tagName;
  627. //Google id class都是隨機。百度更過分,style script順序都是隨機的
  628. //if(ele.id) selector += '#' + ele.id;
  629. //if(ele.classList) selector += [].map.call(ele.classList,d=>'.'+d).join('');
  630. let parent = ele.parentElement;
  631. if(parent){
  632. let i,j=0;
  633. for(i=0;i<parent.children.length;i++){
  634. if(parent.children[i].tagName==selector){
  635. j++;
  636. if(parent.children[i]==ele){
  637. break;
  638. }
  639. }
  640. }
  641. selector = this.geneSelector(parent) + ' > ' + selector + (parent.tagName=="HTML"?"":`:nth-of-type(${j})`);
  642. }
  643. return selector;
  644. }
  645.  
  646. getPageElement(doc, curWin, dontFind) {
  647. let pageElement=null;
  648. let self=this;
  649. if(this.curSiteRule.pageElement){
  650. pageElement=this.curSiteRule.type==0?getAllElementsByXpath(this.curSiteRule.pageElement,doc,doc):doc.querySelectorAll(this.curSiteRule.pageElement);
  651. }
  652. if((!pageElement || pageElement.length==0) && curWin && !dontFind){
  653. let body=doc.body;
  654. if(!body)return null;
  655. let bodyHeight=parseInt(body.scrollHeight);
  656. let curHeight=bodyHeight;
  657. function checkElement(ele){
  658. if(curHeight/bodyHeight<=0.30){
  659. self.curSiteRule.pageElement=allOfBody;
  660. debug(self.curSiteRule.pageElement);
  661. return [body];
  662. }
  663. if(ele.tagName=="TABLE" || ele.tagName=="UL"){
  664. self.curSiteRule.pageElement=self.geneSelector(ele);
  665. debug(self.curSiteRule.pageElement);
  666. return [ele];
  667. }
  668. if(ele.children.length==0 && !self.curSiteRule.pageElement){
  669. if(ele.parentNode.tagName=="P")ele=ele.parentNode;
  670. self.curSiteRule.pageElement=self.geneSelector(ele.parentNode)+">"+ele.tagName;
  671. debug(self.curSiteRule.pageElement);
  672. return [ele];
  673. }
  674. let i,maxHeight=curHeight*0.6,curMaxEle=null,curMaxArea=0,maxWidth=0;
  675. let isHori=true;
  676. let offsetTop=ele.children[0].offsetTop;
  677. for(i=0;i<ele.children.length;i++){
  678. if(ele.children[i].offsetTop!=offsetTop){
  679. isHori=false;
  680. break;
  681. }
  682. }
  683. for(i=0;i<ele.children.length;i++){
  684. let curNode=ele.children[i];
  685. let comStyle=curWin.getComputedStyle(curNode);
  686. let h=parseInt(curNode.scrollHeight);
  687. let w=parseInt(curNode.scrollWidth);
  688. if(isNaN(h) || isNaN(w))continue;
  689. if(isHori && h==0)continue;
  690. let a=h*w+h,moreChild=curNode.children[0];
  691. while(moreChild){
  692. comStyle=curWin.getComputedStyle(moreChild);
  693. let ch=parseInt(moreChild.scrollHeight);
  694. let cw=parseInt(moreChild.scrollWidth);
  695. if(h<ch)h=ch;
  696. if(moreChild.innerText!="" && !isNaN(ch) && !isNaN(cw)){
  697. a+=ch*cw;
  698. }
  699. moreChild=moreChild.nextElementSibling;
  700. }
  701. let isMax=false;
  702. if(isHori){
  703. if(maxWidth<w){
  704. isMax=true;
  705. }else if(maxWidth==w && curMaxArea<a){
  706. isMax=true;
  707. }
  708. }else{
  709. isMax=curMaxArea<a;
  710. }
  711. if(curMaxEle==null || isMax){
  712. if(h>0)curHeight=h;
  713. curMaxArea=a;
  714. maxWidth=w;
  715. curMaxEle=curNode;
  716. }
  717. }
  718. if(curMaxEle && (isHori || curHeight>maxHeight)){
  719. return checkElement(curMaxEle);
  720. }
  721. if(!self.curSiteRule.pageElement){
  722. if(ele.tagName=="P" || ele.tagName=="BR")ele=ele.parentNode;
  723. self.curSiteRule.pageElement=self.geneSelector(ele);
  724. if(ele.children.length>1 && ele.tagName!="UL")self.curSiteRule.pageElement+=">*";
  725. debug(self.curSiteRule.pageElement);
  726. }
  727. return (ele.tagName=="UL"||ele.children.length==1)?[ele]:ele.children;
  728. }
  729. pageElement=checkElement(body);
  730. if(pageElement)this.saveCurSiteRule();
  731. }
  732. if(this.insert && pageElement && pageElement[0]){
  733. let example=this.insertPos==2?this.insert.children[0]:this.insert;
  734. let inTable=example.parentNode.tagName=="TABLE" ||
  735. example.tagName=="TR" ||
  736. example.tagName=="TBODY" ||
  737. (example.previousElementSibling && example.previousElementSibling.tagName=="TR") ||
  738. (example.previousElementSibling && example.previousElementSibling.tagName=="TBODY");
  739. if(inTable && pageElement[0].tagName!="TR" && pageElement[0].tagName!="TBODY"){
  740. pageElement=null;
  741. }
  742. }
  743. return pageElement;
  744. }
  745.  
  746. getPage(doc){
  747. if(_unsafeWindow.Discourse)return {};
  748. let canSave=false;//發現頁碼選擇器在其他頁對不上,還是別保存了
  749. let url=this.curUrl.replace("#!","").replace("index.php?","?");
  750. let pageNum=1,preStr="",afterStr="";
  751. let pageMatch1=url.match(/(.*[a-z\/\-_](?:p|page)?\/?)(\d+)(\.s?html?$|\/?$)/i);
  752. let pageMatch2=url.match(/(.*[\?&]p(?:age)?=)(\d+)($|[#&].*)/i);
  753. if(pageMatch1){
  754. preStr=pageMatch1[1];
  755. pageNum=pageMatch1[2];
  756. afterStr=pageMatch1[3];
  757. }else if(pageMatch2){
  758. preStr=pageMatch2[1];
  759. pageNum=pageMatch2[2];
  760. afterStr=pageMatch2[3];
  761. }
  762. let curPage=doc,i,cur,jsNext;
  763. let next=curPage.querySelector(".page-next>a")||
  764. curPage.querySelector("a.next_page")||
  765. curPage.querySelector("#next_page")||
  766. curPage.querySelector(".nextPage")||
  767. curPage.querySelector("a[data-pagination=next]")||
  768. curPage.querySelector(".pagination a[rel=next]")||
  769. curPage.querySelector("a.pageright")||
  770. curPage.querySelector("a#rightFix")||
  771. curPage.querySelector("a.next")||
  772. curPage.querySelector("a#next")||
  773. curPage.querySelector(".next>a")||
  774. curPage.querySelector(".next>button")||
  775. curPage.querySelector("a[alt=next]")||
  776. curPage.querySelector("[title=next]");
  777. if(next && (!next.href || /^javascript:/.test(next.href))){
  778. jsNext=next;
  779. next=null;
  780. }
  781. if(!next){
  782. next=curPage.querySelectorAll("[aria-label='Next']");
  783. if(next && next.length==1){
  784. next=next[0];
  785. if(!next.href || /^javascript:/.test(next.href)){
  786. jsNext=next;
  787. next=null;
  788. }
  789. }else{
  790. next=null;
  791. }
  792. }
  793. if(!next){
  794. next=curPage.querySelectorAll("[aria-label='Next page']");
  795. if(next && next.length==1){
  796. next=next[0];
  797. if(!next.href || /^javascript:/.test(next.href)){
  798. jsNext=next;
  799. next=null;
  800. }
  801. }else{
  802. next=null;
  803. }
  804. }
  805. if(!next){
  806. let pageDiv=curPage.querySelector("div.wp-pagenavi");
  807. if(pageDiv){
  808. cur=pageDiv.querySelector("span.current");
  809. if(cur){
  810. next=cur.nextSibling;
  811. }
  812. }else{
  813. cur=curPage.querySelector("div.article-paging>span");
  814. if(cur){
  815. next=cur.nextElementSibling;
  816. }
  817. }
  818. }
  819. if(!next){
  820. let pageDiv=curPage.querySelector("div.pages>ul");
  821. if(pageDiv){
  822. cur=pageDiv.querySelector("li>b");
  823. if(cur)next=cur.parentNode.nextElementSibling.querySelector("a");
  824. }
  825. }
  826. if(!next){
  827. let aTags=curPage.querySelectorAll("a");
  828. let next1,next2,next3,next4,nextJs1,nextJs2,nextJs3;
  829. for(i=aTags.length-1;i>=0;i--){
  830. let aTag=aTags[i];
  831. if(aTag.innerText=="§")continue;
  832. if(aTag.style.display=="none")continue;
  833. if(next1 && next2 && next3 && next4)break;
  834. if(!next1){
  835. if(/^\s*[下后後][一1]?[页頁张張]|^next( page)?\s*›?$|次のページ/i.test(aTag.innerText)){
  836. if(!aTag.href || /^javascript:/.test(aTag.href)){
  837. if(!nextJs1)nextJs1=aTag;
  838. }else{
  839. next1=aTag;
  840. }
  841. }
  842. }
  843. if(!next2){
  844. if(/^\s*[下后後][一1]?[章话話]/i.test(aTag.innerText)){
  845. if(!aTag.href || /^javascript:/.test(aTag.href)){
  846. if(!nextJs2)nextJs2=aTag;
  847. }else{
  848. next2=aTag;
  849. }
  850. }
  851. }
  852. if(!next3){
  853. if(aTag.innerText=="&gt;" || aTag.innerText=="▶" || aTag.innerText==">" || aTag.innerText=="›" || /nextpage/i.test(aTag.className)){
  854. if(!aTag.href || /^javascript:/.test(aTag.href)){
  855. if(!nextJs3)nextJs3=aTag;
  856. }else{
  857. next3=aTag;
  858. }
  859. }
  860. }
  861. if(!aTag.href || /^javascript:/.test(aTag.href))continue;
  862. if(!next4){
  863. if(aTag.href.replace("?&","?").replace("#!","").replace("index.php?","?").replace(preStr,"").replace(afterStr,"")==parseInt(pageNum)+1){
  864. next4=aTag;
  865. }else if(aTag.href.replace("?&","?").replace("index.php?","?").indexOf(url)!=-1 && /^[\/\?&]?[_-]?(p|page)?=?\/?2(\/|\?|&|$)/i.test(aTag.href.replace(url,"").replace(/\.s?html?$/i,""))){
  866. let pageOne=aTag.getAttribute("href").replace(/2(\/|\?|&|$)/,"1$1");
  867. pageOne=curPage.querySelector(`a[href='${pageOne}']`);
  868. if(!pageOne)next4=aTag;
  869. }
  870. }
  871. }
  872. next=next1||next3||next4||next2||nextJs1||nextJs3||nextJs2;
  873. }
  874. if(!next)next=jsNext;
  875. if(next && next.classList.contains("results-more"))next=null;
  876. return {next:next,canSave:canSave};
  877. }
  878.  
  879. canonicalUri(src, base_path) {
  880. if(src.charAt(0)=="#")return location.href+src;
  881. var root_page = /^[^?#]*\//.exec(location.href)[0],
  882. root_domain = /^\w+\:\/\/\/?[^\/]+/.exec(root_page)[0],
  883. absolute_regex = /^\w+\:\/\//;
  884. src=src.replace(/\.\//,"");
  885. if (/^\/\/\/?/.test(src)){
  886. src = location.protocol + src;
  887. }
  888. else if (!absolute_regex.test(src) && src.charAt(0) != "/"){
  889. src = (base_path || "") + src;
  890. }
  891. return (absolute_regex.test(src) ? src : ((src.charAt(0) == "/" ? root_domain : root_page) + src));
  892. }
  893.  
  894. getLinkByPage(url, pageNum) {
  895. if(!url)return;
  896. if(this.curSiteRule.pageNum){
  897. let result=this.curSiteRule.pageNum;
  898. let strMatch=result.match(/\{.*?}/);
  899. if(!strMatch)return null;
  900. let urlReg=new RegExp("("+result.replace(strMatch[0], ")\\d+(")+")");
  901. let code=strMatch[0].replace(/^{/,"").replace(/}$/,"").replace(/\$p/g,pageNum);
  902. if(code==pageNum){
  903. result=url.replace(urlReg,"$1"+code+"$2");
  904. }else{
  905. try{
  906. code=Function('"use strict";return ' + code)();
  907. result=url.replace(urlReg,"$1"+code+"$2");
  908. }catch(e){
  909. debug(e);
  910. }
  911. }
  912. return result==url?url.replace(/([&\/\?](p=|page[=\/]))\d+/i, "$1"+pageNum):result;
  913. }else{
  914. return url.replace(/([&\/\?](p=|page[=\/]))\d+/i, "$1"+pageNum);
  915. }
  916. }
  917.  
  918. getNextLink(doc) {
  919. let nextLink=null,page;
  920. if(this.curSiteRule.pageElementByJs){
  921. this.nextLinkHref="#";
  922. return true;
  923. }else if(this.curSiteRule.nextLinkByJs){
  924. try{
  925. let targetUrl=Function("doc",'"use strict";' + this.curSiteRule.nextLinkByJs)(doc);
  926. if(targetUrl)nextLink={href:targetUrl};
  927. }catch(e){
  928. debug(e);
  929. }
  930. }else if(this.curSiteRule.nextLinkByUrl){
  931. let targetUrl=this.curUrl.replace(new RegExp(this.curSiteRule.nextLinkByUrl[0]), this.curSiteRule.nextLinkByUrl[1]);
  932. if(targetUrl != this.curUrl){
  933. let reps=targetUrl.match(/{.*?}/g);
  934. if(reps){
  935. reps.forEach(rep=>{
  936. let code=rep.replace("{","").replace("}","");
  937. let result=code.match(/^(\d+)\+1$/);
  938. if(result){
  939. result=parseInt(result[1])+1;
  940. }else{
  941. try{
  942. result=Function('"use strict";return ' + code)();
  943. }catch(e){
  944. debug(e);
  945. }
  946. }
  947. targetUrl=targetUrl.replace(rep, result);
  948. });
  949. }
  950. }
  951. nextLink={href:targetUrl};
  952. }else if(this.curSiteRule.nextLink){
  953. if(this.curSiteRule.nextLink!="0"){
  954. nextLink=this.curSiteRule.type==0?getElementByXpath(this.curSiteRule.nextLink,doc,doc):doc.querySelector(this.curSiteRule.nextLink);
  955. }
  956. }else{
  957. page=this.getPage(doc);
  958. nextLink=page.next;
  959. if(nextLink){
  960. if(nextLink.tagName=="INPUT" && nextLink.parentNode.tagName=="FORM"){
  961. let params=[];
  962. let form=nextLink.parentNode;
  963. [].forEach.call(form.querySelectorAll("input"), input=>{
  964. params.push(input.name+'='+input.value);
  965. });
  966. nextLink.href=form.action+'?'+params.join('&');
  967. }
  968. if(nextLink.classList.contains("noClick")){
  969. this.nextLinkHref=false;
  970. return null;
  971. }else if(doc==document){
  972. if((!nextLink.href || /^javascript:/.test(nextLink.href)) && !isVisible(nextLink, _unsafeWindow)){
  973. this.nextLinkHref=false;
  974. return null;
  975. }else{
  976. let nextLinkCs=_unsafeWindow.getComputedStyle(nextLink);
  977. if(nextLinkCs.cursor=="not-allowed"){
  978. this.nextLinkHref=false;
  979. return null;
  980. }
  981. }
  982. }
  983. }
  984. }
  985. if(nextLink){
  986. if(nextLink.href && nextLink.href==this.curUrl){
  987. nextLink=null;
  988. }else if(/^javascript:/.test(nextLink.href) && (this.curSiteRule.action==0 || this.curSiteRule.action==1)){
  989. nextLink=null;
  990. }
  991. /*if(!this.curSiteRule.nextLink && page && page.canSave){
  992. this.curSiteRule.nextLink=this.geneSelector(nextLink);
  993. this.curSiteRule.type=1;
  994. this.saveCurSiteRule();
  995. }*/
  996. }
  997. if(nextLink){
  998. this.nextLinkHref=nextLink.href?this.canonicalUri(nextLink.href):"#";
  999. debug(nextLink);
  1000. }else{
  1001. this.nextLinkHref=false;
  1002. }
  1003. return nextLink;
  1004. }
  1005.  
  1006. getInsert(refresh) {
  1007. if(refresh){
  1008. this.insert=null;
  1009. }
  1010. if(this.insert && !refresh){
  1011. let parent=this.insert;
  1012. while(parent && parent.nodeName!="BODY"){
  1013. parent=parent.parentNode;
  1014. }
  1015. if(parent && parent.nodeName=="BODY"){
  1016. return this.insert;
  1017. }
  1018. }
  1019. if(this.curSiteRule.insert){
  1020. this.insert=this.curSiteRule.type==0?getElementByXpath(this.curSiteRule.insert,document):document.querySelector(this.curSiteRule.insert);
  1021. }else{
  1022. let pageElement=this.getPageElement(document, _unsafeWindow);
  1023. if(pageElement && pageElement.length>0){
  1024. var pELast = pageElement[pageElement.length - 1];
  1025. this.insert = pELast.nextSibling ? pELast.nextSibling : pELast.parentNode.appendChild(document.createTextNode(' '));
  1026. }
  1027. }
  1028. return this.insert;
  1029. }
  1030.  
  1031. pageAction(doc,eles){
  1032. let code=this.curSiteRule.pageAction;
  1033. if(code){
  1034. try{
  1035. Function("doc","eles",'"use strict";' + code)(doc,eles);
  1036. }catch(e){
  1037. debug(e);
  1038. }
  1039. }
  1040. let setLazyImg=img=>{
  1041. let realSrc;
  1042. if(img.getAttribute("_src") && !img.src){
  1043. realSrc=img.getAttribute("_src");
  1044. }else if(img.dataset && img.dataset.original){
  1045. realSrc=img.dataset.original;
  1046. }else if(img.dataset && img.dataset.src){
  1047. realSrc=img.dataset.src;
  1048. }else if(img._lazyrias && img._lazyrias.srcset){
  1049. realSrc=img._lazyrias.srcset[img._lazyrias.srcset.length-1];
  1050. }else if(img.dataset && img.dataset.origFile){
  1051. realSrc=img.dataset.origFile;
  1052. }else if(img.srcset){
  1053. var srcs=img.srcset.split(","),largeSize=0;
  1054. srcs.forEach(srci=>{
  1055. let srcInfo=srci.trim().split(" "),curSize=parseInt(srcInfo[1]);
  1056. if(srcInfo[1] && curSize>largeSize){
  1057. largeSize=curSize;
  1058. realSrc=srcInfo[0];
  1059. }
  1060. });
  1061. }
  1062. if(realSrc){
  1063. img.src=realSrc;
  1064. img.style.display="block";
  1065. }
  1066. };
  1067. [].forEach.call(eles, ele=>{
  1068. if(ele.tagName=="IMG"){
  1069. setLazyImg(ele);
  1070. }else{
  1071. [].forEach.call(ele.querySelectorAll("img"), img=>{
  1072. setLazyImg(img);
  1073. });
  1074. }
  1075. if(ele.tagName=="A" && ele.classList.contains("lazyload")){
  1076. if(ele.dataset.original){
  1077. ele.style.backgroundImage='url("'+ele.dataset.original+'")';
  1078. }
  1079. }else{
  1080. [].forEach.call(ele.querySelectorAll("a.lazyload"), a=>{
  1081. if(a.dataset.original){
  1082. a.style.backgroundImage='url("'+a.dataset.original+'")';
  1083. }
  1084. });
  1085. }
  1086. });
  1087. let lazyImgSrc=this.curSiteRule.lazyImgSrc;
  1088. if(lazyImgSrc){
  1089. [].forEach.call(eles, ele=>{
  1090. [].forEach.call(ele.querySelectorAll("img"), img=>{
  1091. if(img.getAttribute(lazyImgSrc)){
  1092. img.src=img.getAttribute(lazyImgSrc);
  1093. }
  1094. });
  1095. });
  1096. }
  1097. }
  1098.  
  1099. initPage(callback){
  1100. let self=this;
  1101. curPage=1;
  1102. //if(this.curSiteRule.url && !this.curSiteRule.singleUrl)return;
  1103. this.curSiteRule={};
  1104. this.pageDoc=document;
  1105. this.nextLinkHref=null;
  1106. this.curUrl=location.href;
  1107. this.getRule(()=>{
  1108. if(self.curSiteRule.enable==0){
  1109. debug("Stop as rule disable");
  1110. isPause=true;
  1111. return;
  1112. }
  1113. if(self.curSiteRule && !self.curSiteRule.singleUrl){
  1114. self.hpRules=self.hpRules.filter(item=>{return item&&item.url!=self.curSiteRule.url});
  1115. self.hpRules.unshift(self.curSiteRule);
  1116. if(self.hpRules.length>30){
  1117. self.hpRules.pop();
  1118. }
  1119. storage.setItem("hpRules", self.hpRules);
  1120. }
  1121. let css=self.curSiteRule.css;
  1122. if(css){
  1123. _GM_addStyle(css);
  1124. }
  1125. let code=self.curSiteRule.init;
  1126. if(code){
  1127. try{
  1128. Function('doc','"use strict";' + code)(null);
  1129. }catch(e){
  1130. debug(e);
  1131. }
  1132. }
  1133. self.getNextLink(document);
  1134. callback();
  1135. });
  1136. }
  1137.  
  1138. insertPage(doc, eles, url, callback, tried){
  1139. this.pageDoc=doc;
  1140. this.curUrl=url;
  1141. let nextLink=this.getNextLink(doc);
  1142. if(curPage==1 && !tried && !this.nextLinkHref && this.curSiteRule.singleUrl && this.curSiteRule.pageElement && this.curSiteRule.pageElement!=allOfBody){
  1143. this.curSiteRule.action=1;
  1144. return false;
  1145. }
  1146. if(callback)callback(eles);
  1147. this.getInsert();
  1148. var self=this,newEles=[];
  1149. if(!eles || eles.length==0 || !self.insert || !self.insert.parentNode){
  1150. }else{
  1151. [].forEach.call(eles, ele=>{
  1152. let newEle=ele.cloneNode(true);
  1153. let oldCanvass=ele.querySelectorAll("canvas");
  1154. let newCanvass=newEle.querySelectorAll("canvas");
  1155. for(let i=0;i<oldCanvass.length;i++){
  1156. let oldCanvas=oldCanvass[i];
  1157. let newCanvas=newCanvass[i];
  1158. newCanvas.getContext('2d').drawImage(oldCanvas, 0, 0);
  1159. }
  1160. if(self.curSiteRule.insertPos==2){
  1161. self.insert.appendChild(newEle);
  1162. }else{
  1163. self.insert.parentNode.insertBefore(newEle, self.insert);
  1164. }
  1165. newEles.push(newEle);
  1166. });
  1167. }
  1168. this.pageAction(doc, newEles);
  1169. return true;
  1170. }
  1171. }
  1172. var ruleParser = new RuleParser();
  1173.  
  1174. function initConfig(){
  1175. _GM_registerMenuCommand(i18n(forceState==1?"enable":"disableSite"), ()=>{
  1176. storage.setItem("forceState_"+location.host, (forceState==1?0:1));
  1177. location.reload();
  1178. });
  1179. _GM_registerMenuCommand(i18n("update"), ()=>{
  1180. updateRules(()=>{
  1181. alert(i18n("updateSucc"));
  1182. location.reload();
  1183. },(rule,err)=>{
  1184. alert("Update "+rule.url+" rules fail! "+err);
  1185. });
  1186. alert(i18n("beginUpdate"));
  1187. });
  1188.  
  1189. var configCon,insertPos;
  1190. if(location.href==configPage){
  1191. _GM_addStyle(`
  1192. p>span:nth-child(1),p>span:nth-child(2),p>span:nth-child(3){
  1193. cursor: pointer;
  1194. user-select: none;
  1195. }
  1196. p>span:nth-child(1):hover,p>span:nth-child(2):hover,p>span:nth-child(3):hover{
  1197. color:red;
  1198. }
  1199. .updateDate{
  1200. cursor: pointer;
  1201. user-select: none;
  1202. }
  1203. .updateDate:hover{
  1204. color:red;
  1205. }
  1206. `);
  1207. configCon=document.querySelector(".markdown-body");
  1208. insertPos=configCon.querySelector("hr");
  1209. }else if(location.href==configPage){
  1210. }else return false;
  1211. class Rulebar {
  1212. init(ruleUrl){
  1213. this.ruleUrl=ruleUrl;
  1214. this.item=document.createElement("p");
  1215. this.item.title=ruleUrl.type==0?"AutoPagerize Rules":"Pagetual Rules";
  1216. this.item.dataset.id=this.ruleUrl.id;
  1217. let url=document.createElement("span");
  1218. url.innerHTML=ruleUrl.url;
  1219. let up=document.createElement("span");
  1220. up.innerHTML="↑ ";
  1221. up.title=i18n("sortTitle");
  1222. let down=document.createElement("span");
  1223. down.innerHTML="↓ ";
  1224. down.title=i18n("sortTitle");
  1225. let del=document.createElement("span");
  1226. del.innerHTML="× ";
  1227. up.onclick=e=>{
  1228. this.moveUp();
  1229. };
  1230. down.onclick=e=>{
  1231. this.moveDown();
  1232. };
  1233. del.onclick=e=>{
  1234. this.del();
  1235. };
  1236. this.item.appendChild(up);
  1237. this.item.appendChild(down);
  1238. this.item.appendChild(del);
  1239. this.item.appendChild(url);
  1240. configCon.insertBefore(this.item, insertPos);
  1241. }
  1242. saveSort(){
  1243. let sort=[];
  1244. [].forEach.call(this.item.parentNode.querySelectorAll("p[data-id]"), i=>{
  1245. sort.push(i.dataset.id);
  1246. });
  1247. rulesData.sort=sort;
  1248. storage.setItem("rulesData", rulesData);
  1249. }
  1250. moveUp(){
  1251. let preE=this.item.previousElementSibling;
  1252. if(preE.tagName=="P" && preE.children.length>1){
  1253. this.item.parentNode.insertBefore(this.item,preE);
  1254. this.saveSort();
  1255. }
  1256. }
  1257. moveDown(){
  1258. let nextE=this.item.nextElementSibling;
  1259. if(nextE.tagName=="P" && nextE.children.length>1){
  1260. this.item.parentNode.insertBefore(nextE,this.item);
  1261. this.saveSort();
  1262. }
  1263. }
  1264. del(){
  1265. if(this.ruleUrl.id<2){
  1266. alert(i18n("cantDel"));
  1267. }else if(window.confirm(i18n("confirmDel"))){
  1268. for(let u=0;u<rulesData.urls.length;u++){
  1269. if(this.ruleUrl.id==rulesData.urls[u].id){
  1270. rulesData.urls.splice(u,1);
  1271. break;
  1272. }
  1273. }
  1274. for(let u=0;u<rulesData.sort.length;u++){
  1275. if(this.ruleUrl.id==rulesData.sort[u]){
  1276. rulesData.sort.splice(u,1);
  1277. break;
  1278. }
  1279. }
  1280. storage.setItem("rulesData", rulesData);
  1281. ruleParser.rules=ruleParser.rules.filter(item=>{return item.from!=this.ruleUrl.id});
  1282. storage.setItem("rules", ruleParser.rules);
  1283. this.item.parentNode.removeChild(this.item);
  1284. //location.reload();
  1285. }
  1286. }
  1287. }
  1288. let updateP=document.createElement("p"),i=0;
  1289. let now=new Date().getTime(),inUpdate=false;
  1290.  
  1291.  
  1292. let pastDate=(new Date(updateDate)).toString(),passStr;
  1293. let passTime=(now-updateDate)/1000;
  1294. if(isNaN(passTime)){
  1295. passStr=i18n("firstUpdate");
  1296. }else if(passTime<60){
  1297. passStr=i18n("passSec", parseInt(passTime))+" ← "+i18n("click2update");
  1298. }else if(passTime<60*60){
  1299. passStr=i18n("passMin", parseInt(passTime/60))+" ← "+i18n("click2update");
  1300. }else if(passTime<60*60*24){
  1301. passStr=i18n("passHour", parseInt(passTime/3600))+" ← "+i18n("click2update");
  1302. }else{
  1303. passStr=i18n("passDay", parseInt(passTime/86400))+" ← "+i18n("click2update");
  1304. }
  1305.  
  1306.  
  1307. updateP.className="updateDate";
  1308. updateP.innerHTML=passStr;
  1309. updateP.title=i18n("update")+" - "+pastDate;
  1310. updateP.onclick=e=>{
  1311. updateRules(()=>{
  1312. alert(i18n("updateSucc"));
  1313. updateP.innerHTML=i18n("passSec", 0);
  1314. updateP.title=i18n("update");
  1315. },(rule,err)=>{
  1316. alert("Update "+rule.url+" rules fail! "+err);
  1317. });
  1318. alert(i18n("beginUpdate"));
  1319. };
  1320. configCon.insertBefore(updateP, insertPos);
  1321. if(ruleUrls){
  1322. ruleUrls.forEach(ruleUrl=>{
  1323. var rulebar=new Rulebar();
  1324. rulebar.init(ruleUrl);
  1325. });
  1326. }
  1327. let customUrlsTitle=document.createElement("h2");
  1328. customUrlsTitle.innerHTML=i18n("customUrls");
  1329. configCon.insertBefore(customUrlsTitle, insertPos);
  1330. let customUrlsInput=document.createElement("textarea");
  1331. customUrlsInput.style.width="100%";
  1332. customUrlsInput.placeholder="0|http://wedata.net/databases/AutoPagerize/items_all.json";
  1333. configCon.insertBefore(customUrlsInput, insertPos);
  1334.  
  1335. let opacityTitle=document.createElement("h2");
  1336. opacityTitle.innerHTML=i18n("opacity");
  1337. let opacityInput=document.createElement("input");
  1338. opacityInput.value=rulesData.opacity*100;
  1339. opacityInput.type="number";
  1340. opacityInput.style.width="50px";
  1341. opacityInput.style.margin="0 0 0 10px";
  1342. opacityTitle.appendChild(opacityInput);
  1343. configCon.insertBefore(opacityTitle, insertPos);
  1344.  
  1345.  
  1346. let hideBarTitle=document.createElement("h2");
  1347. hideBarTitle.innerHTML=i18n("hideBar");
  1348. let hideBarInput=document.createElement("input");
  1349. hideBarInput.type="checkbox";
  1350. hideBarInput.style.width="50px";
  1351. hideBarInput.style.height="20px";
  1352. hideBarInput.checked=rulesData.hideBar;
  1353. hideBarTitle.appendChild(hideBarInput);
  1354. configCon.insertBefore(hideBarTitle, insertPos);
  1355.  
  1356. let dbClick2StopTitle=document.createElement("h2");
  1357. dbClick2StopTitle.innerHTML=i18n("dbClick2Stop");
  1358. let dbClick2StopInput=document.createElement("input");
  1359. dbClick2StopInput.type="checkbox";
  1360. dbClick2StopInput.style.width="50px";
  1361. dbClick2StopInput.style.height="20px";
  1362. dbClick2StopInput.checked=rulesData.dbClick2Stop;
  1363. dbClick2StopTitle.appendChild(dbClick2StopInput);
  1364. configCon.insertBefore(dbClick2StopTitle, insertPos);
  1365.  
  1366. let enableWhiteListTitle=document.createElement("h2");
  1367. enableWhiteListTitle.innerHTML=i18n("autoRun");
  1368. let enableWhiteListInput=document.createElement("input");
  1369. enableWhiteListInput.type="checkbox";
  1370. enableWhiteListInput.style.width="50px";
  1371. enableWhiteListInput.style.height="20px";
  1372. enableWhiteListInput.checked=rulesData.enableWhiteList!=true;
  1373. enableWhiteListTitle.appendChild(enableWhiteListInput);
  1374. configCon.insertBefore(enableWhiteListTitle, insertPos);
  1375.  
  1376. let customRulesTitle=document.createElement("h2");
  1377. customRulesTitle.innerHTML=i18n("customRules")
  1378. configCon.insertBefore(customRulesTitle, insertPos);
  1379. let customRulesInput=document.createElement("textarea");
  1380. customRulesInput.style.width="100%";
  1381. customRulesInput.style.height="500px";
  1382. 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]`;
  1383. customRulesInput.value=getFormatJSON(ruleParser.customRules);
  1384. configCon.insertBefore(customRulesInput, insertPos);
  1385. let saveBtn=document.createElement("button");
  1386. saveBtn.innerHTML=i18n("save");
  1387. saveBtn.style.width="100%";
  1388. configCon.insertBefore(saveBtn, insertPos);
  1389. saveBtn.onclick=e=>{
  1390. try{
  1391. storage.setItem("hpRules", []);
  1392. if(customRulesInput.value==""){
  1393. storage.setItem("customRules", "");
  1394. }else{
  1395. let customRules=JSON.parse(customRulesInput.value);
  1396. if(Array && Array.isArray && !Array.isArray(customRules)){
  1397. alert("Rules must be a Array!");
  1398. return;
  1399. }
  1400. debug(customRules);
  1401. storage.setItem("customRules", customRules);
  1402. }
  1403. }catch(e){
  1404. debug(e);
  1405. alert("JSON error, check again!");
  1406. return;
  1407. }
  1408. rulesData.opacity=opacityInput.value/100;
  1409. rulesData.hideBar=hideBarInput.checked;
  1410. rulesData.dbClick2Stop=dbClick2StopInput.checked;
  1411. rulesData.enableWhiteList=!enableWhiteListInput.checked;
  1412. storage.setItem("rulesData", rulesData);
  1413. let customUrls=customUrlsInput.value.trim();
  1414. if(customUrls){
  1415. customUrls=customUrls.split(/\n/);
  1416. for(let c=0;c<customUrls.length;c++){
  1417. let urlArr=customUrls[c].split("|"),url,type=1;
  1418. if(urlArr.length==1){
  1419. url=urlArr[0].trim();
  1420. if(!/^http/.test(url)){
  1421. alert("Wrong url, check again!");
  1422. return;
  1423. }
  1424. }else if(urlArr.length==2){
  1425. type=urlArr[0].trim();
  1426. url=urlArr[1].trim();
  1427. if(!/^http/.test(url)){
  1428. alert("Wrong url, check again!");
  1429. return;
  1430. }
  1431. }else{
  1432. break;
  1433. }
  1434. let maxId=0,hasUrl=false;
  1435. if(!rulesData.urls){
  1436. rulesData.urls=[];
  1437. maxId=1;
  1438. }else{
  1439. rulesData.urls.forEach(u=>{
  1440. if(maxId<u.id){
  1441. maxId=u.id;
  1442. }
  1443. if(u.url==url){
  1444. hasUrl=true;
  1445. }
  1446. });
  1447. if(hasUrl)break;
  1448. }
  1449. rulesData.urls.push({id:maxId+1,url:url,type:type});
  1450. rulesData.sort.push(maxId+1);
  1451. storage.setItem("rulesData", rulesData);
  1452. }
  1453. }
  1454. alert("Modified successfully");
  1455. location.reload();
  1456. };
  1457. return true;
  1458. }
  1459.  
  1460. var inUpdate=false;
  1461. function updateRules(success,fail){
  1462. if(inUpdate)return;
  1463. inUpdate=true;
  1464. let ruleIndex=0;
  1465. storage.setItem("hpRules", []);
  1466. function addNextRule(){
  1467. if(ruleIndex>=ruleUrls.length){
  1468. let now=new Date().getTime();
  1469. storage.setItem("ruleLastUpdate", now);
  1470. storage.setItem("rules", ruleParser.rules);
  1471. inUpdate=false;
  1472. success();
  1473. }else{
  1474. let rule=ruleUrls[ruleIndex++];
  1475. ruleParser.addRuleByUrl(rule.url, rule.type, rule.id, (json,err)=>{
  1476. if(!json){
  1477. fail(rule,err);
  1478. }
  1479. storage.setItem("rules", ruleParser.rules);
  1480. addNextRule();
  1481. })
  1482. }
  1483. }
  1484. addNextRule();
  1485. }
  1486.  
  1487. function objIsArr(obj) {
  1488. return obj &&
  1489. typeof obj === 'object' &&
  1490. typeof obj.length === 'number' &&
  1491. !(obj.propertyIsEnumerable('length'));
  1492. }
  1493.  
  1494. function isVisible(el, win) {
  1495. if(!el.offsetParent)return false;
  1496. var loopable = true,
  1497. visible = el.tagName && win.getComputedStyle(el).display != 'none' && win.getComputedStyle(el).visibility != 'hidden';
  1498. while(loopable && visible) {
  1499. el = el.parentNode;
  1500.  
  1501. if(el && el.tagName && el.tagName!="BODY") {
  1502. visible = win.getComputedStyle(el).display != 'none' && win.getComputedStyle(el).visibility != 'hidden';
  1503. }else {
  1504. loopable = false;
  1505. }
  1506. }
  1507. return visible;
  1508. }
  1509.  
  1510. function getFormatJSON(obj){
  1511. if(!objIsArr(obj))return "";
  1512. let ret="[\n";
  1513. let len=obj.length,i=0,isLast;
  1514. obj.forEach(item=>{
  1515. ret+=" {\n";
  1516. let iLen=Object.keys(item).length,j=0;
  1517. for(let key in item){
  1518. isLast=(++j)==iLen;
  1519. let value=item[key];
  1520. if(objIsArr(value)){
  1521. let vstr="[",v=0,vIsLast=false;
  1522. value.forEach(vi=>{
  1523. vIsLast=(++v)==value.length;
  1524. vstr+="\""+vi.replace(/\\/g,"\\\\")+"\""+(vIsLast?"":",");
  1525. });
  1526. vstr+="]";
  1527. ret+=" \""+key+"\":"+vstr+""+(isLast?"":",")+"\n";
  1528. }else{
  1529. if(typeof value=="string"){
  1530. value=value.replace(/\\/g,"\\\\").replace(/"/g,"\\\"");
  1531. value="\""+value+"\"";
  1532. }
  1533. ret+=" \""+key+"\":"+value+""+(isLast?"":",")+"\n";
  1534. }
  1535. }
  1536. isLast=(++i)==len;
  1537. ret+=" }"+(isLast?"":",")+"\n";
  1538. });
  1539. ret+="]";
  1540. return ret;
  1541. }
  1542.  
  1543. function initRules(callback) {
  1544. /*0 wedata格式,1 pagetual格式*/
  1545. ruleUrls=[
  1546. {
  1547. id:0,
  1548. url:'https://raw.githubusercontent.com/hoothin/UserScripts/master/Pagetual/pagetualRules.json',
  1549. type:1
  1550. },
  1551. {
  1552. id:1,
  1553. url:'http://wedata.net/databases/AutoPagerize/items_all.json',
  1554. type:0,
  1555. }
  1556. ];var i=0,j=0;
  1557.  
  1558. ruleParser.initSavedRules(()=>{
  1559. storage.getItem("rulesData", data=>{
  1560. if(data){
  1561. rulesData=data;
  1562. if(data.urls)ruleUrls=ruleUrls.concat(data.urls);
  1563. if(data.sort){
  1564. let urls=[];
  1565. data.sort.forEach(id=>{
  1566. for(let s=0;s<ruleUrls.length;s++){
  1567. if(id==ruleUrls[s].id){
  1568. urls.push(ruleUrls[s]);
  1569. break;
  1570. }
  1571. }
  1572. });
  1573. ruleUrls=urls;
  1574. }
  1575. }
  1576. if(typeof(rulesData.opacity)=="undefined"){
  1577. rulesData.opacity=0.3;
  1578. }
  1579. if(typeof(rulesData.hideBar)=="undefined"){
  1580. rulesData.hideBar=true;
  1581. }
  1582. if(typeof(rulesData.dbClick2Stop)=="undefined"){
  1583. rulesData.dbClick2Stop=true;
  1584. }
  1585. if(typeof(rulesData.enableWhiteList)=="undefined"){
  1586. rulesData.enableWhiteList=false;
  1587. }
  1588. storage.getItem("forceState_"+location.host, v=>{
  1589. storage.getItem("ruleLastUpdate", date=>{
  1590. if(typeof(v)=="undefined"){
  1591. v=(rulesData.enableWhiteList?1:0);
  1592. }
  1593. forceState=v;
  1594. updateDate=date;
  1595. if(initConfig())return;
  1596. if(forceState==1)return;
  1597. let now=new Date().getTime();
  1598. if(!date || now-date>2*24*60*60*1000){
  1599. storage.setItem("ruleLastUpdate", now);
  1600. updateRules(()=>{
  1601. callback();
  1602. },(rule,err)=>{});
  1603. }else{
  1604. callback();
  1605. }
  1606. });
  1607. });
  1608. });
  1609. });
  1610. }
  1611.  
  1612. function requestDoc(url, callback){
  1613. _GM_xmlhttpRequest({
  1614. url: url,
  1615. method: 'GET',
  1616. overrideMimeType: 'text/html;charset='+document.charset,
  1617. headers: {
  1618. 'Referer': location.href
  1619. },
  1620. timeout: 5000,
  1621. onload: function(res) {
  1622. var doc=null;
  1623. try {
  1624. doc=document.implementation.createHTMLDocument('');
  1625. doc.documentElement.innerHTML=res.response;
  1626. }
  1627. catch (e) {
  1628. debug('parse error'+e.toString());
  1629. }
  1630. let pageElement=ruleParser.getPageElement(doc);
  1631. //只有1的話怕不是圖片哦
  1632. if(pageElement && (pageElement.length>1 || (pageElement.length==1 && pageElement[0].tagName!="IMG") )){
  1633. let result=ruleParser.insertPage(doc, pageElement, url, callback, false);
  1634. if(!result){
  1635. requestFromIframe(url, (doc, eles)=>{
  1636. loadPageOver();
  1637. if(eles){
  1638. ruleParser.insertPage(doc, eles, url, callback, true);
  1639. }
  1640. });
  1641. }
  1642. }else if(ruleParser.curSiteRule.singleUrl){
  1643. requestFromIframe(url, (doc, eles)=>{
  1644. loadPageOver();
  1645. if(eles){
  1646. ruleParser.insertPage(doc, eles, url, callback, true);
  1647. }
  1648. });
  1649. }
  1650. },
  1651. onerror: function(e){
  1652. debug(e);
  1653. callback(false);
  1654. },
  1655. ontimeout: function(e){
  1656. debug(e);
  1657. callback(false);
  1658. }
  1659. });
  1660. }
  1661.  
  1662. var failFromIframe=0;
  1663. function requestFromIframe(url, callback){
  1664. let orgPage,curPage;
  1665. let iframe = document.createElement('iframe');
  1666. iframe.name = 'pagetual-iframe';
  1667. iframe.width = '100%';
  1668. iframe.height = '0';
  1669. iframe.frameBorder = '0';
  1670. if(ruleParser.curSiteRule.sandbox!=false){
  1671. iframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  1672. }
  1673. iframe.style.cssText = 'margin:0!important;padding:0!important;visibility:hidden!important;';
  1674. iframe.addEventListener("load", e=>{
  1675. let waitTime=500,checkEval;
  1676. if(ruleParser.curSiteRule.wait){
  1677. if(isNaN(ruleParser.curSiteRule.wait)){
  1678. try{
  1679. checkEval=Function("doc",'"use strict";' + ruleParser.curSiteRule.wait);
  1680. }catch(e){
  1681. debug(e);
  1682. }
  1683. }else{
  1684. waitTime=ruleParser.curSiteRule.wait;
  1685. }
  1686. }
  1687. setTimeout(()=>{
  1688. let tryTimes=0;
  1689. function checkIframe(){
  1690. try{
  1691. let doc=iframe.contentDocument || iframe.contentWindow.document;
  1692. let eles=ruleParser.getPageElement(doc, iframe.contentWindow);
  1693. if(eles && eles.length>0 && (!checkEval || checkEval(doc))){
  1694. callback(doc, eles);
  1695. }else if(tryTimes++ < 10){
  1696. setTimeout(()=>{
  1697. checkIframe();
  1698. },waitTime);
  1699. return;
  1700. }else{
  1701. if(failFromIframe++ > 2){
  1702. failFromIframe=0;
  1703. debug("Stop as failFromIframe");
  1704. isPause=true;
  1705. callback(false, false);
  1706. }else{
  1707. callback(false, false);
  1708. }
  1709. }
  1710. }catch(e){
  1711. debug("Stop as cors");
  1712. //isPause=true;
  1713. ruleParser.curSiteRule.pageElement=allOfBody;
  1714. ruleParser.getInsert(true);
  1715. callback(false, false);
  1716. }
  1717. document.body.removeChild(iframe);
  1718. }
  1719. checkIframe();
  1720. },waitTime);
  1721. });
  1722. iframe.src=url;
  1723. document.body.appendChild(iframe);
  1724. }
  1725.  
  1726. function initPage(){
  1727. ruleParser.initPage(()=>{
  1728. if(ruleParser.nextLinkHref){
  1729. initView();
  1730. let isJs=/^(javascript|#)/.test(ruleParser.nextLinkHref.replace(location.href,""));
  1731. if(!isJs){
  1732. _GM_registerMenuCommand(i18n(forceState==2?"cancelForceIframe":"forceIframe"), ()=>{
  1733. storage.setItem("forceState_"+location.host, (forceState==2?"":2));
  1734. location.reload();
  1735. });
  1736. }
  1737. _GM_registerMenuCommand(i18n("loadNow"), ()=>{
  1738. let pageNum=window.prompt(i18n("loadConfirm"), "1");
  1739. if(pageNum==="" || pageNum===null)return;
  1740. pageNum=Math.abs(parseInt(pageNum));
  1741. autoLoadNum=pageNum;
  1742. nextPage();
  1743. });
  1744. }
  1745. initListener();
  1746. nextPage();
  1747. });
  1748. }
  1749.  
  1750. function initView(){
  1751. _GM_addStyle(`
  1752. .pagetual_pageBar.stop {
  1753. -webkit-filter: invert(100%);
  1754. filter: invert(100%);
  1755. opacity: 1!important;
  1756. }
  1757. .pagetual_pageBar.hide {
  1758. display: none!important;
  1759. }
  1760. .pagetual_pageBar:hover {
  1761. opacity: 1!important;
  1762. }
  1763. .pagetual_pageBar span {
  1764. vertical-align: super;
  1765. }
  1766.  
  1767. .pagetual_pageBar span>svg:hover {
  1768. animation: touhouAni 1s infinite;
  1769. }
  1770. .pagetual_pageBar .pagetual_pageNum:hover{
  1771. color: #ff6464;
  1772. }
  1773.  
  1774. @keyframes touhouAni{
  1775. from {transform: rotate(0deg) scale3d(1.2, 1.2, 1.2);}
  1776. to {transform: rotate(360deg);}
  1777. }
  1778. @-webkit-keyframes touhouAni{
  1779. from {transform: rotate(0deg) scale3d(1.2, 1.2, 1.2);}
  1780. to {transform: rotate(360deg);}
  1781. }
  1782. `);
  1783. 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: 5px auto; text-align: center;`;
  1784. }
  1785. var loadingDiv=document.createElement("div");
  1786. loadingDiv.className="pagetual_loading";
  1787. loadingDiv.style.cssText="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;";
  1788. loadingDiv.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>`;
  1789.  
  1790. var upSvg=`<svg width="30" height="30" 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>`;
  1791. var downSvg=`<svg width="30" height="30" 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>`;
  1792. var pageBarStyle;
  1793. var initStyle=`display: initial;right: unset;left: unset;top: unset;bottom: unset;inset: unset;clear: both;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;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;`;
  1794. 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);`;
  1795.  
  1796. var isPause=false,isLoading=false,curPage=1,forceState=0,bottomGap=1000,autoLoadNum=-1;
  1797.  
  1798. function changeStop(stop, hide){
  1799. isPause=stop;
  1800. [].forEach.call(document.querySelectorAll(".pagetual_pageBar"), bar=>{
  1801. if(isPause){
  1802. bar.classList.add("stop");
  1803. if(hide)bar.classList.add("hide");
  1804. }else{
  1805. bar.classList.remove("stop");
  1806. if(hide)bar.classList.remove("hide");
  1807. }
  1808. });
  1809. }
  1810.  
  1811. function isInViewPort(element) {
  1812. if(!element.parentNode)return false;
  1813. if(_unsafeWindow.getComputedStyle(element).display=="none")return false;
  1814. const viewWidth = window.innerWidth || document.documentElement.clientWidth;
  1815. const viewHeight = window.innerHeight || document.documentElement.clientHeight;
  1816. const {
  1817. top,
  1818. right,
  1819. bottom,
  1820. left,
  1821. } = element.getBoundingClientRect();
  1822.  
  1823. return (
  1824. top >= 0 &&
  1825. left >= 0 &&
  1826. right <= viewWidth &&
  1827. bottom <= viewHeight
  1828. );
  1829. }
  1830.  
  1831. var urlChanged=false;
  1832. var _wr = function(type) {
  1833. var orig = history[type];
  1834. return function() {
  1835. var rv = orig.apply(this, arguments);
  1836. var e = new Event(type);
  1837. e.arguments = arguments;
  1838. window.dispatchEvent(e);
  1839. return rv;
  1840. };
  1841. };
  1842. history.pushState = _wr('pushState');
  1843. window.addEventListener('pushState', function(e) {
  1844. urlChanged=true;
  1845. isPause=true;
  1846. setTimeout(()=>{
  1847. if(location.href==configPage){
  1848. location.reload();
  1849. }
  1850. },1);
  1851. });
  1852.  
  1853. function initListener(){
  1854. let loadmoreBtn,loading=true;
  1855. setTimeout(()=>{
  1856. loadmoreBtn=getLoadMore(document);
  1857. if(loadmoreBtn && isVisible(loadmoreBtn, _unsafeWindow)){
  1858. loading=false;
  1859. }
  1860. },500);
  1861. let checkScrollReach=()=>{
  1862. let scrolly=window.scrollY;
  1863. let windowHeight=window.innerHeight || document.documentElement.clientHeight;
  1864. let scrollH=Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
  1865. if(scrollH-scrolly-windowHeight<bottomGap){
  1866. nextPage();
  1867. }
  1868. };
  1869. document.addEventListener('scroll', e=>{
  1870. if(urlChanged){
  1871. ruleParser.initPage(()=>{
  1872. if(ruleParser.nextLinkHref)initView();
  1873. });
  1874. urlChanged=false;
  1875. isPause=false;
  1876. }
  1877. if(isPause)return;
  1878. if(!loading){
  1879. loading=true;
  1880. setTimeout(()=>{loading=false},200);
  1881. if(!loadmoreBtn || !loadmoreBtn.parentNode){
  1882. loadmoreBtn=getLoadMore(document);
  1883. }
  1884. if(loadmoreBtn){
  1885. if(isInViewPort(loadmoreBtn)){
  1886. emuClick(loadmoreBtn);
  1887. }
  1888. }
  1889. }
  1890. setTimeout(()=>{
  1891. if(!isLoading){
  1892. checkScrollReach();
  1893. }
  1894. },100);
  1895. }, true);
  1896. document.addEventListener('dblclick', e=>{
  1897. if(rulesData.dbClick2Stop){
  1898. setTimeout(()=>{
  1899. changeStop(!isPause, rulesData.hideBar);
  1900. if(!isPause){
  1901. checkScrollReach();
  1902. }
  1903. },200);
  1904. }
  1905. });
  1906. }
  1907.  
  1908. function getLoadMore(doc){
  1909. if(ruleParser.curSiteRule.loadMore==="")return null;
  1910. let btnSel=ruleParser.curSiteRule.loadMore||".LoadMore,.load-more";
  1911. let loadmoreBtn=doc.querySelector(btnSel);
  1912. if(!loadmoreBtn){
  1913. let buttons=doc.querySelectorAll("input,button,a,div[onclick]"),loadmoreReg=/^\s*(加载更多|加載更多|load\s*more|もっと読み込む)\s*$/i;
  1914. for(let i=0;i<buttons.length;i++){
  1915. let button=buttons[i];
  1916. if(button && loadmoreReg.test(button.innerText)){
  1917. loadmoreBtn=button;
  1918. break;
  1919. }
  1920. }
  1921. }
  1922. return loadmoreBtn;
  1923. }
  1924.  
  1925. var hasPageNum=true;
  1926. function createPageBar(url){
  1927. let insert=ruleParser.getInsert();
  1928. if(!insert || !insert.parentNode)return;
  1929. curPage++;
  1930. let example=ruleParser.curSiteRule.insertPos==2?insert.children[0]:insert;
  1931. let inTable=example.parentNode.tagName=="TABLE" ||
  1932. example.tagName=="TR" ||
  1933. example.tagName=="TBODY" ||
  1934. (example.previousElementSibling && example.previousElementSibling.tagName=="TR") ||
  1935. (example.previousElementSibling && example.previousElementSibling.tagName=="TBODY");
  1936. let inLi=example.tagName=="LI" || (example.previousElementSibling && example.previousElementSibling.tagName=="LI");
  1937. let pageBar=document.createElement(inTable?"tr":"div");
  1938. let upSpan=document.createElement("span");
  1939. let downSpan=document.createElement("span");
  1940. let pageText=document.createElement("a");
  1941. pageBar.className="pagetual_pageBar";
  1942. pageBar.id="pagetual_pageBar";
  1943. if(isPause){
  1944. pageBar.classList.add("stop");
  1945. }
  1946. pageBar.style.cssText=pageBarStyle;
  1947. pageBar.title=i18n(isPause?"enable":"disable");
  1948. upSpan.innerHTML=upSvg;
  1949. upSpan.title=i18n("toTop");
  1950. downSpan.innerHTML=downSvg;
  1951. downSpan.title=i18n("toBottom");
  1952. upSpan.style.cssText=initStyle;
  1953. downSpan.style.cssText=initStyle;
  1954. pageText.href=url;
  1955. pageText.style.cssText=pageTextStyle;
  1956. pageText.title=i18n("current");
  1957. pageBar.appendChild(upSpan);
  1958. pageBar.appendChild(pageText);
  1959. if(ruleParser.curSiteRule.pageNum || (hasPageNum && /[&\/\?](p=|page[=\/])\d+/.test(url))){
  1960. pageText.innerHTML="Page ";
  1961. let pageNum=document.createElement("span");
  1962. pageNum.innerText=curPage;
  1963. pageNum.className="pagetual_pageNum";
  1964. pageNum.title=i18n("inputPageNum");
  1965. pageNum.style.cssText=pageTextStyle;
  1966. pageNum.style.cursor="pointer";
  1967. pageNum.style.color="";
  1968. pageNum.addEventListener("click", e=>{
  1969. let pageInput=prompt(i18n("inputPageNum"));
  1970. if(pageInput){
  1971. let pageLink=ruleParser.getLinkByPage(url, pageInput);
  1972. if(pageLink){
  1973. _GM_openInTab(pageLink,{active:true});
  1974. }
  1975. }
  1976. e.preventDefault();
  1977. e.stopPropagation();
  1978. });
  1979. pageBar.appendChild(pageNum);
  1980. }else{
  1981. pageText.innerHTML="Page "+curPage;
  1982. hasPageNum=false;
  1983. }
  1984. pageBar.appendChild(downSpan);
  1985. if(inTable){
  1986. example=(insert.tagName=="TR" || insert.tagName=="TBODY")?insert:insert.previousElementSibling;
  1987. if(example.previousElementSibling)example=example.previousElementSibling;
  1988. let tdNum=0;
  1989. [].forEach.call(example.querySelectorAll("td,th"), td=>{
  1990. tdNum+=td.colSpan||1;
  1991. });
  1992. pageBar.style.display="table-row";
  1993. pageBar.style.backgroundColor="unset";
  1994. let td=document.createElement("td");
  1995. td.style.backgroundColor="rgb(240 240 240 / 80%)";
  1996. td.style.borderRadius="20px";
  1997. td.colSpan=tdNum||1;
  1998. td.style.padding="0 0";
  1999. td.style.textAlign="center";
  2000. td.appendChild(upSpan);
  2001. td.appendChild(pageText);
  2002. td.appendChild(downSpan);
  2003. pageBar.appendChild(td);
  2004. }
  2005.  
  2006. upSpan.addEventListener("click", e=>{
  2007. //changeStop(true);
  2008. //pageBar.title=i18n(isPause?"enable":"disable");
  2009. document.body.scrollTop=0;
  2010. document.documentElement.scrollTop=0;
  2011. e.preventDefault();
  2012. e.stopPropagation();
  2013. });
  2014. downSpan.addEventListener("click", e=>{
  2015. changeStop(true);
  2016. pageBar.title=i18n(isPause?"enable":"disable");
  2017. document.body.scrollTop=9999999;
  2018. document.documentElement.scrollTop=9999999;
  2019. e.preventDefault();
  2020. e.stopPropagation();
  2021. });
  2022. pageBar.addEventListener("click", e=>{
  2023. changeStop(!isPause);
  2024. pageBar.title=i18n(isPause?"enable":"disable");
  2025. });
  2026. pageText.addEventListener("click", e=>{
  2027. e.stopPropagation();
  2028. });
  2029. let parentStyle=_unsafeWindow.getComputedStyle(insert.parentNode);
  2030. pageBar.style.width=parseInt(parentStyle.width)*.99-parseInt(parentStyle.paddingLeft)-parseInt(parentStyle.paddingRight)+"px";
  2031. if(inLi){
  2032. pageBar.style.width="90%";
  2033. pageBar.style.minWidth="150px";
  2034. pageBar.style.display="inline-block";
  2035. let line=document.createElement("li");
  2036. line.style.textAlign="center";
  2037. line.className=example.tagName=="LI"?example.className:example.previousElementSibling.className;
  2038. line.appendChild(pageBar);
  2039. if(ruleParser.curSiteRule.insertPos==2){
  2040. insert.appendChild(line);
  2041. }else{
  2042. insert.parentNode.insertBefore(line, insert);
  2043. }
  2044. }else{
  2045. if(ruleParser.curSiteRule.insertPos==2){
  2046. insert.appendChild(pageBar);
  2047. }else{
  2048. insert.parentNode.insertBefore(pageBar, insert);
  2049. }
  2050. }
  2051. if(ruleParser.curSiteRule.pageBar){
  2052. try{
  2053. Function("pageBar",'"use strict";' + ruleParser.curSiteRule.pageBar)(pageBar);
  2054. }catch(e){
  2055. debug(e);
  2056. }
  2057. }
  2058.  
  2059. let scrollH=Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
  2060. let actualTop = pageBar.offsetTop;
  2061. let current = pageBar.parentNode.offsetParent;
  2062. while (current !== null){
  2063. actualTop += current.offsetTop;
  2064. current = current.offsetParent;
  2065. }
  2066. bottomGap=scrollH-actualTop+(window.innerHeight||document.documentElement.clientHeight)*(ruleParser.curSiteRule.rate||1);
  2067. return pageBar;
  2068. }
  2069.  
  2070. function emuClick(btn){
  2071. if(!PointerEvent)return btn.click();
  2072. let eventParam={
  2073. isTrusted: true,
  2074. altKey: false,
  2075. azimuthAngle: 0,
  2076. bubbles: true,
  2077. button: 0,
  2078. buttons: 0,
  2079. clientX: 1,
  2080. clientY: 1,
  2081. cancelBubble: false,
  2082. cancelable: true,
  2083. composed: true,
  2084. ctrlKey: false,
  2085. defaultPrevented: false,
  2086. detail: 1,
  2087. eventPhase: 2,
  2088. fromElement: null,
  2089. height: 1,
  2090. isPrimary: false,
  2091. metaKey: false,
  2092. pointerId: 1,
  2093. pointerType: "mouse",
  2094. pressure: 0,
  2095. relatedTarget: null,
  2096. returnValue: true,
  2097. shiftKey: false,
  2098. toElement: null,
  2099. twist: 0,
  2100. type: "click",
  2101. which: 1
  2102. };
  2103. var mouseclick = new PointerEvent("click",eventParam);
  2104. btn.dispatchEvent(mouseclick);
  2105. }
  2106.  
  2107. var emuIframe;
  2108. function emuPage(callback){
  2109. let orgPage,curPage,iframeDoc,times=0,loadmoreBtn,loadmoreEnd=false,waitTimes=10;
  2110. function checkPage(){
  2111. if(isPause)return;
  2112. try{
  2113. iframeDoc=emuIframe.contentDocument || emuIframe.contentWindow.document;
  2114. }catch(e){
  2115. debug("Stop as cors");
  2116. isPause=true;
  2117. callback(false, false);
  2118. return;
  2119. }
  2120.  
  2121. let nextLink=ruleParser.getNextLink(iframeDoc);
  2122. let waitTime=500,checkEval;
  2123. if(ruleParser.curSiteRule.wait){
  2124. if(isNaN(ruleParser.curSiteRule.wait)){
  2125. try{
  2126. checkEval=Function("doc",'"use strict";' + ruleParser.curSiteRule.wait);
  2127. }catch(e){
  2128. debug(e);
  2129. }
  2130. }else{
  2131. waitTime=ruleParser.curSiteRule.wait;
  2132. }
  2133. }
  2134.  
  2135. if(!orgPage){
  2136. if(!loadmoreEnd){
  2137. loadmoreBtn=getLoadMore(iframeDoc);
  2138. if(loadmoreBtn && isVisible(loadmoreBtn, iframeDoc.defaultView)){
  2139. emuClick(loadmoreBtn);
  2140. let intv=setInterval(()=>{
  2141. loadmoreBtn=getLoadMore(iframeDoc);
  2142. if(!loadmoreBtn || !loadmoreBtn.parentNode || !isVisible(loadmoreBtn, iframeDoc.defaultView)){
  2143. clearInterval(intv);
  2144. loadmoreEnd=true;
  2145. setTimeout(()=>{
  2146. checkPage();
  2147. },500);
  2148. }else{
  2149. emuClick(loadmoreBtn);
  2150. }
  2151. },200);
  2152. return;
  2153. }else{
  2154. loadmoreEnd=true;
  2155. }
  2156. }
  2157. let pageEle=ruleParser.getPageElement(iframeDoc, iframeDoc.defaultView, true);
  2158. if(!nextLink || !pageEle || (checkEval && !checkEval(iframeDoc))){
  2159. if(ruleParser.curSiteRule.wait && waitTimes-->0){
  2160. setTimeout(()=>{
  2161. checkPage();
  2162. },waitTime);
  2163. return;
  2164. }
  2165. }
  2166. orgPage=pageEle;
  2167. if(!orgPage || orgPage.length==0){
  2168. debug("Stop as no page when emu");
  2169. isPause=true;
  2170. callback(false, false);
  2171. return;
  2172. }
  2173. if(orgPage && orgPage[0].tagName=="UL")orgPage=orgPage[0].children;
  2174. if(orgPage && nextLink){
  2175. orgPage=orgPage[parseInt(orgPage.length/2)];
  2176. if(!isVisible(nextLink, iframeDoc.defaultView)){
  2177. debug("Stop as next hide when emu");
  2178. isPause=true;
  2179. callback(false, false);
  2180. }else{
  2181. emuClick(nextLink);
  2182. setTimeout(()=>{
  2183. checkPage();
  2184. },500);
  2185. }
  2186. }else{
  2187. debug("Stop as no next when emu");
  2188. isPause=true;
  2189. callback(false, false);
  2190. }
  2191. return;
  2192. }
  2193. if(times++ > 20){
  2194. //emuClick(nextLink);
  2195. debug("Stop as timeout when emu");
  2196. isPause=true;
  2197. callback(false, false);
  2198. return;
  2199. }
  2200. let eles=ruleParser.getPageElement(iframeDoc, iframeDoc.defaultView, true),checkItem=eles;
  2201. if(eles && eles.length>0 && eles[0].tagName=="UL")checkItem=eles[0].children;
  2202. if(!eles || eles.length==0 || orgPage == checkItem[parseInt(checkItem.length/2)] || (checkEval && !checkEval(iframeDoc))){
  2203. setTimeout(()=>{
  2204. checkPage();
  2205. },waitTime);
  2206. }else{
  2207. callback(iframeDoc, eles);
  2208. }
  2209. }
  2210. if(!emuIframe){
  2211. emuIframe = document.createElement('iframe');
  2212. emuIframe.name = 'pagetual-iframe';
  2213. if(ruleParser.curSiteRule.sandbox!=false){
  2214. emuIframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  2215. }
  2216. emuIframe.width = '100%';
  2217. emuIframe.height = '0';
  2218. emuIframe.frameBorder = '0';
  2219. emuIframe.style.cssText = 'margin:0!important;padding:0!important;visibility:hidden!important;';
  2220. emuIframe.addEventListener("load", e=>{
  2221. setTimeout(()=>{
  2222. try{
  2223. iframeDoc=emuIframe.contentDocument || emuIframe.contentWindow.document;
  2224. }catch(e){
  2225. debug("Stop as cors");
  2226. isPause=true;
  2227. callback(false, false);
  2228. return;
  2229. }
  2230. let code=ruleParser.curSiteRule.init;
  2231. if(code){
  2232. try{
  2233. Function('doc','"use strict";' + code)(iframeDoc);
  2234. }catch(e){
  2235. debug(e);
  2236. }
  2237. }
  2238. checkPage();
  2239. },500);
  2240. });
  2241. emuIframe.src=location.href;
  2242. document.body.appendChild(emuIframe);
  2243. }else{
  2244. checkPage();
  2245. }
  2246. }
  2247.  
  2248. function forceIframe(url, callback){
  2249. let curIframe = document.createElement('iframe'),iframeDoc;
  2250. curIframe.name = 'pagetual-iframe';
  2251. curIframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  2252. curIframe.frameBorder = '0';
  2253. curIframe.scrolling="no";
  2254. 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;';
  2255. curIframe.addEventListener("load", e=>{
  2256. try{
  2257. iframeDoc=curIframe.contentDocument || curIframe.contentWindow.document;
  2258. }catch(e){
  2259. debug("Stop as cors");
  2260. isPause=true;
  2261. callback(false, false);
  2262. return;
  2263. }
  2264. ruleParser.insertPage(iframeDoc, [], url, null, true);
  2265. callback(curIframe, true);
  2266. curIframe.style.height=iframeDoc.body.scrollHeight+"px";
  2267. curIframe.style.width=iframeDoc.body.scrollWidth+"px";
  2268. });
  2269. let inAction=false;
  2270. let forceRefresh=e=>{
  2271. if(inAction || !iframeDoc)return;
  2272. inAction=true;
  2273. setTimeout(()=>{
  2274. inAction=false;
  2275. curIframe.style.height=iframeDoc.body.scrollHeight+"px";
  2276. curIframe.style.width=iframeDoc.body.scrollWidth+"px";
  2277. if(!ruleParser.nextLinkHref){
  2278. ruleParser.getNextLink(iframeDoc);
  2279. if(ruleParser.nextLinkHref){
  2280. document.removeEventListener("scroll", forceRefresh);
  2281. }
  2282. }else{
  2283. document.removeEventListener("scroll", forceRefresh);
  2284. }
  2285. },500);
  2286. curIframe.style.height=iframeDoc.body.scrollHeight+"px";
  2287. curIframe.style.width=iframeDoc.body.scrollWidth+"px";
  2288. };
  2289. document.addEventListener("scroll", forceRefresh);
  2290. curIframe.src=url;
  2291. let insert=ruleParser.getInsert();
  2292. document.body.appendChild(curIframe);
  2293. return curIframe;
  2294. }
  2295.  
  2296. function loadPageOver(){
  2297. isLoading=false;
  2298. if(loadingDiv.parentNode){
  2299. loadingDiv.parentNode.removeChild(loadingDiv);
  2300. }
  2301. setTimeout(()=>{
  2302. let scrollH=Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
  2303. if(scrollH < (window.innerHeight || document.documentElement.clientHeight)){
  2304. nextPage();
  2305. }
  2306. },1);
  2307. }
  2308.  
  2309. function nextPage(){
  2310. if(isPause || isLoading)return;
  2311. let nextLink=ruleParser.nextLinkHref;
  2312. if(!nextLink)return;
  2313. let insert=ruleParser.getInsert();
  2314. if(insert){
  2315. let isJs=/^(javascript|#)/.test(nextLink.replace(location.href,""));
  2316. if(location.protocol=="https:" && /^http:/.test(nextLink)){
  2317. nextLink=nextLink.replace(/^http/,"https");
  2318. }
  2319. isLoading=true;
  2320. document.body.appendChild(loadingDiv);
  2321. let sleep=ruleParser.curSiteRule.sleep||0;
  2322. setTimeout(()=>{
  2323. if(ruleParser.curSiteRule.pageElementByJs){
  2324. var over=ele=>{
  2325. loadPageOver();
  2326. if(ele){
  2327. createPageBar(nextLink);
  2328. ruleParser.insertPage(null, ele, nextLink, null, true);
  2329. if(autoLoadNum>=0){
  2330. if(autoLoadNum!=0 && --autoLoadNum==0){
  2331. autoLoadNum=-1;
  2332. }else{
  2333. nextPage();
  2334. }
  2335. }
  2336. }else{
  2337. debug("Stop as no page when get by js");
  2338. isPause=true;
  2339. }
  2340. };
  2341. try{
  2342. Function("over",'"use strict";' + ruleParser.curSiteRule.pageElementByJs)(over);
  2343. }catch(e){
  2344. debug(e);
  2345. }
  2346. }else if(ruleParser.curSiteRule.action==1 && !isJs){
  2347. requestFromIframe(nextLink, (doc, eles)=>{
  2348. loadPageOver();
  2349. if(eles){
  2350. createPageBar(nextLink);
  2351. ruleParser.insertPage(doc, eles, nextLink, null, true);
  2352. if(autoLoadNum>=0){
  2353. if(autoLoadNum!=0 && --autoLoadNum==0){
  2354. autoLoadNum=-1;
  2355. }else{
  2356. nextPage();
  2357. }
  2358. }
  2359. }
  2360. });
  2361. }else if((forceState==2||ruleParser.curSiteRule.action==2) && !isJs){
  2362. forceIframe(nextLink, (iframe, eles)=>{
  2363. loadPageOver();
  2364. let pageBar=createPageBar(nextLink);
  2365. iframe.parentNode.insertBefore(pageBar, iframe);
  2366. if(autoLoadNum>=0){
  2367. if(autoLoadNum!=0 && --autoLoadNum==0){
  2368. autoLoadNum=-1;
  2369. }else{
  2370. nextPage();
  2371. }
  2372. }
  2373. });
  2374. }else{
  2375. if(!isJs){
  2376. requestDoc(nextLink, (eles)=>{
  2377. loadPageOver();
  2378. if(eles){
  2379. createPageBar(nextLink);
  2380. if(autoLoadNum>=0){
  2381. if(autoLoadNum!=0 && --autoLoadNum==0){
  2382. autoLoadNum=-1;
  2383. }else{
  2384. nextPage();
  2385. }
  2386. }
  2387. }
  2388. });
  2389. }else{
  2390. emuPage((doc, eles)=>{
  2391. loadPageOver();
  2392. if(eles){
  2393. createPageBar(nextLink);
  2394. ruleParser.insertPage(doc, eles, "", null, true);
  2395. if(autoLoadNum>=0){
  2396. if(autoLoadNum!=0 && --autoLoadNum==0){
  2397. autoLoadNum=-1;
  2398. }else{
  2399. nextPage();
  2400. }
  2401. }
  2402. }
  2403. });
  2404. }
  2405. }
  2406. },sleep);
  2407. }
  2408. }
  2409.  
  2410. function init(){
  2411. initRules(()=>{
  2412. initPage();
  2413. });
  2414. }
  2415. setTimeout(()=>{
  2416. init();
  2417. },300);
  2418. })();