东方永页机

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

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

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

QingJ © 2025

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