东方永页机 - 自动翻页脚本

自动加载并拼接下一分页内容,无需规则即可支持任何网页

当前为 2022-04-25 提交的版本,查看 最新版本

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