东方永页机

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

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

  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.7
  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.unshift(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=="TBODY" || ele.tagName=="UL"){
  664. self.curSiteRule.pageElement=self.geneSelector(ele)+">*";
  665. debug(self.curSiteRule.pageElement);
  666. return ele.children;
  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(this.curUrl!=aTag.href && aTag.href.replace("?&","?").replace("index.php?","?").indexOf(url)!=-1 && /^[\/\?&]?[_-]?(p|page)?=?\/?2(\/|\?|&|$)/i.test(aTag.href.replace(url,"").replace(/\.s?html?$/i,""))){
  866. let curHref=aTag.getAttribute("href");
  867. let pageOne=curHref.replace(/\/2(\/|\?|&|$)/,"/1$1");
  868. if(pageOne==curHref)pageOne=null;
  869. else pageOne=curPage.querySelector(`a[href='${pageOne}']`);
  870. if(!pageOne)next4=aTag;
  871. }
  872. }
  873. }
  874. next=next1||next3||next4||next2||nextJs1||nextJs3||nextJs2;
  875. }
  876. if(!next)next=jsNext;
  877. if(next && next.classList.contains("results-more"))next=null;
  878. return {next:next,canSave:canSave};
  879. }
  880.  
  881. canonicalUri(src, base_path) {
  882. if(src.charAt(0)=="#")return location.href+src;
  883. var root_page = /^[^?#]*\//.exec(location.href)[0],
  884. root_domain = /^\w+\:\/\/\/?[^\/]+/.exec(root_page)[0],
  885. absolute_regex = /^\w+\:\/\//;
  886. src=src.replace(/\.\//,"");
  887. if (/^\/\/\/?/.test(src)){
  888. src = location.protocol + src;
  889. }
  890. else if (!absolute_regex.test(src) && src.charAt(0) != "/"){
  891. src = (base_path || "") + src;
  892. }
  893. return (absolute_regex.test(src) ? src : ((src.charAt(0) == "/" ? root_domain : root_page) + src));
  894. }
  895.  
  896. getLinkByPage(url, pageNum) {
  897. if(!url)return;
  898. if(this.curSiteRule.pageNum){
  899. let result=this.curSiteRule.pageNum;
  900. let strMatch=result.match(/\{.*?}/);
  901. if(!strMatch)return null;
  902. let urlReg=new RegExp("("+result.replace(strMatch[0], ")\\d+(")+")");
  903. let code=strMatch[0].replace(/^{/,"").replace(/}$/,"").replace(/\$p/g,pageNum);
  904. if(code==pageNum){
  905. result=url.replace(urlReg,"$1"+code+"$2");
  906. }else{
  907. try{
  908. code=Function('"use strict";return ' + code)();
  909. result=url.replace(urlReg,"$1"+code+"$2");
  910. }catch(e){
  911. debug(e);
  912. }
  913. }
  914. return result==url?url.replace(/([&\/\?](p=|page[=\/]))\d+/i, "$1"+pageNum):result;
  915. }else{
  916. return url.replace(/([&\/\?](p=|page[=\/]))\d+/i, "$1"+pageNum);
  917. }
  918. }
  919.  
  920. getNextLink(doc) {
  921. let nextLink=null,page;
  922. if(this.curSiteRule.pageElementByJs){
  923. this.nextLinkHref="#";
  924. return true;
  925. }else if(this.curSiteRule.nextLinkByJs){
  926. try{
  927. let targetUrl=Function("doc",'"use strict";' + this.curSiteRule.nextLinkByJs)(doc);
  928. if(targetUrl)nextLink={href:targetUrl};
  929. }catch(e){
  930. debug(e);
  931. }
  932. }else if(this.curSiteRule.nextLinkByUrl){
  933. let targetUrl=this.curUrl.replace(new RegExp(this.curSiteRule.nextLinkByUrl[0]), this.curSiteRule.nextLinkByUrl[1]);
  934. if(targetUrl != this.curUrl){
  935. let reps=targetUrl.match(/{.*?}/g);
  936. if(reps){
  937. reps.forEach(rep=>{
  938. let code=rep.replace("{","").replace("}","");
  939. let result=code.match(/^(\d+)\+1$/);
  940. if(result){
  941. result=parseInt(result[1])+1;
  942. }else{
  943. try{
  944. result=Function('"use strict";return ' + code)();
  945. }catch(e){
  946. debug(e);
  947. }
  948. }
  949. targetUrl=targetUrl.replace(rep, result);
  950. });
  951. }
  952. }
  953. nextLink={href:targetUrl};
  954. }else if(this.curSiteRule.nextLink){
  955. if(this.curSiteRule.nextLink!="0"){
  956. nextLink=this.curSiteRule.type==0?getElementByXpath(this.curSiteRule.nextLink,doc,doc):doc.querySelector(this.curSiteRule.nextLink);
  957. }
  958. }else{
  959. page=this.getPage(doc);
  960. nextLink=page.next;
  961. if(nextLink){
  962. if(nextLink.tagName=="INPUT" && nextLink.parentNode.tagName=="FORM"){
  963. let params=[];
  964. let form=nextLink.parentNode;
  965. [].forEach.call(form.querySelectorAll("input"), input=>{
  966. params.push(input.name+'='+input.value);
  967. });
  968. nextLink.href=form.action+'?'+params.join('&');
  969. }
  970. if(nextLink.classList.contains("noClick")){
  971. this.nextLinkHref=false;
  972. return null;
  973. }else if(doc==document){
  974. if((!nextLink.href || /^javascript:/.test(nextLink.href)) && !isVisible(nextLink, _unsafeWindow)){
  975. this.nextLinkHref=false;
  976. return null;
  977. }else{
  978. let nextLinkCs=_unsafeWindow.getComputedStyle(nextLink);
  979. if(nextLinkCs.cursor=="not-allowed"){
  980. this.nextLinkHref=false;
  981. return null;
  982. }
  983. }
  984. }
  985. }
  986. }
  987. if(nextLink){
  988. if(nextLink.href && nextLink.href==this.curUrl){
  989. nextLink=null;
  990. }else if(/^javascript:/.test(nextLink.href) && (this.curSiteRule.action==0 || this.curSiteRule.action==1)){
  991. nextLink=null;
  992. }
  993. /*if(!this.curSiteRule.nextLink && page && page.canSave){
  994. this.curSiteRule.nextLink=this.geneSelector(nextLink);
  995. this.curSiteRule.type=1;
  996. this.saveCurSiteRule();
  997. }*/
  998. }
  999. if(nextLink){
  1000. this.nextLinkHref=nextLink.href?this.canonicalUri(nextLink.href):"#";
  1001. debug(nextLink);
  1002. }else{
  1003. this.nextLinkHref=false;
  1004. }
  1005. return nextLink;
  1006. }
  1007.  
  1008. getInsert(refresh) {
  1009. if(refresh){
  1010. this.insert=null;
  1011. }
  1012. if(this.insert && !refresh){
  1013. let parent=this.insert;
  1014. while(parent && parent.nodeName!="BODY"){
  1015. parent=parent.parentNode;
  1016. }
  1017. if(parent && parent.nodeName=="BODY"){
  1018. return this.insert;
  1019. }
  1020. }
  1021. if(this.curSiteRule.insert){
  1022. this.insert=this.curSiteRule.type==0?getElementByXpath(this.curSiteRule.insert,document):document.querySelector(this.curSiteRule.insert);
  1023. }else{
  1024. let pageElement=this.getPageElement(document, _unsafeWindow);
  1025. if(pageElement && pageElement.length>0){
  1026. var pELast = pageElement[pageElement.length - 1];
  1027. this.insert = pELast.nextSibling ? pELast.nextSibling : pELast.parentNode.appendChild(document.createTextNode(' '));
  1028. }
  1029. }
  1030. return this.insert;
  1031. }
  1032.  
  1033. pageInit(doc,eles){
  1034. let code=this.curSiteRule.pageInit;
  1035. if(code){
  1036. try{
  1037. Function("doc","eles",'"use strict";' + code)(doc,eles);
  1038. }catch(e){
  1039. debug(e);
  1040. }
  1041. }
  1042. }
  1043.  
  1044. pageAction(doc,eles){
  1045. let code=this.curSiteRule.pageAction;
  1046. if(code){
  1047. try{
  1048. Function("doc","eles",'"use strict";' + code)(doc,eles);
  1049. }catch(e){
  1050. debug(e);
  1051. }
  1052. }
  1053. let setLazyImg=img=>{
  1054. let realSrc;
  1055. if(img.getAttribute("_src") && !img.src){
  1056. realSrc=img.getAttribute("_src");
  1057. }else if(img.dataset && img.dataset.original){
  1058. realSrc=img.dataset.original;
  1059. }else if(img.dataset && img.dataset.src){
  1060. realSrc=img.dataset.src;
  1061. }else if(img._lazyrias && img._lazyrias.srcset){
  1062. realSrc=img._lazyrias.srcset[img._lazyrias.srcset.length-1];
  1063. }else if(img.dataset && img.dataset.origFile){
  1064. realSrc=img.dataset.origFile;
  1065. }else if(img.srcset){
  1066. var srcs=img.srcset.split(","),largeSize=0;
  1067. srcs.forEach(srci=>{
  1068. let srcInfo=srci.trim().split(" "),curSize=parseInt(srcInfo[1]);
  1069. if(srcInfo[1] && curSize>largeSize){
  1070. largeSize=curSize;
  1071. realSrc=srcInfo[0];
  1072. }
  1073. });
  1074. }
  1075. if(realSrc){
  1076. img.src=realSrc;
  1077. img.style.display="block";
  1078. }
  1079. };
  1080. [].forEach.call(eles, ele=>{
  1081. if(ele.tagName=="IMG"){
  1082. setLazyImg(ele);
  1083. }else{
  1084. [].forEach.call(ele.querySelectorAll("img"), img=>{
  1085. setLazyImg(img);
  1086. });
  1087. }
  1088. if(ele.tagName=="A" && ele.classList.contains("lazyload")){
  1089. if(ele.dataset.original){
  1090. ele.style.backgroundImage='url("'+ele.dataset.original+'")';
  1091. }
  1092. }else{
  1093. [].forEach.call(ele.querySelectorAll("a.lazyload"), a=>{
  1094. if(a.dataset.original){
  1095. a.style.backgroundImage='url("'+a.dataset.original+'")';
  1096. }
  1097. });
  1098. }
  1099. });
  1100. let lazyImgSrc=this.curSiteRule.lazyImgSrc;
  1101. if(lazyImgSrc){
  1102. [].forEach.call(eles, ele=>{
  1103. [].forEach.call(ele.querySelectorAll("img"), img=>{
  1104. if(img.getAttribute(lazyImgSrc)){
  1105. img.src=img.getAttribute(lazyImgSrc);
  1106. }
  1107. });
  1108. });
  1109. }
  1110. }
  1111.  
  1112. initPage(callback){
  1113. let self=this;
  1114. curPage=1;
  1115. //if(this.curSiteRule.url && !this.curSiteRule.singleUrl)return;
  1116. this.curSiteRule={};
  1117. this.pageDoc=document;
  1118. this.nextLinkHref=null;
  1119. this.curUrl=location.href;
  1120. this.getRule(()=>{
  1121. if(self.curSiteRule.enable==0){
  1122. debug("Stop as rule disable");
  1123. isPause=true;
  1124. return;
  1125. }
  1126. if(self.curSiteRule && !self.curSiteRule.singleUrl){
  1127. self.hpRules=self.hpRules.filter(item=>{return item&&item.url!=self.curSiteRule.url});
  1128. self.hpRules.unshift(self.curSiteRule);
  1129. if(self.hpRules.length>30){
  1130. self.hpRules.pop();
  1131. }
  1132. storage.setItem("hpRules", self.hpRules);
  1133. }
  1134. let css=self.curSiteRule.css;
  1135. if(css){
  1136. _GM_addStyle(css);
  1137. }
  1138. let code=self.curSiteRule.init;
  1139. if(code){
  1140. try{
  1141. Function('doc','"use strict";' + code)(null);
  1142. }catch(e){
  1143. debug(e);
  1144. }
  1145. }
  1146. self.getNextLink(document);
  1147. callback();
  1148. });
  1149. }
  1150.  
  1151. insertPage(doc, eles, url, callback, tried){
  1152. let oldUrl=this.curUrl;
  1153. let oldTitle=this.pageDoc.title;
  1154. this.pageDoc=doc;
  1155. this.curUrl=url;
  1156. let nextLink=this.getNextLink(doc);
  1157. if(curPage==1 && !tried && !this.nextLinkHref && this.curSiteRule.singleUrl && this.curSiteRule.pageElement && this.curSiteRule.pageElement!=allOfBody){
  1158. this.curSiteRule.action=1;
  1159. this.curUrl=location.href;
  1160. return false;
  1161. }
  1162. if(callback)callback(eles);
  1163. this.getInsert();
  1164. var self=this,newEles=[];
  1165. if(!eles || eles.length==0 || !self.insert || !self.insert.parentNode){
  1166. }else{
  1167. this.pageInit(doc, eles);
  1168. [].forEach.call(eles, ele=>{
  1169. let newEle=ele.cloneNode(true);
  1170. let oldCanvass=ele.querySelectorAll("canvas");
  1171. let newCanvass=newEle.querySelectorAll("canvas");
  1172. for(let i=0;i<oldCanvass.length;i++){
  1173. let oldCanvas=oldCanvass[i];
  1174. let newCanvas=newCanvass[i];
  1175. newCanvas.getContext('2d').drawImage(oldCanvas, 0, 0);
  1176. }
  1177. if(self.curSiteRule.insertPos==2){
  1178. self.insert.appendChild(newEle);
  1179. }else{
  1180. self.insert.parentNode.insertBefore(newEle, self.insert);
  1181. }
  1182. newEles.push(newEle);
  1183. });
  1184. }
  1185. this.pageAction(doc, newEles);
  1186. if(oldUrl!=location.href){
  1187. let isJs=/^(javascript|#)/.test(oldUrl.replace(location.href,""));
  1188. if(!isJs){
  1189. _unsafeWindow.history.replaceState(undefined, oldTitle, oldUrl);
  1190. }
  1191. }
  1192. return true;
  1193. }
  1194. }
  1195. var ruleParser = new RuleParser();
  1196.  
  1197. function initConfig(){
  1198. _GM_registerMenuCommand(i18n(forceState==1?"enable":"disableSite"), ()=>{
  1199. storage.setItem("forceState_"+location.host, (forceState==1?0:1));
  1200. location.reload();
  1201. });
  1202. _GM_registerMenuCommand(i18n("update"), ()=>{
  1203. updateRules(()=>{
  1204. alert(i18n("updateSucc"));
  1205. location.reload();
  1206. },(rule,err)=>{
  1207. alert("Update "+rule.url+" rules fail!");
  1208. });
  1209. alert(i18n("beginUpdate"));
  1210. });
  1211.  
  1212. var configCon,insertPos;
  1213. if(location.href==configPage){
  1214. _GM_addStyle(`
  1215. p>span:nth-child(1),p>span:nth-child(2),p>span:nth-child(3){
  1216. cursor: pointer;
  1217. user-select: none;
  1218. }
  1219. p>span:nth-child(1):hover,p>span:nth-child(2):hover,p>span:nth-child(3):hover{
  1220. color:red;
  1221. }
  1222. .updateDate{
  1223. cursor: pointer;
  1224. user-select: none;
  1225. }
  1226. .updateDate:hover{
  1227. color:red;
  1228. }
  1229. `);
  1230. configCon=document.querySelector(".markdown-body");
  1231. insertPos=configCon.querySelector("hr");
  1232. }else if(location.href==configPage){
  1233. }else return false;
  1234. class Rulebar {
  1235. init(ruleUrl){
  1236. this.ruleUrl=ruleUrl;
  1237. this.item=document.createElement("p");
  1238. this.item.title=ruleUrl.type==0?"AutoPagerize Rules":"Pagetual Rules";
  1239. this.item.dataset.id=this.ruleUrl.id;
  1240. let url=document.createElement("span");
  1241. url.innerHTML=ruleUrl.url;
  1242. let up=document.createElement("span");
  1243. up.innerHTML="↑ ";
  1244. up.title=i18n("sortTitle");
  1245. let down=document.createElement("span");
  1246. down.innerHTML="↓ ";
  1247. down.title=i18n("sortTitle");
  1248. let del=document.createElement("span");
  1249. del.innerHTML="× ";
  1250. up.onclick=e=>{
  1251. this.moveUp();
  1252. };
  1253. down.onclick=e=>{
  1254. this.moveDown();
  1255. };
  1256. del.onclick=e=>{
  1257. this.del();
  1258. };
  1259. this.item.appendChild(up);
  1260. this.item.appendChild(down);
  1261. this.item.appendChild(del);
  1262. this.item.appendChild(url);
  1263. configCon.insertBefore(this.item, insertPos);
  1264. }
  1265. saveSort(){
  1266. let sort=[];
  1267. [].forEach.call(this.item.parentNode.querySelectorAll("p[data-id]"), i=>{
  1268. sort.push(i.dataset.id);
  1269. });
  1270. rulesData.sort=sort;
  1271. storage.setItem("rulesData", rulesData);
  1272. }
  1273. moveUp(){
  1274. let preE=this.item.previousElementSibling;
  1275. if(preE.tagName=="P" && preE.children.length>1){
  1276. this.item.parentNode.insertBefore(this.item,preE);
  1277. this.saveSort();
  1278. }
  1279. }
  1280. moveDown(){
  1281. let nextE=this.item.nextElementSibling;
  1282. if(nextE.tagName=="P" && nextE.children.length>1){
  1283. this.item.parentNode.insertBefore(nextE,this.item);
  1284. this.saveSort();
  1285. }
  1286. }
  1287. del(){
  1288. if(this.ruleUrl.id<2){
  1289. alert(i18n("cantDel"));
  1290. }else if(window.confirm(i18n("confirmDel"))){
  1291. for(let u=0;u<rulesData.urls.length;u++){
  1292. if(this.ruleUrl.id==rulesData.urls[u].id){
  1293. rulesData.urls.splice(u,1);
  1294. break;
  1295. }
  1296. }
  1297. for(let u=0;u<rulesData.sort.length;u++){
  1298. if(this.ruleUrl.id==rulesData.sort[u]){
  1299. rulesData.sort.splice(u,1);
  1300. break;
  1301. }
  1302. }
  1303. storage.setItem("rulesData", rulesData);
  1304. ruleParser.rules=ruleParser.rules.filter(item=>{return item.from!=this.ruleUrl.id});
  1305. storage.setItem("rules", ruleParser.rules);
  1306. this.item.parentNode.removeChild(this.item);
  1307. //location.reload();
  1308. }
  1309. }
  1310. }
  1311. let updateP=document.createElement("p"),i=0;
  1312. let now=new Date().getTime(),inUpdate=false;
  1313.  
  1314.  
  1315. let pastDate=(new Date(updateDate)).toString(),passStr;
  1316. let passTime=(now-updateDate)/1000;
  1317. if(isNaN(passTime)){
  1318. passStr=i18n("firstUpdate");
  1319. }else if(passTime<60){
  1320. passStr=i18n("passSec", parseInt(passTime))+" ← "+i18n("click2update");
  1321. }else if(passTime<60*60){
  1322. passStr=i18n("passMin", parseInt(passTime/60))+" ← "+i18n("click2update");
  1323. }else if(passTime<60*60*24){
  1324. passStr=i18n("passHour", parseInt(passTime/3600))+" ← "+i18n("click2update");
  1325. }else{
  1326. passStr=i18n("passDay", parseInt(passTime/86400))+" ← "+i18n("click2update");
  1327. }
  1328.  
  1329.  
  1330. updateP.className="updateDate";
  1331. updateP.innerHTML=passStr;
  1332. updateP.title=i18n("update")+" - "+pastDate;
  1333. updateP.onclick=e=>{
  1334. updateRules(()=>{
  1335. alert(i18n("updateSucc"));
  1336. updateP.innerHTML=i18n("passSec", 0);
  1337. updateP.title=i18n("update");
  1338. },(rule,err)=>{
  1339. alert("Update "+rule.url+" rules fail!");
  1340. });
  1341. alert(i18n("beginUpdate"));
  1342. };
  1343. configCon.insertBefore(updateP, insertPos);
  1344. if(ruleUrls){
  1345. ruleUrls.forEach(ruleUrl=>{
  1346. var rulebar=new Rulebar();
  1347. rulebar.init(ruleUrl);
  1348. });
  1349. }
  1350. let customUrlsTitle=document.createElement("h2");
  1351. customUrlsTitle.innerHTML=i18n("customUrls");
  1352. configCon.insertBefore(customUrlsTitle, insertPos);
  1353. let customUrlsInput=document.createElement("textarea");
  1354. customUrlsInput.style.width="100%";
  1355. customUrlsInput.placeholder="0|http://wedata.net/databases/AutoPagerize/items_all.json";
  1356. configCon.insertBefore(customUrlsInput, insertPos);
  1357.  
  1358. let opacityTitle=document.createElement("h2");
  1359. opacityTitle.innerHTML=i18n("opacity");
  1360. let opacityInput=document.createElement("input");
  1361. opacityInput.value=rulesData.opacity*100;
  1362. opacityInput.type="number";
  1363. opacityInput.style.width="50px";
  1364. opacityInput.style.margin="0 0 0 10px";
  1365. opacityTitle.appendChild(opacityInput);
  1366. configCon.insertBefore(opacityTitle, insertPos);
  1367.  
  1368.  
  1369. let hideBarTitle=document.createElement("h2");
  1370. hideBarTitle.innerHTML=i18n("hideBar");
  1371. let hideBarInput=document.createElement("input");
  1372. hideBarInput.type="checkbox";
  1373. hideBarInput.style.width="50px";
  1374. hideBarInput.style.height="20px";
  1375. hideBarInput.checked=rulesData.hideBar;
  1376. hideBarTitle.appendChild(hideBarInput);
  1377. configCon.insertBefore(hideBarTitle, insertPos);
  1378.  
  1379. let dbClick2StopTitle=document.createElement("h2");
  1380. dbClick2StopTitle.innerHTML=i18n("dbClick2Stop");
  1381. let dbClick2StopInput=document.createElement("input");
  1382. dbClick2StopInput.type="checkbox";
  1383. dbClick2StopInput.style.width="50px";
  1384. dbClick2StopInput.style.height="20px";
  1385. dbClick2StopInput.checked=rulesData.dbClick2Stop;
  1386. dbClick2StopTitle.appendChild(dbClick2StopInput);
  1387. configCon.insertBefore(dbClick2StopTitle, insertPos);
  1388.  
  1389. let enableWhiteListTitle=document.createElement("h2");
  1390. enableWhiteListTitle.innerHTML=i18n("autoRun");
  1391. let enableWhiteListInput=document.createElement("input");
  1392. enableWhiteListInput.type="checkbox";
  1393. enableWhiteListInput.style.width="50px";
  1394. enableWhiteListInput.style.height="20px";
  1395. enableWhiteListInput.checked=rulesData.enableWhiteList!=true;
  1396. enableWhiteListTitle.appendChild(enableWhiteListInput);
  1397. configCon.insertBefore(enableWhiteListTitle, insertPos);
  1398.  
  1399. let customRulesTitle=document.createElement("h2");
  1400. customRulesTitle.innerHTML=i18n("customRules")
  1401. configCon.insertBefore(customRulesTitle, insertPos);
  1402. let customRulesInput=document.createElement("textarea");
  1403. customRulesInput.style.width="100%";
  1404. customRulesInput.style.height="500px";
  1405. 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]`;
  1406. customRulesInput.value=getFormatJSON(ruleParser.customRules);
  1407. configCon.insertBefore(customRulesInput, insertPos);
  1408. let saveBtn=document.createElement("button");
  1409. saveBtn.innerHTML=i18n("save");
  1410. saveBtn.style.width="100%";
  1411. configCon.insertBefore(saveBtn, insertPos);
  1412. saveBtn.onclick=e=>{
  1413. try{
  1414. storage.setItem("hpRules", []);
  1415. if(customRulesInput.value==""){
  1416. storage.setItem("customRules", "");
  1417. }else{
  1418. let customRules=JSON.parse(customRulesInput.value);
  1419. if(Array && Array.isArray && !Array.isArray(customRules)){
  1420. alert("Rules must be a Array!");
  1421. return;
  1422. }
  1423. debug(customRules);
  1424. storage.setItem("customRules", customRules);
  1425. }
  1426. }catch(e){
  1427. debug(e);
  1428. alert("JSON error, check again!");
  1429. return;
  1430. }
  1431. rulesData.opacity=opacityInput.value/100;
  1432. rulesData.hideBar=hideBarInput.checked;
  1433. rulesData.dbClick2Stop=dbClick2StopInput.checked;
  1434. rulesData.enableWhiteList=!enableWhiteListInput.checked;
  1435. storage.setItem("rulesData", rulesData);
  1436. let customUrls=customUrlsInput.value.trim();
  1437. if(customUrls){
  1438. customUrls=customUrls.split(/\n/);
  1439. for(let c=0;c<customUrls.length;c++){
  1440. let urlArr=customUrls[c].split("|"),url,type=1;
  1441. if(urlArr.length==1){
  1442. url=urlArr[0].trim();
  1443. if(!/^http/.test(url)){
  1444. alert("Wrong url, check again!");
  1445. return;
  1446. }
  1447. }else if(urlArr.length==2){
  1448. type=urlArr[0].trim();
  1449. url=urlArr[1].trim();
  1450. if(!/^http/.test(url)){
  1451. alert("Wrong url, check again!");
  1452. return;
  1453. }
  1454. }else{
  1455. break;
  1456. }
  1457. let maxId=0,hasUrl=false;
  1458. if(!rulesData.urls){
  1459. rulesData.urls=[];
  1460. maxId=1;
  1461. }else{
  1462. rulesData.urls.forEach(u=>{
  1463. if(maxId<u.id){
  1464. maxId=u.id;
  1465. }
  1466. if(u.url==url){
  1467. hasUrl=true;
  1468. }
  1469. });
  1470. if(hasUrl)break;
  1471. }
  1472. rulesData.urls.push({id:maxId+1,url:url,type:type});
  1473. rulesData.sort.push(maxId+1);
  1474. storage.setItem("rulesData", rulesData);
  1475. }
  1476. }
  1477. alert("Modified successfully");
  1478. location.reload();
  1479. };
  1480. return true;
  1481. }
  1482.  
  1483. var inUpdate=false;
  1484. function updateRules(success,fail){
  1485. if(inUpdate)return;
  1486. inUpdate=true;
  1487. let ruleIndex=ruleUrls.length-1;
  1488. storage.setItem("hpRules", []);
  1489. function addNextRule(){
  1490. if(ruleIndex<0){
  1491. let now=new Date().getTime();
  1492. storage.setItem("ruleLastUpdate", now);
  1493. storage.setItem("rules", ruleParser.rules);
  1494. inUpdate=false;
  1495. success();
  1496. }else{
  1497. let rule=ruleUrls[ruleIndex--];
  1498. ruleParser.addRuleByUrl(rule.url, rule.type, rule.id, (json,err)=>{
  1499. if(!json){
  1500. fail(rule,err);
  1501. }
  1502. storage.setItem("rules", ruleParser.rules);
  1503. addNextRule();
  1504. })
  1505. }
  1506. }
  1507. addNextRule();
  1508. }
  1509.  
  1510. function objIsArr(obj) {
  1511. return obj &&
  1512. typeof obj === 'object' &&
  1513. typeof obj.length === 'number' &&
  1514. !(obj.propertyIsEnumerable('length'));
  1515. }
  1516.  
  1517. function isVisible(el, win) {
  1518. if(!el.offsetParent)return false;
  1519. var loopable = true,
  1520. visible = el.tagName && win.getComputedStyle(el).display != 'none' && win.getComputedStyle(el).visibility != 'hidden';
  1521. while(loopable && visible) {
  1522. el = el.parentNode;
  1523.  
  1524. if(el && el.tagName && el.tagName!="BODY") {
  1525. visible = win.getComputedStyle(el).display != 'none' && win.getComputedStyle(el).visibility != 'hidden';
  1526. }else {
  1527. loopable = false;
  1528. }
  1529. }
  1530. return visible;
  1531. }
  1532.  
  1533. function getFormatJSON(obj){
  1534. if(!objIsArr(obj))return "";
  1535. let ret="[\n";
  1536. let len=obj.length,i=0,isLast;
  1537. obj.forEach(item=>{
  1538. ret+=" {\n";
  1539. let iLen=Object.keys(item).length,j=0;
  1540. for(let key in item){
  1541. isLast=(++j)==iLen;
  1542. let value=item[key];
  1543. if(objIsArr(value)){
  1544. let vstr="[",v=0,vIsLast=false;
  1545. value.forEach(vi=>{
  1546. vIsLast=(++v)==value.length;
  1547. vstr+="\""+vi.replace(/\\/g,"\\\\")+"\""+(vIsLast?"":",");
  1548. });
  1549. vstr+="]";
  1550. ret+=" \""+key+"\":"+vstr+""+(isLast?"":",")+"\n";
  1551. }else{
  1552. if(typeof value=="string"){
  1553. value=value.replace(/\\/g,"\\\\").replace(/"/g,"\\\"");
  1554. value="\""+value+"\"";
  1555. }
  1556. ret+=" \""+key+"\":"+value+""+(isLast?"":",")+"\n";
  1557. }
  1558. }
  1559. isLast=(++i)==len;
  1560. ret+=" }"+(isLast?"":",")+"\n";
  1561. });
  1562. ret+="]";
  1563. return ret;
  1564. }
  1565.  
  1566. function initRules(callback) {
  1567. /*0 wedata格式,1 pagetual格式*/
  1568. ruleUrls=[
  1569. {
  1570. id:0,
  1571. url:'https://raw.githubusercontent.com/hoothin/UserScripts/master/Pagetual/pagetualRules.json',
  1572. type:1
  1573. },
  1574. {
  1575. id:1,
  1576. url:'http://wedata.net/databases/AutoPagerize/items_all.json',
  1577. type:0,
  1578. }
  1579. ];var i=0,j=0;
  1580.  
  1581. ruleParser.initSavedRules(()=>{
  1582. storage.getItem("rulesData", data=>{
  1583. if(data){
  1584. rulesData=data;
  1585. if(data.urls)ruleUrls=ruleUrls.concat(data.urls);
  1586. if(data.sort){
  1587. let urls=[];
  1588. data.sort.forEach(id=>{
  1589. for(let s=0;s<ruleUrls.length;s++){
  1590. if(id==ruleUrls[s].id){
  1591. urls.push(ruleUrls[s]);
  1592. break;
  1593. }
  1594. }
  1595. });
  1596. ruleUrls=urls;
  1597. }
  1598. }
  1599. if(typeof(rulesData.opacity)=="undefined"){
  1600. rulesData.opacity=0.3;
  1601. }
  1602. if(typeof(rulesData.hideBar)=="undefined"){
  1603. rulesData.hideBar=true;
  1604. }
  1605. if(typeof(rulesData.dbClick2Stop)=="undefined"){
  1606. rulesData.dbClick2Stop=true;
  1607. }
  1608. if(typeof(rulesData.enableWhiteList)=="undefined"){
  1609. rulesData.enableWhiteList=false;
  1610. }
  1611. storage.getItem("forceState_"+location.host, v=>{
  1612. storage.getItem("ruleLastUpdate", date=>{
  1613. if(typeof(v)=="undefined"){
  1614. v=(rulesData.enableWhiteList?1:0);
  1615. }
  1616. forceState=v;
  1617. updateDate=date;
  1618. if(initConfig())return;
  1619. if(forceState==1)return;
  1620. let now=new Date().getTime();
  1621. if(!date || now-date>2*24*60*60*1000){
  1622. storage.setItem("ruleLastUpdate", now);
  1623. updateRules(()=>{
  1624. callback();
  1625. },(rule,err)=>{});
  1626. }else{
  1627. callback();
  1628. }
  1629. });
  1630. });
  1631. });
  1632. });
  1633. }
  1634.  
  1635. function requestDoc(url, callback){
  1636. _GM_xmlhttpRequest({
  1637. url: url,
  1638. method: 'GET',
  1639. overrideMimeType: 'text/html;charset='+document.charset,
  1640. headers: {
  1641. 'Referer': location.href
  1642. },
  1643. timeout: 5000,
  1644. onload: function(res) {
  1645. var doc=null;
  1646. try {
  1647. doc=document.implementation.createHTMLDocument('');
  1648. doc.documentElement.innerHTML=res.response;
  1649. }
  1650. catch (e) {
  1651. debug('parse error'+e.toString());
  1652. }
  1653. let pageElement=ruleParser.getPageElement(doc);
  1654. //只有1的話怕不是圖片哦
  1655. if(pageElement && (pageElement.length>1 || (pageElement.length==1 && pageElement[0].tagName!="IMG") )){
  1656. let result=ruleParser.insertPage(doc, pageElement, url, callback, false);
  1657. if(!result){
  1658. requestFromIframe(url, (doc, eles)=>{
  1659. loadPageOver();
  1660. if(eles){
  1661. ruleParser.insertPage(doc, eles, url, callback, true);
  1662. }
  1663. });
  1664. }
  1665. }else if(ruleParser.curSiteRule.singleUrl){
  1666. requestFromIframe(url, (doc, eles)=>{
  1667. loadPageOver();
  1668. if(eles){
  1669. ruleParser.insertPage(doc, eles, url, callback, true);
  1670. }
  1671. });
  1672. }
  1673. },
  1674. onerror: function(e){
  1675. debug(e);
  1676. callback(false);
  1677. },
  1678. ontimeout: function(e){
  1679. debug(e);
  1680. callback(false);
  1681. }
  1682. });
  1683. }
  1684.  
  1685. var failFromIframe=0;
  1686. function requestFromIframe(url, callback){
  1687. let orgPage,curPage;
  1688. let iframe = document.createElement('iframe');
  1689. iframe.name = 'pagetual-iframe';
  1690. iframe.width = '100%';
  1691. iframe.height = '0';
  1692. iframe.frameBorder = '0';
  1693. if(ruleParser.curSiteRule.sandbox!=false){
  1694. iframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  1695. }
  1696. iframe.style.cssText = 'margin:0!important;padding:0!important;visibility:hidden!important;';
  1697. iframe.addEventListener("load", e=>{
  1698. let waitTime=500,checkEval;
  1699. if(ruleParser.curSiteRule.wait){
  1700. if(isNaN(ruleParser.curSiteRule.wait)){
  1701. try{
  1702. checkEval=Function("doc",'"use strict";' + ruleParser.curSiteRule.wait);
  1703. }catch(e){
  1704. debug(e);
  1705. }
  1706. }else{
  1707. waitTime=ruleParser.curSiteRule.wait;
  1708. }
  1709. }
  1710. setTimeout(()=>{
  1711. let tryTimes=0;
  1712. function checkIframe(){
  1713. try{
  1714. let doc=iframe.contentDocument || iframe.contentWindow.document;
  1715. let eles=ruleParser.getPageElement(doc, iframe.contentWindow);
  1716. if(eles && eles.length>0 && (!checkEval || checkEval(doc))){
  1717. callback(doc, eles);
  1718. }else if(tryTimes++ < 10){
  1719. setTimeout(()=>{
  1720. checkIframe();
  1721. },waitTime);
  1722. return;
  1723. }else{
  1724. if(failFromIframe++ > 2){
  1725. failFromIframe=0;
  1726. debug("Stop as failFromIframe");
  1727. isPause=true;
  1728. callback(false, false);
  1729. }else{
  1730. callback(false, false);
  1731. }
  1732. }
  1733. }catch(e){
  1734. debug("Stop as cors");
  1735. //isPause=true;
  1736. ruleParser.curSiteRule.pageElement=allOfBody;
  1737. ruleParser.getInsert(true);
  1738. callback(false, false);
  1739. }
  1740. document.body.removeChild(iframe);
  1741. }
  1742. checkIframe();
  1743. },waitTime);
  1744. });
  1745. iframe.src=url;
  1746. document.body.appendChild(iframe);
  1747. }
  1748.  
  1749. function initPage(){
  1750. ruleParser.initPage(()=>{
  1751. if(ruleParser.nextLinkHref){
  1752. initView();
  1753. let isJs=/^(javascript|#)/.test(ruleParser.nextLinkHref.replace(location.href,""));
  1754. if(!isJs){
  1755. _GM_registerMenuCommand(i18n(forceState==2?"cancelForceIframe":"forceIframe"), ()=>{
  1756. storage.setItem("forceState_"+location.host, (forceState==2?"":2));
  1757. location.reload();
  1758. });
  1759. }
  1760. _GM_registerMenuCommand(i18n("loadNow"), ()=>{
  1761. let loadNum=window.prompt(i18n("loadConfirm"), "1");
  1762. if(loadNum==="" || loadNum===null)return;
  1763. autoLoadNum=Math.abs(parseInt(loadNum));
  1764. nextPage();
  1765. });
  1766. }
  1767. initListener();
  1768. nextPage();
  1769. });
  1770. }
  1771.  
  1772. function initView(){
  1773. _GM_addStyle(`
  1774. .pagetual_pageBar.stop {
  1775. -webkit-filter: invert(100%);
  1776. filter: invert(100%);
  1777. opacity: 1!important;
  1778. }
  1779. .pagetual_pageBar.hide {
  1780. display: none!important;
  1781. }
  1782. .pagetual_pageBar:hover {
  1783. opacity: 1!important;
  1784. }
  1785. .pagetual_pageBar span {
  1786. vertical-align: super;
  1787. }
  1788.  
  1789. .pagetual_pageBar span>svg:hover {
  1790. animation: touhouAni 1s infinite;
  1791. }
  1792. .pagetual_pageBar .pagetual_pageNum{
  1793. color: #55555f;
  1794. }
  1795. .pagetual_pageBar .pagetual_pageNum:hover{
  1796. color: #ff6464;
  1797. }
  1798.  
  1799. @keyframes touhouAni{
  1800. from {transform: rotate(0deg) scale3d(1.2, 1.2, 1.2);}
  1801. to {transform: rotate(360deg);}
  1802. }
  1803. @-webkit-keyframes touhouAni{
  1804. from {transform: rotate(0deg) scale3d(1.2, 1.2, 1.2);}
  1805. to {transform: rotate(360deg);}
  1806. }
  1807. `);
  1808. 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;`;
  1809. }
  1810. var loadingDiv=document.createElement("div");
  1811. loadingDiv.className="pagetual_loading";
  1812. 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;";
  1813. 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>`;
  1814.  
  1815. 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>`;
  1816. 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>`;
  1817. var pageBarStyle;
  1818. 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;`;
  1819. 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);`;
  1820.  
  1821. var isPause=false,isLoading=false,curPage=1,forceState=0,bottomGap=1000,autoLoadNum=-1;
  1822.  
  1823. function changeStop(stop, hide){
  1824. isPause=stop;
  1825. [].forEach.call(document.querySelectorAll(".pagetual_pageBar"), bar=>{
  1826. if(isPause){
  1827. bar.classList.add("stop");
  1828. if(hide)bar.classList.add("hide");
  1829. }else{
  1830. bar.classList.remove("stop");
  1831. if(hide)bar.classList.remove("hide");
  1832. }
  1833. });
  1834. }
  1835.  
  1836. function isInViewPort(element) {
  1837. if(!element.parentNode)return false;
  1838. if(_unsafeWindow.getComputedStyle(element).display=="none")return false;
  1839. const viewWidth = window.innerWidth || document.documentElement.clientWidth;
  1840. const viewHeight = window.innerHeight || document.documentElement.clientHeight;
  1841. const {
  1842. top,
  1843. right,
  1844. bottom,
  1845. left,
  1846. } = element.getBoundingClientRect();
  1847.  
  1848. return (
  1849. top >= 0 &&
  1850. left >= 0 &&
  1851. right <= viewWidth &&
  1852. bottom <= viewHeight
  1853. );
  1854. }
  1855.  
  1856. var urlChanged=false;
  1857. var _wr = function(type) {
  1858. var orig = history[type];
  1859. return function() {
  1860. var rv = orig.apply(this, arguments);
  1861. var e = new Event(type);
  1862. e.arguments = arguments;
  1863. window.dispatchEvent(e);
  1864. return rv;
  1865. };
  1866. };
  1867. history.pushState = _wr('pushState');
  1868. window.addEventListener('pushState', function(e) {
  1869. urlChanged=true;
  1870. isPause=true;
  1871. setTimeout(()=>{
  1872. if(location.href==configPage){
  1873. location.reload();
  1874. }
  1875. },1);
  1876. });
  1877.  
  1878. function initListener(){
  1879. let loadmoreBtn,loading=true;
  1880. setTimeout(()=>{
  1881. loadmoreBtn=getLoadMore(document);
  1882. if(loadmoreBtn && isVisible(loadmoreBtn, _unsafeWindow)){
  1883. loading=false;
  1884. }
  1885. },500);
  1886. let checkScrollReach=()=>{
  1887. let scrolly=window.scrollY;
  1888. let windowHeight=window.innerHeight || document.documentElement.clientHeight;
  1889. let scrollH=Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
  1890. if(scrollH-scrolly-windowHeight<bottomGap){
  1891. nextPage();
  1892. }
  1893. };
  1894. document.addEventListener('scroll', e=>{
  1895. if(urlChanged){
  1896. ruleParser.initPage(()=>{
  1897. if(ruleParser.nextLinkHref)initView();
  1898. });
  1899. urlChanged=false;
  1900. isPause=false;
  1901. }
  1902. if(isPause)return;
  1903. if(!loading){
  1904. loading=true;
  1905. setTimeout(()=>{loading=false},200);
  1906. if(!loadmoreBtn || !loadmoreBtn.parentNode){
  1907. loadmoreBtn=getLoadMore(document);
  1908. }
  1909. if(loadmoreBtn){
  1910. if(isInViewPort(loadmoreBtn)){
  1911. emuClick(loadmoreBtn);
  1912. }
  1913. }
  1914. }
  1915. setTimeout(()=>{
  1916. if(!isLoading){
  1917. checkScrollReach();
  1918. }
  1919. },100);
  1920. }, true);
  1921. document.addEventListener('dblclick', e=>{
  1922. if(rulesData.dbClick2Stop){
  1923. setTimeout(()=>{
  1924. changeStop(!isPause, rulesData.hideBar);
  1925. if(!isPause){
  1926. checkScrollReach();
  1927. }
  1928. },200);
  1929. }
  1930. });
  1931. }
  1932.  
  1933. function getLoadMore(doc){
  1934. if(ruleParser.curSiteRule.loadMore==="")return null;
  1935. let btnSel=ruleParser.curSiteRule.loadMore||".LoadMore,.load-more";
  1936. let loadmoreBtn=doc.querySelector(btnSel);
  1937. if(!loadmoreBtn){
  1938. let buttons=doc.querySelectorAll("input,button,a,div[onclick]"),loadmoreReg=/^\s*(加载更多|加載更多|load\s*more|もっと読み込む)\s*$/i;
  1939. for(let i=0;i<buttons.length;i++){
  1940. let button=buttons[i];
  1941. if(button && loadmoreReg.test(button.innerText)){
  1942. loadmoreBtn=button;
  1943. break;
  1944. }
  1945. }
  1946. }
  1947. return loadmoreBtn;
  1948. }
  1949.  
  1950. var hasPageNum=true;
  1951. function createPageBar(url){
  1952. let insert=ruleParser.getInsert();
  1953. if(!insert || !insert.parentNode)return;
  1954. curPage++;
  1955. if(rulesData.opacity==0)return;
  1956. let example=ruleParser.curSiteRule.insertPos==2?insert.children[0]:insert;
  1957. let inTable=example.parentNode.tagName=="TABLE" ||
  1958. example.tagName=="TR" ||
  1959. example.tagName=="TBODY" ||
  1960. (example.previousElementSibling && example.previousElementSibling.tagName=="TR") ||
  1961. (example.previousElementSibling && example.previousElementSibling.tagName=="TBODY");
  1962. let inLi=example.tagName=="LI" || (example.previousElementSibling && example.previousElementSibling.tagName=="LI");
  1963. let pageBar=document.createElement(inTable?"tr":(inLi?"li":"div"));
  1964. let upSpan=document.createElement("span");
  1965. let downSpan=document.createElement("span");
  1966. let pageText=document.createElement("a");
  1967. let pageNum;
  1968. pageBar.className="pagetual_pageBar";
  1969. pageBar.id="pagetual_pageBar";
  1970. if(isPause){
  1971. pageBar.classList.add("stop");
  1972. }
  1973. pageBar.style.cssText=pageBarStyle;
  1974. pageBar.title=i18n(isPause?"enable":"disable");
  1975. upSpan.innerHTML=upSvg;
  1976. upSpan.title=i18n("toTop");
  1977. downSpan.innerHTML=downSvg;
  1978. downSpan.title=i18n("toBottom");
  1979. upSpan.style.cssText=initStyle;
  1980. downSpan.style.cssText=initStyle;
  1981. pageText.href=url;
  1982. pageText.style.cssText=pageTextStyle;
  1983. pageText.title=i18n("current");
  1984. pageBar.appendChild(upSpan);
  1985. pageBar.appendChild(pageText);
  1986. if(ruleParser.curSiteRule.pageNum || (hasPageNum && /[&\/\?](p=|page[=\/])\d+/.test(url))){
  1987. pageText.innerHTML="Page ";
  1988. pageNum=document.createElement("span");
  1989. pageNum.innerText=curPage;
  1990. pageNum.className="pagetual_pageNum";
  1991. pageNum.title=i18n("inputPageNum");
  1992. pageNum.style.cssText=pageTextStyle;
  1993. pageNum.style.cursor="pointer";
  1994. pageNum.style.color="";
  1995. pageNum.addEventListener("click", e=>{
  1996. let pageInput=prompt(i18n("inputPageNum"));
  1997. if(pageInput){
  1998. let pageLink=ruleParser.getLinkByPage(url, pageInput);
  1999. if(pageLink){
  2000. _GM_openInTab(pageLink,{active:true});
  2001. }
  2002. }
  2003. e.preventDefault();
  2004. e.stopPropagation();
  2005. });
  2006. pageBar.appendChild(pageNum);
  2007. }else{
  2008. pageText.innerHTML="Page "+curPage;
  2009. hasPageNum=false;
  2010. }
  2011. pageBar.appendChild(downSpan);
  2012. if(inTable){
  2013. example=(example.tagName=="TR" || example.tagName=="TBODY")?example:example.previousElementSibling;
  2014. if(example.previousElementSibling)example=example.previousElementSibling;
  2015. let tdNum=0;
  2016. [].forEach.call(example.querySelectorAll("td,th"), td=>{
  2017. tdNum+=td.colSpan||1;
  2018. });
  2019. pageBar.style.display="table-row";
  2020. pageBar.style.backgroundColor="unset";
  2021. let td=document.createElement("td");
  2022. td.style.backgroundColor="rgb(240 240 240 / 80%)";
  2023. td.style.borderRadius="20px";
  2024. td.colSpan=tdNum||1;
  2025. td.style.padding="0 0";
  2026. td.style.textAlign="center";
  2027. td.appendChild(upSpan);
  2028. td.appendChild(pageText);
  2029. if(pageNum)td.appendChild(pageNum);
  2030. td.appendChild(downSpan);
  2031. pageBar.appendChild(td);
  2032. }else if(inLi){
  2033. example=example.tagName=="LI"?example:example.previousElementSibling;
  2034. pageBar.style.display="table-row";
  2035. pageBar.style.backgroundColor="unset";
  2036. let td=document.createElement("td");
  2037. td.style.backgroundColor="rgb(240 240 240 / 80%)";
  2038. td.style.borderRadius="20px";
  2039. td.colSpan=example.children.length;
  2040. td.style.padding="0 0";
  2041. td.style.textAlign="center";
  2042. td.style.minWidth="150px";
  2043. td.appendChild(upSpan);
  2044. td.appendChild(pageText);
  2045. if(pageNum)td.appendChild(pageNum);
  2046. td.appendChild(downSpan);
  2047. pageBar.appendChild(td);
  2048. }
  2049.  
  2050. upSpan.addEventListener("click", e=>{
  2051. document.body.scrollTop=0;
  2052. document.documentElement.scrollTop=0;
  2053. e.preventDefault();
  2054. e.stopPropagation();
  2055. });
  2056. downSpan.addEventListener("click", e=>{
  2057. changeStop(true);
  2058. pageBar.title=i18n(isPause?"enable":"disable");
  2059. document.body.scrollTop=9999999;
  2060. document.documentElement.scrollTop=9999999;
  2061. e.preventDefault();
  2062. e.stopPropagation();
  2063. });
  2064. pageBar.addEventListener("click", e=>{
  2065. changeStop(!isPause);
  2066. pageBar.title=i18n(isPause?"enable":"disable");
  2067. });
  2068. pageText.addEventListener("click", e=>{
  2069. e.stopPropagation();
  2070. });
  2071. let parentStyle=_unsafeWindow.getComputedStyle(insert.parentNode);
  2072. pageBar.style.width=parseInt(parentStyle.width)*.99-parseInt(parentStyle.paddingLeft)-parseInt(parentStyle.paddingRight)+"px";
  2073. if(ruleParser.curSiteRule.insertPos==2){
  2074. insert.appendChild(pageBar);
  2075. }else{
  2076. insert.parentNode.insertBefore(pageBar, insert);
  2077. }
  2078. if(ruleParser.curSiteRule.pageBar){
  2079. try{
  2080. Function("pageBar",'"use strict";' + ruleParser.curSiteRule.pageBar)(pageBar);
  2081. }catch(e){
  2082. debug(e);
  2083. }
  2084. }
  2085.  
  2086. let scrollH=Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
  2087. let posEle=pageBar.nextElementSibling||pageBar;
  2088. while(posEle && !posEle.offsetParent){
  2089. posEle=posEle.previousElementSibling||posEle.parentNode;
  2090. }
  2091. if(posEle){
  2092. let actualTop = posEle.offsetTop;
  2093. let current = posEle.parentNode.offsetParent;
  2094. while (current !== null){
  2095. actualTop += current.offsetTop;
  2096. current = current.offsetParent;
  2097. }
  2098. bottomGap=scrollH-actualTop+(window.innerHeight||document.documentElement.clientHeight)*(ruleParser.curSiteRule.rate||1);
  2099. }else{
  2100. bottomGap=1000;
  2101. }
  2102. return pageBar;
  2103. }
  2104.  
  2105. function emuClick(btn){
  2106. if(!PointerEvent)return btn.click();
  2107. let eventParam={
  2108. isTrusted: true,
  2109. altKey: false,
  2110. azimuthAngle: 0,
  2111. bubbles: true,
  2112. button: 0,
  2113. buttons: 0,
  2114. clientX: 1,
  2115. clientY: 1,
  2116. cancelBubble: false,
  2117. cancelable: true,
  2118. composed: true,
  2119. ctrlKey: false,
  2120. defaultPrevented: false,
  2121. detail: 1,
  2122. eventPhase: 2,
  2123. fromElement: null,
  2124. height: 1,
  2125. isPrimary: false,
  2126. metaKey: false,
  2127. pointerId: 1,
  2128. pointerType: "mouse",
  2129. pressure: 0,
  2130. relatedTarget: null,
  2131. returnValue: true,
  2132. shiftKey: false,
  2133. toElement: null,
  2134. twist: 0,
  2135. type: "click",
  2136. which: 1
  2137. };
  2138. var mouseclick = new PointerEvent("click",eventParam);
  2139. btn.dispatchEvent(mouseclick);
  2140. }
  2141.  
  2142. var emuIframe;
  2143. function emuPage(callback){
  2144. let orgPage,curPage,iframeDoc,times=0,loadmoreBtn,loadmoreEnd=false,waitTimes=10;
  2145. function checkPage(){
  2146. if(isPause)return;
  2147. try{
  2148. iframeDoc=emuIframe.contentDocument || emuIframe.contentWindow.document;
  2149. }catch(e){
  2150. debug("Stop as cors");
  2151. isPause=true;
  2152. callback(false, false);
  2153. return;
  2154. }
  2155.  
  2156. let nextLink=ruleParser.getNextLink(iframeDoc);
  2157. let waitTime=500,checkEval;
  2158. if(ruleParser.curSiteRule.wait){
  2159. if(isNaN(ruleParser.curSiteRule.wait)){
  2160. try{
  2161. checkEval=Function("doc",'"use strict";' + ruleParser.curSiteRule.wait);
  2162. }catch(e){
  2163. debug(e);
  2164. }
  2165. }else{
  2166. waitTime=ruleParser.curSiteRule.wait;
  2167. }
  2168. }
  2169.  
  2170. if(!orgPage){
  2171. if(!loadmoreEnd){
  2172. loadmoreBtn=getLoadMore(iframeDoc);
  2173. if(loadmoreBtn && isVisible(loadmoreBtn, iframeDoc.defaultView)){
  2174. emuClick(loadmoreBtn);
  2175. let intv=setInterval(()=>{
  2176. loadmoreBtn=getLoadMore(iframeDoc);
  2177. if(!loadmoreBtn || !loadmoreBtn.parentNode || !isVisible(loadmoreBtn, iframeDoc.defaultView)){
  2178. clearInterval(intv);
  2179. loadmoreEnd=true;
  2180. setTimeout(()=>{
  2181. checkPage();
  2182. },500);
  2183. }else{
  2184. emuClick(loadmoreBtn);
  2185. }
  2186. },200);
  2187. return;
  2188. }else{
  2189. loadmoreEnd=true;
  2190. }
  2191. }
  2192. let pageEle=ruleParser.getPageElement(iframeDoc, iframeDoc.defaultView, true);
  2193. if(!nextLink || !pageEle || (checkEval && !checkEval(iframeDoc))){
  2194. if(ruleParser.curSiteRule.wait && waitTimes-->0){
  2195. setTimeout(()=>{
  2196. checkPage();
  2197. },waitTime);
  2198. return;
  2199. }
  2200. }
  2201. orgPage=pageEle;
  2202. if(!orgPage || orgPage.length==0){
  2203. debug("Stop as no page when emu");
  2204. isPause=true;
  2205. callback(false, false);
  2206. return;
  2207. }
  2208. if(orgPage && orgPage[0].tagName=="UL")orgPage=orgPage[0].children;
  2209. if(orgPage && nextLink){
  2210. orgPage=orgPage[parseInt(orgPage.length/2)];
  2211. if(!isVisible(nextLink, iframeDoc.defaultView)){
  2212. debug("Stop as next hide when emu");
  2213. isPause=true;
  2214. callback(false, false);
  2215. }else{
  2216. emuClick(nextLink);
  2217. setTimeout(()=>{
  2218. checkPage();
  2219. },500);
  2220. }
  2221. }else{
  2222. debug("Stop as no next when emu");
  2223. isPause=true;
  2224. callback(false, false);
  2225. }
  2226. return;
  2227. }
  2228. if(times++ > 20){
  2229. //emuClick(nextLink);
  2230. debug("Stop as timeout when emu");
  2231. isPause=true;
  2232. callback(false, false);
  2233. return;
  2234. }
  2235. let eles=ruleParser.getPageElement(iframeDoc, iframeDoc.defaultView, true),checkItem=eles;
  2236. if(eles && eles.length>0 && eles[0].tagName=="UL")checkItem=eles[0].children;
  2237. if(!eles || eles.length==0 || orgPage == checkItem[parseInt(checkItem.length/2)] || (checkEval && !checkEval(iframeDoc))){
  2238. setTimeout(()=>{
  2239. checkPage();
  2240. },waitTime);
  2241. }else{
  2242. callback(iframeDoc, eles);
  2243. }
  2244. }
  2245. if(!emuIframe){
  2246. emuIframe = document.createElement('iframe');
  2247. emuIframe.name = 'pagetual-iframe';
  2248. if(ruleParser.curSiteRule.sandbox!=false){
  2249. emuIframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  2250. }
  2251. emuIframe.width = '100%';
  2252. emuIframe.height = '0';
  2253. emuIframe.frameBorder = '0';
  2254. emuIframe.style.cssText = 'margin:0!important;padding:0!important;visibility:hidden!important;';
  2255. emuIframe.addEventListener("load", e=>{
  2256. setTimeout(()=>{
  2257. try{
  2258. iframeDoc=emuIframe.contentDocument || emuIframe.contentWindow.document;
  2259. }catch(e){
  2260. debug("Stop as cors");
  2261. isPause=true;
  2262. callback(false, false);
  2263. return;
  2264. }
  2265. let code=ruleParser.curSiteRule.init;
  2266. if(code){
  2267. try{
  2268. Function('doc','"use strict";' + code)(iframeDoc);
  2269. }catch(e){
  2270. debug(e);
  2271. }
  2272. }
  2273. checkPage();
  2274. },500);
  2275. });
  2276. emuIframe.src=location.href;
  2277. document.body.appendChild(emuIframe);
  2278. }else{
  2279. checkPage();
  2280. }
  2281. }
  2282.  
  2283. function forceIframe(url, callback){
  2284. let curIframe = document.createElement('iframe'),iframeDoc;
  2285. curIframe.name = 'pagetual-iframe';
  2286. curIframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  2287. curIframe.frameBorder = '0';
  2288. curIframe.scrolling="no";
  2289. 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;';
  2290. curIframe.addEventListener("load", e=>{
  2291. try{
  2292. iframeDoc=curIframe.contentDocument || curIframe.contentWindow.document;
  2293. }catch(e){
  2294. debug("Stop as cors");
  2295. isPause=true;
  2296. callback(false, false);
  2297. return;
  2298. }
  2299. ruleParser.insertPage(iframeDoc, [], url, null, true);
  2300. callback(curIframe, true);
  2301. curIframe.style.height=iframeDoc.body.scrollHeight+"px";
  2302. curIframe.style.width=iframeDoc.body.scrollWidth+"px";
  2303. });
  2304. let inAction=false;
  2305. let forceRefresh=e=>{
  2306. if(inAction || !iframeDoc)return;
  2307. inAction=true;
  2308. setTimeout(()=>{
  2309. inAction=false;
  2310. curIframe.style.height=iframeDoc.body.scrollHeight+"px";
  2311. curIframe.style.width=iframeDoc.body.scrollWidth+"px";
  2312. if(!ruleParser.nextLinkHref){
  2313. ruleParser.getNextLink(iframeDoc);
  2314. if(ruleParser.nextLinkHref){
  2315. document.removeEventListener("scroll", forceRefresh);
  2316. }
  2317. }else{
  2318. document.removeEventListener("scroll", forceRefresh);
  2319. }
  2320. },500);
  2321. curIframe.style.height=iframeDoc.body.scrollHeight+"px";
  2322. curIframe.style.width=iframeDoc.body.scrollWidth+"px";
  2323. };
  2324. document.addEventListener("scroll", forceRefresh);
  2325. curIframe.src=url;
  2326. let insert=ruleParser.getInsert();
  2327. document.body.appendChild(curIframe);
  2328. return curIframe;
  2329. }
  2330.  
  2331. function loadPageOver(){
  2332. isLoading=false;
  2333. if(loadingDiv.parentNode){
  2334. loadingDiv.parentNode.removeChild(loadingDiv);
  2335. }
  2336. setTimeout(()=>{
  2337. let scrollH=Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
  2338. if(scrollH < (window.innerHeight || document.documentElement.clientHeight)){
  2339. nextPage();
  2340. }
  2341. },1);
  2342. }
  2343.  
  2344. function nextPage(){
  2345. if(isPause || isLoading)return;
  2346. let nextLink=ruleParser.nextLinkHref;
  2347. if(!nextLink)return;
  2348. let insert=ruleParser.getInsert();
  2349. if(insert){
  2350. let isJs=/^(javascript|#)/.test(nextLink.replace(location.href,""));
  2351. if(location.protocol=="https:" && /^http:/.test(nextLink)){
  2352. nextLink=nextLink.replace(/^http/,"https");
  2353. }
  2354. isLoading=true;
  2355. document.body.appendChild(loadingDiv);
  2356. let sleep=ruleParser.curSiteRule.sleep||0;
  2357. setTimeout(()=>{
  2358. if(ruleParser.curSiteRule.pageElementByJs){
  2359. var over=ele=>{
  2360. loadPageOver();
  2361. if(ele){
  2362. createPageBar(nextLink);
  2363. ruleParser.insertPage(null, ele, nextLink, null, true);
  2364. if(autoLoadNum>=0){
  2365. if(autoLoadNum!=0 && --autoLoadNum==0){
  2366. autoLoadNum=-1;
  2367. }else{
  2368. nextPage();
  2369. }
  2370. }
  2371. }else{
  2372. debug("Stop as no page when get by js");
  2373. isPause=true;
  2374. }
  2375. };
  2376. try{
  2377. Function("over",'"use strict";' + ruleParser.curSiteRule.pageElementByJs)(over);
  2378. }catch(e){
  2379. debug(e);
  2380. }
  2381. }else if(ruleParser.curSiteRule.action==1 && !isJs){
  2382. requestFromIframe(nextLink, (doc, eles)=>{
  2383. loadPageOver();
  2384. if(eles){
  2385. createPageBar(nextLink);
  2386. ruleParser.insertPage(doc, eles, nextLink, null, true);
  2387. if(autoLoadNum>=0){
  2388. if(autoLoadNum!=0 && --autoLoadNum==0){
  2389. autoLoadNum=-1;
  2390. }else{
  2391. nextPage();
  2392. }
  2393. }
  2394. }
  2395. });
  2396. }else if((forceState==2||ruleParser.curSiteRule.action==2) && !isJs){
  2397. forceIframe(nextLink, (iframe, eles)=>{
  2398. loadPageOver();
  2399. let pageBar=createPageBar(nextLink);
  2400. iframe.parentNode.insertBefore(pageBar, iframe);
  2401. if(autoLoadNum>=0){
  2402. if(autoLoadNum!=0 && --autoLoadNum==0){
  2403. autoLoadNum=-1;
  2404. }else{
  2405. nextPage();
  2406. }
  2407. }
  2408. });
  2409. }else{
  2410. if(!isJs){
  2411. requestDoc(nextLink, (eles)=>{
  2412. loadPageOver();
  2413. if(eles){
  2414. createPageBar(nextLink);
  2415. if(autoLoadNum>=0){
  2416. if(autoLoadNum!=0 && --autoLoadNum==0){
  2417. autoLoadNum=-1;
  2418. }else{
  2419. nextPage();
  2420. }
  2421. }
  2422. }
  2423. });
  2424. }else{
  2425. emuPage((doc, eles)=>{
  2426. loadPageOver();
  2427. if(eles){
  2428. createPageBar(nextLink);
  2429. ruleParser.insertPage(doc, eles, "", null, true);
  2430. if(autoLoadNum>=0){
  2431. if(autoLoadNum!=0 && --autoLoadNum==0){
  2432. autoLoadNum=-1;
  2433. }else{
  2434. nextPage();
  2435. }
  2436. }
  2437. }
  2438. });
  2439. }
  2440. }
  2441. },sleep);
  2442. }
  2443. }
  2444.  
  2445. function init(){
  2446. initRules(()=>{
  2447. initPage();
  2448. });
  2449. }
  2450. setTimeout(()=>{
  2451. init();
  2452. },300);
  2453. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址