东方永页机

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

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

QingJ © 2025

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