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

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

目前为 2022-04-06 提交的版本。查看 最新版本

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