东方永页机

自动翻页脚本 - 自动加载并拼接下一分页内容,无需规则驱动支持任意网页

当前为 2022-06-20 提交的版本,查看 最新版本

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