东方永页机

自动翻页

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

  1. // ==UserScript==
  2. // @name Pagetual
  3. // @name:zh-CN 东方永页机
  4. // @name:zh-TW 東方永頁機
  5. // @name:ja 東方永頁機
  6. // @name:ru Пейджетуал
  7. // @name:de Pagetual
  8. // @name:es Pagetual
  9. // @name:fr Pagetual
  10. // @name:it Pagetual
  11. // @name:ko 東方永頁機
  12. // @namespace hoothin
  13. // @version 1.0.6
  14. // @description Simply auto loading paginated web pages
  15. // @description:zh-CN 自动翻页
  16. // @description:zh-TW 自動翻頁
  17. // @description:ja Webページを自動で読み込み継ぎ足し表示を行うブラウザ拡張です
  18. // @description:ru Просто автоматически загрузите следующую страницу
  19. // @description:de Laden Sie einfach automatisch die nächste Seite
  20. // @description:es Carga automática de páginas web paginadas
  21. // @description:fr Chargement automatique des pages Web paginées
  22. // @description:it Caricamento automatico di pagine Web impaginate
  23. // @description:ko 다음 페이지를 자동으로 로드하세요
  24. // @author hoothin
  25. // @include http://*
  26. // @include https://*
  27. // @license MIT
  28. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAdVBMVEUAAADZHQbYHQXaHQbYHgbZHATYHgbYHgbZHQXYHgXZHgbZHgfYHQXYHQbXHQDYHgbYHgbYHQXZHgbYHgbYHgbYHgXaHQbYHQbYHQbZHgbZHgbZHQbZHgbYHgbYHQbYHQXYHgfaHwDZHQXZHQbYHwTVHQrYHgbCqWmRAAAAJnRSTlMAUL8oyD/el2cy1CCPcg/35euggPG6FajOr4Z617OdXkkIi1g4GcTbfAwAAAJDSURBVFjD7dbXtqpADADQ0NsA0hHQY83/f+K9rqNOCzPwfvaTugimjEH4s86739yX0+3n8dwd7dQh/seiaWbHscUqCnZEP24LvrXjXLyyaVJMp2RbeFajZInhJWCHcurB7idEVfS+89SGsS28qJFwyN59ZcgsnXeRVrwvSNLFNJEA1/jfDqW4Po8Z1+XwsX6HGA1Cft1Y0ndw0OjCrwxDj4hPDqgYXbGlZ35pj0fQ1dr0C4DM5++lZHMt/qLNXq2rAG5qtWPdoaL5/ZwXloHAP1onUKs3SOSOO5YEsFNLkJN2T/IRRt0gj6ZUUpZTyMmzUx/Vw8wVhwgEI9pog2PiHRO0OhTq2hLnytBK3yRdsKeCGjRnoagKLRbQNfwsXdGiAkLMu5ihWUSvr5S/tPWPlIV8kxvn58CKatMu7Ly1+Kzcsgwj0wqXlgnNMT7CrFPwwaTp+OsSKS4Y1UJ5Phl/BSM3F7JBQmKO78UGJWT/zWJpTae7E4CTtJcnVJ2E7dV4RAWlY36uM96gFnHQT0HFe0zOIZYbnGsVRJZHc/DZFS15pu54URfUSgYJvVJ8BgpPWWu3TwfJUxkvPahylAzy573ybXfQHVEUFt/2uC67KgWcybPp602gndMMKF647bfIUg9oQYmCH6B1PF7jHFDQk2n6PJ7gDcil1FekowdGuVBGqw7rGZXNFSweLnJRJo4pD4cENphb/CoH57O6o8q/wzbJtCBXuXVz7sqQBbBDMJ9a6e+SA/tlzjx0Q3TLL0/4Y/QPSYfEIomUUSEAAAAASUVORK5CYII=
  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. // @connect wedata.net
  46. // @connect githubusercontent.com
  47. // @run-at document-idle
  48. // @exclude http://www.toodledo.com/tasks/*
  49. // @exclude http*://maps.google.com*/*
  50. // @exclude *://www.google.*/_/chrome/newtab*
  51. // @exclude *://mega.*/*
  52. // @exclude *://*.mega.*/*
  53. // @exclude *://*.youku.com/v_*
  54. // @exclude *://*pan.baidu.com
  55. // @exclude *://*.iqiyi.com/v_*
  56. // @exclude *://*.iqiyi.com/w_*
  57. // @exclude *://*.iqiyi.com/a_*
  58. // @exclude *://*.le.com/ptv/vplay/*
  59. // @exclude *://v.qq.com/x/cover/*
  60. // @exclude *://v.qq.com/x/page/*
  61. // @exclude *://v.qq.com/tv/*
  62. // @exclude *://*.tudou.com/listplay/*
  63. // @exclude *://*.tudou.com/albumplay/*
  64. // @exclude *://*.tudou.com/programs/view/*
  65. // @exclude *://*.mgtv.com/b/*
  66. // @exclude *://film.sohu.com/album/*
  67. // @exclude *://tv.sohu.com/v/*
  68. // @exclude *://*.bilibili.com/video/*
  69. // @exclude *://*.bilibili.com/bangumi/play/*
  70. // @exclude *://*.baofeng.com/play/*
  71. // @exclude *://vip.pptv.com/show/*
  72. // @exclude *://v.pptv.com/show/*
  73. // @exclude *://www.le.com/ptv/vplay/*
  74. // @exclude *://www.wasu.cn/Play/show/*
  75. // @exclude *://m.v.qq.com/x/cover/*
  76. // @exclude *://m.v.qq.com/x/page/*
  77. // @exclude *://m.v.qq.com/*
  78. // @exclude *://m.iqiyi.com/*
  79. // @exclude *://m.iqiyi.com/kszt/*
  80. // @exclude *://m.youku.com/alipay_video/*
  81. // @exclude *://m.mgtv.com/b/*
  82. // @exclude *://m.tv.sohu.com/v/*
  83. // @exclude *://m.film.sohu.com/album/*
  84. // @exclude *://m.le.com/ptv/vplay/*
  85. // @exclude *://m.pptv.com/show/*
  86. // @exclude *://m.acfun.cn/v/*
  87. // @exclude *://m.bilibili.com/video/*
  88. // @exclude *://m.bilibili.com/anime/*
  89. // @exclude *://m.bilibili.com/bangumi/play/*
  90. // @exclude *://m.wasu.cn/Play/show/*
  91. // @exclude *://www.youtube.com
  92. // @exclude *://www.youtube.com/
  93. // @exclude *://www.youtube.com/watch*
  94. // @exclude *://baike.baidu.com/*
  95. // ==/UserScript==
  96.  
  97. (function() {
  98. 'use strict';
  99.  
  100. if (window.name === 'pagetual-iframe') {
  101. var domloaded = function (){
  102. window.scroll(window.scrollX, 99999);
  103. //window.parent.postMessage('pagetual-iframe:DOMLoaded', '*');
  104. };
  105. if(window.opera){
  106. document.addEventListener('DOMContentLoaded', domloaded, false);
  107. } else {
  108. domloaded();
  109. }
  110. return;
  111. }
  112.  
  113. if(window.top != window.self){
  114. return;
  115. }
  116.  
  117. const lang = navigator.appName=="Netscape"?navigator.language:navigator.userLanguage;
  118. var i18n=(name, param)=>{
  119. let config={};
  120. switch (lang){
  121. case "zh-CN":
  122. case "zh-SG":
  123. config={
  124. disable:"暂时禁用",
  125. disableSite:"在此站禁用",
  126. enable:"启用翻页",
  127. toTop:"回到顶部",
  128. toBottom:"前往页尾",
  129. current:"当前页",
  130. forceIframe:"强制拼接",
  131. cancelForceIframe:"取消强制拼接",
  132. configure:"打开配置页",
  133. update:"立即更新规则",
  134. passSec:"更新于 #t# 秒前",
  135. passMin:"更新于 #t# 分钟前",
  136. passHour:"更新于 #t# 小时前",
  137. passDay:"更新于 #t# 天前",
  138. cantDel:"无法删除内置规则",
  139. confirmDel:"是否确认要删除此规则?",
  140. updateSucc:"更新成功",
  141. beginUpdate:"正在更新,请稍候",
  142. customUrls:"导入规则url,一行一条,AutoPagerize 格式规则需要以\"0|\"开头",
  143. customRules:"输入【东方永页机】格式的自定义规则",
  144. save:"保存设置",
  145. loadingText:"少女祈祷中...",
  146. opacity:"分页隔条透明值",
  147. hideBar:"空白处双击隐藏分页隔条",
  148. sortTitle:"排序在下次更新规则后生效"
  149. };
  150. break;
  151. case "zh-TW":
  152. case "zh-HK":
  153. config={
  154. disable:"暫時禁用",
  155. disableSite:"在此站禁用",
  156. enable:"啟用翻頁",
  157. toTop:"回到頂部",
  158. toBottom:"前往頁尾",
  159. current:"當前頁",
  160. forceIframe:"強制拼接",
  161. cancelForceIframe:"取消强制拼接",
  162. configure:"打開配置頁",
  163. update:"立即更新規則",
  164. passSec:"更新于 #t# 秒前",
  165. passMin:"更新于 #t# 分鐘前",
  166. passHour:"更新于 #t# 小時前",
  167. passDay:"更新于 #t# 天前",
  168. cantDel:"無法刪除内置規則",
  169. confirmDel:"是否確認要刪除此規則?",
  170. updateSucc:"更新成功",
  171. beginUpdate:"正在更新,請稍候",
  172. customUrls:"導入規則url,一行一條,AutoPagerize 格式規則需要以\"0|\"開頭",
  173. customRules:"輸入【東方永頁機】格式的自定義規則",
  174. save:"存儲設置",
  175. loadingText:"少女祈禱中...",
  176. opacity:"分頁隔條透明值",
  177. hideBar:"空白處雙擊隱藏分頁隔條",
  178. sortTitle:"排序在下次更新規則後生效"
  179. };
  180. break;
  181. case "ja":
  182. config = {
  183. disable: "一時的に無効にする",
  184. disableSite:"このサイト無効",
  185. enable: "ページめくりを有効にする",
  186. toTop: "トップに戻る",
  187. toBottom: "ページの下部に移動",
  188. current: "現在のページ",
  189. forceIframe: "強制ステッチ",
  190. cancelForceIframe: "強制ステッチをキャンセル",
  191. configure: "設定ページを開く",
  192. update: "今すぐルールを更新してください",
  193. passSec: "#t#秒前に更新",
  194. passMin: "#t#分前に更新",
  195. passHour: "#t#時間前に更新",
  196. passDay: "#t#日前に更新",
  197. cantDel: "組み込みルールを削除できません",
  198. confirmDel: "このルールを削除してもよろしいですか?",
  199. updateSucc: "更新に成功しました",
  200. beginUpdate: "更新中、お待ちください",
  201. customUrls: "インポートルールのURL、1行に1つ、AutoPagerizeフォーマットルールは\"0|\"で始まる必要があります",
  202. customRules: "【東方永頁機】の形式でカスタムルールを入力してください",
  203. save: "設定を保存",
  204. loadingText: "少女祈祷中...",
  205. opacity:"ページネーションバーの透明値",
  206. hideBar:"空白部分をダブルクリックして、ページ区切り文字を非表示にします",
  207. sortTitle:"並べ替えは、次のルールの更新後に有効になります"
  208. };
  209. break;
  210. default:
  211. config={
  212. disable:"Disable",
  213. disableSite:"Disable on the site",
  214. enable:"Enable",
  215. toTop:"To Top",
  216. toBottom:"To Bottom",
  217. current:"Current Page",
  218. forceIframe:"Force to join",
  219. cancelForceIframe:"Cancel Force join",
  220. configure:"Configure",
  221. update:"Update rules from url now",
  222. passSec:"Updated #t# seconds ago",
  223. passMin:"Updated #t# minutes ago",
  224. passHour:"Updated #t# hours ago",
  225. passDay:"Updated #t# days ago",
  226. cantDel:"Can't delete buildin rules",
  227. confirmDel:"Are you sure you want to delete this rule?",
  228. updateSucc:"Update succeeded",
  229. beginUpdate:"Begin update, wait a minute please",
  230. customUrls:"Import rule url, One url per line, rules on AutoPagerize format need to start with \"0|\"",
  231. customRules:"Input custom rules with [Pagetual] format",
  232. save:"Save",
  233. loadingText:"Shojo Now Loading...",
  234. opacity:"Pagination spacer opacity",
  235. hideBar:"Double-click on the blank space to hide the paging spacer",
  236. sortTitle:"Sorting takes effect after the next rule update"
  237. };
  238. break;
  239. }
  240. return config[name]?config[name].replace("#t#",param):name;
  241. };
  242.  
  243. var enableDebug=true;
  244. var debug=str=>{
  245. if(enableDebug){
  246. console.debug(str);
  247. }
  248. };
  249.  
  250. var _GM_xmlhttpRequest,_GM_registerMenuCommand,_GM_notification,_GM_addStyle,_GM_openInTab;
  251. if(typeof GM_xmlhttpRequest!='undefined'){
  252. _GM_xmlhttpRequest=GM_xmlhttpRequest;
  253. }else if(typeof GM!='undefined' && typeof GM.xmlHttpRequest!='undefined'){
  254. _GM_xmlhttpRequest=GM.xmlHttpRequest;
  255. }
  256. if(typeof GM_registerMenuCommand!='undefined'){
  257. _GM_registerMenuCommand=GM_registerMenuCommand;
  258. }else if(typeof GM!='undefined' && typeof GM.registerMenuCommand!='undefined'){
  259. _GM_registerMenuCommand=GM.registerMenuCommand;
  260. }
  261. if(typeof GM_notification!='undefined'){
  262. _GM_notification=GM_notification;
  263. }else if(typeof GM!='undefined' && typeof GM.notification!='undefined'){
  264. _GM_notification=GM.notification;
  265. }
  266. if(typeof GM_openInTab!='undefined'){
  267. _GM_openInTab=GM_openInTab;
  268. }else if(typeof GM!='undefined' && typeof GM.openInTab!='undefined'){
  269. _GM_openInTab=GM.openInTab;
  270. }
  271. if(typeof GM_addStyle!='undefined'){
  272. _GM_addStyle=GM_addStyle;
  273. }else if(typeof GM!='undefined' && typeof GM.addStyle!='undefined'){
  274. _GM_addStyle=GM.addStyle;
  275. }else{
  276. _GM_addStyle=cssStr=>{
  277. let styleEle=document.createElement("style");
  278. styleEle.innerHTML=cssStr;
  279. document.head.appendChild(styleEle);
  280. };
  281. }
  282.  
  283. if(typeof _GM_xmlhttpRequest=='undefined')_GM_xmlhttpRequest=(f)=>{};
  284. if(typeof _GM_registerMenuCommand=='undefined')_GM_registerMenuCommand=(s,f)=>{};
  285. if(typeof _GM_notification=='undefined')_GM_notification=(s)=>{};
  286. var _unsafeWindow=(typeof unsafeWindow=='undefined')?window:unsafeWindow;
  287. var storage={
  288. supportGM: typeof GM_getValue=='function' && typeof GM_getValue('a','b')!='undefined',
  289. supportGMPromise: typeof GM!='undefined' && typeof GM.getValue=='function' && typeof GM.getValue('a','b')!='undefined',
  290. mxAppStorage:(function(){
  291. try{
  292. return window.external.mxGetRuntime().storage;
  293. }catch(e){
  294. }
  295. })(),
  296. operaUJSStorage:(function(){
  297. try{
  298. return window.opera.scriptStorage;
  299. }catch(e){
  300. }
  301. })(),
  302. setItem:function(key,value){
  303. if(this.operaUJSStorage){
  304. this.operaUJSStorage.setItem(key,value);
  305. }else if(this.mxAppStorage){
  306. this.mxAppStorage.setConfig(key,value);
  307. }else if(this.supportGM){
  308. GM_setValue(key,value);
  309. if(value=="" && typeof GM_deleteValue!='undefined'){
  310. GM_deleteValue(key);
  311. }
  312. }else if(this.supportGMPromise){
  313. GM.setValue(key,value);
  314. if(value=="" && typeof GM!='undefined' && typeof GM.deleteValue!='undefined'){
  315. GM.deleteValue(key);
  316. }
  317. }else if(window.localStorage){
  318. window.localStorage.setItem(key,value);
  319. }
  320. },
  321. getItem:function(key,cb){
  322. var value;
  323. if(this.operaUJSStorage){
  324. value=this.operaUJSStorage.getItem(key);
  325. }else if(this.mxAppStorage){
  326. value=this.mxAppStorage.getConfig(key);
  327. }else if(this.supportGM){
  328. value=GM_getValue(key);
  329. }else if(this.supportGMPromise){
  330. value=GM.getValue(key).then(v=>{cb(v)});
  331. return;
  332. }else if(window.localStorage){
  333. value=window.localStorage.getItem(key);
  334. };
  335. cb(value);
  336. }
  337. };
  338. _GM_registerMenuCommand(i18n("configure"), ()=>{
  339. _GM_openInTab("https://github.com/hoothin/UserScripts/tree/master/Pagetual");
  340. });
  341.  
  342. function getElementByXpath(xpath, contextNode, doc){
  343. doc = doc || document;
  344. contextNode = contextNode || doc;
  345. try {
  346. var result = doc.evaluate(xpath, contextNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  347. return result.singleNodeValue && result.singleNodeValue.nodeType === 1 && result.singleNodeValue;
  348. } catch (err) {
  349. throw new Error(`Invalid xpath: ${xpath}`);
  350. }
  351. }
  352.  
  353. function getAllElementsByXpath(xpath, contextNode, doc){
  354. doc = doc || document;
  355. contextNode = contextNode || doc;
  356. var result = [];
  357. try {
  358. var query = doc.evaluate(xpath, contextNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  359. for (var i = 0; i < query.snapshotLength; i++) {
  360. var node = query.snapshotItem(i);
  361. if (node.nodeType === 1) result.push(node);
  362. }
  363. } catch (err) {
  364. throw new Error(`Invalid xpath: ${xpath}`);
  365. }
  366. return result;
  367. }
  368.  
  369. class RuleParser {
  370. constructor() {
  371. this.hpRules=[];
  372. this.customRules=[];
  373. this.rules=[];
  374. this.pageDoc=document;
  375. this.nextLinkHref=null;
  376. this.curUrl=location.href;
  377. this.curSiteRule={};
  378. }
  379.  
  380. initSavedRules(callback){
  381. var self=this;
  382. storage.getItem("hpRules", hpRules=>{
  383. if(hpRules)self.hpRules=hpRules;
  384. storage.getItem("customRules", customRules=>{
  385. if(customRules)self.customRules=customRules;
  386. storage.getItem("rules", rules=>{
  387. if(rules)this.rules=rules;
  388. callback();
  389. });
  390. });
  391. });
  392. }
  393.  
  394. saveCurSiteRule(){
  395. if(!this.curSiteRule || !this.curSiteRule.url || this.curSiteRule.singleUrl)return;
  396. this.hpRules=this.hpRules.filter(item=>{return item&&item.url!=this.curSiteRule.url});
  397. this.hpRules.unshift(this.curSiteRule);
  398. if(this.hpRules.length>30){
  399. this.hpRules.pop();
  400. }
  401. storage.setItem("hpRules", this.hpRules);
  402. }
  403.  
  404. requestJSON(url, callback){
  405. _GM_xmlhttpRequest({
  406. url: url,
  407. method: 'GET',
  408. onload: function(res) {
  409. let json=null;
  410. try{
  411. json=JSON.parse(res.response);
  412. }catch(e){
  413. debug(e);
  414. }
  415. callback(json);
  416. }
  417. });
  418. }
  419.  
  420. formatRule(item, type, from){
  421. switch(type){
  422. case 0:
  423. return {
  424. from:from,
  425. type:type,
  426. name:item.name,
  427. action:item.data.forceIframe=="true"?1:0,
  428. url:item.data.url,
  429. pageElement:item.data.pageElement,
  430. nextLink:item.data.nextLink,
  431. insert:item.data.insertBefore,
  432. insertPos:1,
  433. updatedAt:item.data.updated_at,
  434. css:item.data.Stylus+item.data.CSS,
  435. pageAction:item.data.bookmarklet
  436. };
  437. break;
  438. case 1:
  439. default:
  440. item.from=from;
  441. if(typeof(item.type) == "undefined")item.type=type;
  442. return item;
  443. break;
  444. }
  445. return null;
  446. }
  447.  
  448. addRuleByUrl(url, type, from, callback) {
  449. this.requestJSON(url, json=>{
  450. this.addRules(json, type, from);
  451. storage.setItem("rules", this.rules);
  452. callback();
  453. });
  454. }
  455.  
  456. addRules(rules, type, from) {
  457. if(rules && rules.length>0){
  458. this.rules=this.rules.filter(item=>{return item.from!=from});
  459. rules.forEach(item=>{
  460. let rule=this.formatRule(item, type, from);
  461. if(rule){
  462. this.rules.push(rule);
  463. }
  464. });
  465. }
  466. }
  467.  
  468. getRule(callback) {
  469. if(this.curSiteRule && this.curSiteRule.url){
  470. return this.curSiteRule;
  471. }
  472. var self=this;
  473. for(let i in this.hpRules){
  474. let rule=this.hpRules[i];
  475. if(!rule || !rule.url)continue;
  476. if(rule.singleUrl){
  477. if(location.origin+location.pathname==rule.url){
  478. this.curSiteRule=rule;
  479. debug(rule);
  480. callback();
  481. return rule;
  482. }
  483. continue;
  484. }
  485. let urlReg=new RegExp(rule.url, "i");
  486. if(urlReg.test(location.href)){
  487. if(rule.wait){
  488. let checkReady=()=>{
  489. setTimeout(()=>{
  490. if(rule.pageElement)pageElement=rule.type==0?getElementByXpath(rule.pageElement):document.querySelector(rule.pageElement);
  491. if(rule.nextLink)nextLink=rule.type==0?getElementByXpath(rule.nextLink):document.querySelector(rule.nextLink);
  492. if(rule.insert)insert=rule.type==0?getElementByXpath(rule.insert):document.querySelector(rule.insert);
  493. if((rule.pageElement && !pageElement) ||
  494. (rule.nextLink && !nextLink) ||
  495. (rule.insert && !insert)){
  496. checkReady();
  497. }else{
  498. self.curSiteRule=rule;
  499. debug(rule);
  500. callback();
  501. }
  502. },parseInt(rule.wait));
  503. };
  504. checkReady();
  505. return;
  506. }
  507. let pageElement,nextLink,insert;
  508. if(rule.pageElement)pageElement=rule.type==0?getElementByXpath(rule.pageElement):document.querySelector(rule.pageElement);
  509. if(rule.nextLink)nextLink=rule.type==0?getElementByXpath(rule.nextLink):document.querySelector(rule.nextLink);
  510. if(rule.insert)insert=rule.type==0?getElementByXpath(rule.insert):document.querySelector(rule.insert);
  511. if((rule.pageElement && !pageElement) ||
  512. (rule.nextLink && !nextLink) ||
  513. (rule.insert && !insert)){
  514. continue;
  515. }
  516. this.curSiteRule=rule;
  517. debug(rule);
  518. callback();
  519. return rule;
  520. }
  521. }
  522. for(let i in this.customRules){
  523. let rule=this.customRules[i];
  524. if(!rule || !rule.url)continue;
  525. let urlReg=new RegExp(rule.url, "i");
  526. if(urlReg.test(location.href)){
  527. if(rule.wait){
  528. let checkReady=()=>{
  529. setTimeout(()=>{
  530. if(rule.pageElement)pageElement=rule.type==0?getElementByXpath(rule.pageElement):document.querySelector(rule.pageElement);
  531. if(rule.nextLink)nextLink=rule.type==0?getElementByXpath(rule.nextLink):document.querySelector(rule.nextLink);
  532. if(rule.insert)insert=rule.type==0?getElementByXpath(rule.insert):document.querySelector(rule.insert);
  533. if((rule.pageElement && !pageElement) ||
  534. (rule.nextLink && !nextLink) ||
  535. (rule.insert && !insert)){
  536. checkReady();
  537. }else{
  538. self.curSiteRule=rule;
  539. debug(rule);
  540. callback();
  541. }
  542. },parseInt(rule.wait));
  543. };
  544. checkReady();
  545. return;
  546. }
  547. let pageElement,nextLink,insert;
  548. if(rule.pageElement)pageElement=rule.type==0?getElementByXpath(rule.pageElement):document.querySelector(rule.pageElement);
  549. if(rule.nextLink)nextLink=rule.type==0?getElementByXpath(rule.nextLink):document.querySelector(rule.nextLink);
  550. if(rule.insert)insert=rule.type==0?getElementByXpath(rule.insert):document.querySelector(rule.insert);
  551. if((rule.pageElement && !pageElement) ||
  552. (rule.nextLink && !nextLink) ||
  553. (rule.insert && !insert)){
  554. continue;
  555. }
  556. this.curSiteRule=rule;
  557. debug(rule);
  558. callback();
  559. return rule;
  560. }
  561. }
  562. let r=0;
  563. function searchByTime(){
  564. setTimeout(()=>{
  565. let end=r+80;
  566. end=end>self.rules.length?self.rules.length:end;
  567. for(;r<end;r++){
  568. let rule=self.rules[r];
  569. let urlReg=new RegExp(rule.url, "i");
  570. if(urlReg.test(location.href)){
  571. if(rule.wait){
  572. let checkReady=()=>{
  573. setTimeout(()=>{
  574. if(rule.pageElement)pageElement=rule.type==0?getElementByXpath(rule.pageElement):document.querySelector(rule.pageElement);
  575. if(rule.nextLink)nextLink=rule.type==0?getElementByXpath(rule.nextLink):document.querySelector(rule.nextLink);
  576. if(rule.insert)insert=rule.type==0?getElementByXpath(rule.insert):document.querySelector(rule.insert);
  577. if((rule.pageElement && !pageElement) ||
  578. (rule.nextLink && !nextLink) ||
  579. (rule.insert && !insert)){
  580. checkReady();
  581. }else{
  582. self.curSiteRule=rule;
  583. debug(rule);
  584. callback();
  585. }
  586. },parseInt(rule.wait));
  587. };
  588. checkReady();
  589. return;
  590. }
  591. let pageElement,nextLink,insert;
  592. if(rule.pageElement)pageElement=rule.type==0?getElementByXpath(rule.pageElement):document.querySelector(rule.pageElement);
  593. if(rule.nextLink)nextLink=rule.type==0?getElementByXpath(rule.nextLink):document.querySelector(rule.nextLink);
  594. if(rule.insert)insert=rule.type==0?getElementByXpath(rule.insert):document.querySelector(rule.insert);
  595. if((rule.pageElement && !pageElement) ||
  596. (rule.nextLink && !nextLink) ||
  597. (rule.insert && !insert)){
  598. continue;
  599. }
  600. self.curSiteRule=rule;
  601. debug(rule);
  602. callback();
  603. return;
  604. }
  605. }
  606. if(end>=self.rules.length){
  607. self.curSiteRule={};
  608. self.curSiteRule.url=location.origin+location.pathname;
  609. self.curSiteRule.singleUrl=true;
  610. callback();
  611. return;
  612. }else{
  613. searchByTime();
  614. }
  615. },20);
  616. }
  617. searchByTime();
  618. }
  619.  
  620. geneSelector(ele){
  621. let selector=ele.tagName;
  622. //Google id class都是隨機。百度更過分,style script順序都是隨機的
  623. //if(ele.id) selector += '#' + ele.id;
  624. //if(ele.classList) selector += [].map.call(ele.classList,d=>'.'+d).join('');
  625. let parent = ele.parentElement;
  626. if(parent){
  627. let i,j=0;
  628. for(i=0;i<parent.children.length;i++){
  629. if(parent.children[i].tagName==selector){
  630. j++;
  631. if(parent.children[i]==ele){
  632. break;
  633. }
  634. }
  635. }
  636. selector = this.geneSelector(parent) + ' > ' + selector + (parent.tagName=="HTML"?"":`:nth-of-type(${j})`);
  637. }
  638. return selector;
  639. }
  640.  
  641. getPageElement(doc, curWin, dontFind) {
  642. let pageElement=null;
  643. let self=this;
  644. if(this.curSiteRule.pageElement){
  645. pageElement=this.curSiteRule.type==0?getAllElementsByXpath(this.curSiteRule.pageElement,doc,doc):doc.querySelectorAll(this.curSiteRule.pageElement);
  646. }
  647. if((!pageElement || pageElement.length==0) && curWin && !dontFind){
  648. let body=doc.body;
  649. if(!body)return null;
  650. let bodyHeight=parseInt(body.scrollHeight);
  651. let curHeight=bodyHeight;
  652. function checkElement(ele){
  653. if(curHeight/bodyHeight<=0.30)return null;
  654. if(ele.children.length==0 && !self.curSiteRule.pageElement){
  655. if(ele.parentNode.tagName=="P")ele=ele.parentNode;
  656. self.curSiteRule.pageElement=self.geneSelector(ele.parentNode)+">"+ele.tagName;
  657. self.curSiteRule.type=1;
  658. debug(self.curSiteRule.pageElement);
  659. return [ele];
  660. }
  661. let i,maxHeight=curHeight*0.5,curMaxEle=null,curMaxArea=0,maxWidth=0;
  662. let isHori=true;
  663. let offsetTop=ele.children[0].offsetTop;
  664. for(i=0;i<ele.children.length;i++){
  665. if(ele.children[i].offsetTop!=offsetTop){
  666. isHori=false;
  667. break;
  668. }
  669. }
  670. for(i=0;i<ele.children.length;i++){
  671. let curNode=ele.children[i];
  672. let comStyle=curWin.getComputedStyle(curNode);
  673. let h=parseInt(curNode.scrollHeight);
  674. let w=parseInt(curNode.scrollWidth);
  675. if(isNaN(h) || isNaN(w))continue;
  676. let a=h*w+h,moreChild=curNode.children[0];
  677. while(moreChild){
  678. comStyle=curWin.getComputedStyle(moreChild);
  679. let ch=parseInt(moreChild.scrollHeight);
  680. let cw=parseInt(moreChild.scrollWidth);
  681. if(moreChild.innerText!="" && !isNaN(ch) && !isNaN(cw)){
  682. a+=ch*cw;
  683. }
  684. moreChild=moreChild.nextElementSibling;
  685. }
  686. if(curMaxEle==null || (isHori?maxWidth<w:curMaxArea<a)){
  687. if(h>0)curHeight=h;
  688. curMaxArea=a;
  689. maxWidth=w;
  690. curMaxEle=curNode;
  691. }
  692. }
  693. if(curMaxEle && (isHori || curHeight>maxHeight)){
  694. return checkElement(curMaxEle);
  695. }
  696. if(!self.curSiteRule.pageElement){
  697. if(ele.tagName=="P")ele=ele.parentNode;
  698. self.curSiteRule.pageElement=self.geneSelector(ele);
  699. if(ele.tagName!="UL")self.curSiteRule.pageElement+=">*";
  700. self.curSiteRule.type=1;
  701. debug(self.curSiteRule.pageElement);
  702. }
  703. return ele.tagName=="UL"?ele:ele.children;
  704. }
  705. pageElement=checkElement(body);
  706. if(pageElement)this.saveCurSiteRule();
  707. }
  708. return pageElement;
  709. }
  710.  
  711. getPage(doc){
  712. if(_unsafeWindow.Discourse)return {};
  713. let canSave=false;//發現頁碼選擇器在其他頁對不上,還是別保存了
  714. let url=this.curUrl.replace("#!","");
  715. let pageNum=1,preStr="",afterStr="";
  716. let pageMatch1=url.match(/(.*[a-z\/\-_](?:p|page)?\/?)(\d+)(\.s?html?$|\/?$)/i);
  717. let pageMatch2=url.match(/(.*[\?&]p(?:age)?=)(\d+)($|[#&].*)/i);
  718. if(pageMatch1){
  719. preStr=pageMatch1[1];
  720. pageNum=pageMatch1[2];
  721. afterStr=pageMatch1[3];
  722. }else if(pageMatch2){
  723. preStr=pageMatch2[1];
  724. pageNum=pageMatch2[2];
  725. afterStr=pageMatch2[3];
  726. }
  727. let curPage=doc,i,cur;
  728. let next=curPage.querySelector("a.next"),jsNext;
  729. if(!next)next=curPage.querySelector("a#next");
  730. if(!next)next=curPage.querySelector("a#rightFix");
  731. if(!next)next=curPage.querySelector("a.next_page");
  732. if(!next)next=curPage.querySelector(".next>a");
  733. if(!next)next=curPage.querySelector("#next_page");
  734. if(!next)next=curPage.querySelector(".next>button");
  735. if(!next)next=curPage.querySelector("a[alt=next]");
  736. if(!next)next=curPage.querySelector("[title=next]");
  737. if(!next)next=curPage.querySelector("[aria-label='Next page']");
  738. if(next && (!next.href || /javascript:/.test(next.href))){
  739. jsNext=next;
  740. next=null;
  741. }
  742. if(!next){
  743. let pageDiv=curPage.querySelector("div.wp-pagenavi");
  744. if(pageDiv){
  745. cur=pageDiv.querySelector("span.current");
  746. next=cur.nextSibling;
  747. }else{
  748. cur=curPage.querySelector("div.article-paging>span");
  749. if(cur){
  750. next=cur.nextElementSibling;
  751. }
  752. }
  753. }
  754. if(!next){
  755. let pageDiv=curPage.querySelector("div.pages>ul");
  756. if(pageDiv){
  757. cur=pageDiv.querySelector("li>b");
  758. if(cur)next=cur.parentNode.nextElementSibling.querySelector("a");
  759. }
  760. }
  761. if(!next){
  762. let aTags=curPage.querySelectorAll("a");
  763. let nextf,nexts,nextt,nextfo;
  764. for(i=0;i<aTags.length;i++){
  765. let aTag=aTags[i];
  766. if(nextf && nexts && nextt)break;
  767. if(!nextf){
  768. if(/(\s|^)下[一1]?[页頁张張]|^next( page)?\s*$|次のページ/i.test(aTag.innerText)){
  769. if(!aTag.href || /javascript:/.test(aTag.href)){
  770. nextfo=aTag;
  771. }else{
  772. nextf=aTag;
  773. }
  774. }
  775. }
  776. if(!nexts){
  777. if(aTag.innerText=="&gt;"){
  778. if(!aTag.href || /javascript:/.test(aTag.href)){
  779. nextfo=aTag;
  780. }else{
  781. nexts=aTag;
  782. }
  783. }
  784. }
  785. if(!aTag.href || /javascript:/.test(aTag.href))continue;
  786. if(!nextt){
  787. aTag.href=aTag.href.replace(/\?&/,"?");
  788. if(aTag.innerText=="»"){
  789. nextt=aTag;
  790. }else if(aTag.href.replace("#!","").replace(preStr,"").replace(afterStr,"")==parseInt(pageNum)+1){
  791. nextt=aTag;
  792. }else if(aTag.href.indexOf(url)!=-1 && /^[\/\?&]?[_-]?(p|page)?=?\/?2(\/|\?|&|$)/i.test(aTag.href.replace(url,"").replace(/\.s?html?$/i,""))){
  793. nextt=aTag;
  794. }
  795. }
  796. }
  797. next=nextf||nexts||nextt||nextfo;
  798. }
  799. if(!next)next=jsNext||curPage.querySelector('a[rel="next"]');
  800. return {next:next,canSave:canSave};
  801. }
  802.  
  803. getNextLink(doc) {
  804. let nextLink=null,page;
  805. if(this.curSiteRule.pageElementByJs){
  806. this.nextLinkHref="#";
  807. return true;
  808. }else if(this.curSiteRule.nextLinkByJs){
  809. try{
  810. let targetUrl=Function("doc",'"use strict";' + this.curSiteRule.nextLinkByJs)(doc);;
  811. nextLink={href:targetUrl};
  812. }catch(e){}
  813. }else if(this.curSiteRule.nextLinkByUrl){
  814. let targetUrl=this.curUrl.replace(new RegExp(this.curSiteRule.nextLinkByUrl[0]), this.curSiteRule.nextLinkByUrl[1]);
  815. if(targetUrl != this.curUrl){
  816. let reps=targetUrl.match(/{.*?}/g);
  817. if(reps){
  818. reps.forEach(rep=>{
  819. let code=rep.replace("{","").replace("}","");
  820. let result=code.match(/^(\d+)\+1$/);
  821. if(result){
  822. result=parseInt(result[1])+1;
  823. }else{
  824. try{
  825. result=Function('"use strict";return ' + code)();;
  826. }catch(e){}
  827. }
  828. targetUrl=targetUrl.replace(rep, result);
  829. });
  830. }
  831. }
  832. nextLink={href:targetUrl};
  833. }else if(this.curSiteRule.nextLink){
  834. nextLink=this.curSiteRule.type==0?getElementByXpath(this.curSiteRule.nextLink,doc,doc):doc.querySelector(this.curSiteRule.nextLink);
  835. }
  836. if(!nextLink){
  837. page=this.getPage(doc);
  838. nextLink=page.next;
  839. if(nextLink && doc==document){
  840. let nextLinkCs=_unsafeWindow.getComputedStyle(nextLink);
  841. if(nextLinkCs.cursor=="not-allowed"){
  842. self.nextLinkHref=false;
  843. return null;
  844. }
  845. }
  846. }
  847. if(nextLink){
  848. if(!this.curSiteRule.nextLink && page && page.canSave){
  849. this.curSiteRule.nextLink=this.geneSelector(nextLink);
  850. this.curSiteRule.type=1;
  851. this.saveCurSiteRule();
  852. }
  853. }
  854. if(nextLink){
  855. this.nextLinkHref=nextLink.href?nextLink.href:"#";
  856. debug(nextLink);
  857. }else{
  858. this.nextLinkHref=false;
  859. }
  860. return nextLink;
  861. }
  862.  
  863. getInsert(refresh) {
  864. if(this.insert && !refresh){
  865. let parent=this.insert;
  866. while(parent && parent.nodeName!="BODY"){
  867. parent=parent.parentNode;
  868. }
  869. if(parent && parent.nodeName=="BODY"){
  870. return this.insert;
  871. }
  872. }
  873. if(this.curSiteRule.insert){
  874. this.insert=this.curSiteRule.type==0?getElementByXpath(this.curSiteRule.insert,document):document.querySelector(this.curSiteRule.insert);
  875. }else{
  876. let pageElement=this.getPageElement(document, _unsafeWindow);
  877. if(pageElement && pageElement.length>0){
  878. var pELast = pageElement[pageElement.length - 1];
  879. this.insert = pELast.nextSibling ? pELast.nextSibling : pELast.parentNode.appendChild(document.createTextNode(' '));
  880. }
  881. }
  882. return this.insert;
  883. }
  884.  
  885. pageAction(doc,eles){
  886. let code=this.curSiteRule.pageAction;
  887. if(code){
  888. try{
  889. Function("doc","eles",'"use strict";' + code)(doc,eles);
  890. }catch(e){}
  891. }
  892. let css=this.curSiteRule.css;
  893. if(css){
  894. _GM_addStyle(css);
  895. }
  896. [].forEach.call(eles, ele=>{
  897. [].forEach.call(ele.querySelectorAll("img"), img=>{
  898. //if(img.src==""){
  899. let realSrc;
  900. if(img.dataset && img.dataset.original){
  901. realSrc=img.dataset.original;
  902. }else if(img.dataset && img.dataset.src){
  903. realSrc=img.dataset.src;
  904. }else if(img._lazyrias && img._lazyrias.srcset){
  905. realSrc=img._lazyrias.srcset[img._lazyrias.srcset.length-1];
  906. }else if(img.dataset && img.dataset.origFile){
  907. realSrc=img.dataset.origFile;
  908. }else if(img.srcset){
  909. var srcs=img.srcset.split(","),largeSize=0;
  910. srcs.forEach(srci=>{
  911. let srcInfo=srci.trim().split(" "),curSize=parseInt(srcInfo[1]);
  912. if(srcInfo[1] && curSize>largeSize){
  913. largeSize=curSize;
  914. realSrc=srcInfo[0];
  915. }
  916. });
  917. }
  918. if(realSrc)img.src=realSrc;
  919. //}
  920. });
  921. [].forEach.call(ele.querySelectorAll("a.lazyload"), a=>{
  922. if(a.dataset.original){
  923. a.style.backgroundImage='url("'+a.dataset.original+'")';
  924. }
  925. });
  926. });
  927. let lazyImgSrc=this.curSiteRule.lazyImgSrc;
  928. if(lazyImgSrc){
  929. [].forEach.call(eles, ele=>{
  930. [].forEach.call(ele.querySelectorAll("img"), img=>{
  931. if(img[lazyImgSrc]){
  932. img.src=img[lazyImgSrc];
  933. }
  934. });
  935. });
  936. }
  937. }
  938.  
  939. initPage(callback){
  940. let self=this;
  941. curPage=1;
  942. //if(this.curSiteRule.url && !this.curSiteRule.singleUrl)return;
  943. this.curSiteRule={};
  944. this.pageDoc=document;
  945. this.nextLinkHref=null;
  946. this.curUrl=location.href;
  947. this.getRule(()=>{
  948. if(self.curSiteRule.enable==0){
  949. isPause=true;
  950. return;
  951. }
  952. if(self.curSiteRule && !self.curSiteRule.singleUrl){
  953. self.hpRules=self.hpRules.filter(item=>{return item&&item.url!=self.curSiteRule.url});
  954. self.hpRules.unshift(self.curSiteRule);
  955. if(self.hpRules.length>30){
  956. self.hpRules.pop();
  957. }
  958. storage.setItem("hpRules", self.hpRules);
  959. }
  960. let code=self.curSiteRule.init;
  961. if(code){
  962. try{
  963. Function('"use strict";' + code)();
  964. }catch(e){}
  965. }
  966. self.getNextLink(document);
  967. callback();
  968. });
  969. }
  970.  
  971. insertPage(doc, eles, url, callback, tried){
  972. this.pageDoc=doc;
  973. this.curUrl=url;
  974. this.pageAction(doc, eles);
  975. this.getNextLink(doc);
  976. if(curPage==1 && !tried && !this.nextLinkHref && this.curSiteRule.singleUrl && this.curSiteRule.pageElement){
  977. this.curSiteRule.action=1;
  978. return false;
  979. }
  980. if(callback)callback(eles);
  981. this.getInsert();
  982. var self=this;
  983. if(!eles || eles.length==0 || !self.insert || !self.insert.parentNode){
  984. }else{
  985. [].forEach.call(eles, ele=>{
  986. if(self.curSiteRule.insertPos==2){
  987. self.insert.appendChild(ele.cloneNode(true));
  988. }else{
  989. self.insert.parentNode.insertBefore(ele.cloneNode(true), self.insert);
  990. }
  991. });
  992. }
  993. return true;
  994. }
  995. }
  996. var ruleParser = new RuleParser();
  997.  
  998. var rulesData={},ruleUrls,updateDate;
  999. function initConfig(){
  1000. _GM_registerMenuCommand(i18n(forceState==1?"enable":"disableSite"), ()=>{
  1001. storage.setItem("forceState_"+location.host, (forceState==1?"":1));
  1002. location.reload();
  1003. });
  1004.  
  1005. _GM_registerMenuCommand(i18n(forceState==2?"cancelForceIframe":"forceIframe"), ()=>{
  1006. storage.setItem("forceState_"+location.host, (forceState==2?"":2));
  1007. location.reload();
  1008. });
  1009. var configCon,insertPos;
  1010. if(location.href=="https://github.com/hoothin/UserScripts/tree/master/Pagetual"){
  1011. _GM_addStyle(`
  1012. p>span:nth-child(1),p>span:nth-child(2),p>span:nth-child(3){
  1013. cursor: pointer;
  1014. user-select: none;
  1015. }
  1016. p>span:nth-child(1):hover,p>span:nth-child(2):hover,p>span:nth-child(3):hover{
  1017. color:red;
  1018. }
  1019. .updateDate{
  1020. cursor: pointer;
  1021. user-select: none;
  1022. }
  1023. .updateDate:hover{
  1024. color:red;
  1025. }
  1026. `);
  1027. configCon=document.querySelector(".markdown-body");
  1028. insertPos=configCon.querySelector("hr");
  1029. }else if(location.href=="https://github.com/hoothin/UserScripts/tree/master/Pagetual"){
  1030. }else return;
  1031. class Rulebar {
  1032. init(ruleUrl){
  1033. this.ruleUrl=ruleUrl;
  1034. this.item=document.createElement("p");
  1035. this.item.title=ruleUrl.type==0?"AutoPagerize Rules":"Pagetual Rules";
  1036. this.item.dataset.id=this.ruleUrl.id;
  1037. let url=document.createElement("span");
  1038. url.innerHTML=ruleUrl.url;
  1039. let up=document.createElement("span");
  1040. up.innerHTML="↑ ";
  1041. up.title=i18n("sortTitle");
  1042. let down=document.createElement("span");
  1043. down.innerHTML="↓ ";
  1044. down.title=i18n("sortTitle");
  1045. let del=document.createElement("span");
  1046. del.innerHTML="× ";
  1047. up.onclick=e=>{
  1048. this.moveUp();
  1049. };
  1050. down.onclick=e=>{
  1051. this.moveDown();
  1052. };
  1053. del.onclick=e=>{
  1054. this.del();
  1055. };
  1056. this.item.appendChild(up);
  1057. this.item.appendChild(down);
  1058. this.item.appendChild(del);
  1059. this.item.appendChild(url);
  1060. configCon.insertBefore(this.item, insertPos);
  1061. }
  1062. saveSort(){
  1063. let sort=[];
  1064. [].forEach.call(this.item.parentNode.querySelectorAll("p[data-id]"), i=>{
  1065. sort.push(i.dataset.id);
  1066. });
  1067. rulesData.sort=sort;
  1068. storage.setItem("rulesData", rulesData);
  1069. }
  1070. moveUp(){
  1071. let preE=this.item.previousElementSibling;
  1072. if(preE.tagName=="P" && preE.children.length>1){
  1073. this.item.parentNode.insertBefore(this.item,preE);
  1074. this.saveSort();
  1075. }
  1076. }
  1077. moveDown(){
  1078. let nextE=this.item.nextElementSibling;
  1079. if(nextE.tagName=="P" && nextE.children.length>1){
  1080. this.item.parentNode.insertBefore(nextE,this.item);
  1081. this.saveSort();
  1082. }
  1083. }
  1084. del(){
  1085. if(this.ruleUrl.id<2){
  1086. alert(i18n("cantDel"));
  1087. }else if(window.confirm(i18n("confirmDel"))){
  1088. for(let u=0;u<rulesData.urls.length;u++){
  1089. if(this.ruleUrl.id==rulesData.urls[u].id){
  1090. rulesData.urls.splice(u,1);
  1091. break;
  1092. }
  1093. }
  1094. for(let u=0;u<rulesData.sort.length;u++){
  1095. if(this.ruleUrl.id==rulesData.sort[u]){
  1096. rulesData.sort.splice(u,1);
  1097. break;
  1098. }
  1099. }
  1100. storage.setItem("rulesData", rulesData);
  1101. ruleParser.rules=ruleParser.rules.filter(item=>{return item.from!=this.ruleUrl.id});
  1102. storage.setItem("rules", ruleParser.rules);
  1103. this.item.parentNode.removeChild(this.item);
  1104. //location.reload();
  1105. }
  1106. }
  1107. }
  1108. let updateP=document.createElement("p"),i=0;
  1109. let now=new Date().getTime(),inUpdate=false;
  1110.  
  1111.  
  1112. let pastDate=(new Date(updateDate)).toString(),passStr;
  1113. let passTime=(now-updateDate)/1000;
  1114. if(passTime<60){
  1115. passStr=i18n("passSec", passTime);
  1116. }else if(passTime<60*60){
  1117. passStr=i18n("passMin", parseInt(passTime/60));
  1118. }else if(passTime<60*60*24){
  1119. passStr=i18n("passHour", parseInt(passTime/3600));
  1120. }else{
  1121. passStr=i18n("passDay", parseInt(passTime/86400));
  1122. }
  1123.  
  1124.  
  1125. updateP.className="updateDate";
  1126. updateP.innerHTML=passStr;
  1127. updateP.title=i18n("update")+" - "+pastDate;
  1128. updateP.onclick=e=>{
  1129. if(inUpdate)return;
  1130. inUpdate=true;
  1131. let ruleIndex=0;
  1132. storage.setItem("hpRules", []);
  1133. function addNextRule(){
  1134. if(ruleIndex>=ruleUrls.length){
  1135. storage.setItem("ruleLastUpdate", now);
  1136. alert(i18n("updateSucc"));
  1137. inUpdate=false;
  1138. updateP.innerHTML=i18n("passSec", 0);
  1139. updateP.title=i18n("update");
  1140. }else{
  1141. let rule=ruleUrls[ruleIndex++];
  1142. ruleParser.addRuleByUrl(rule.url, rule.type, rule.id, ()=>{
  1143. addNextRule();
  1144. })
  1145. }
  1146. }
  1147. addNextRule();
  1148. alert(i18n("beginUpdate"));
  1149. };
  1150. configCon.insertBefore(updateP, insertPos);
  1151. if(ruleUrls){
  1152. ruleUrls.forEach(ruleUrl=>{
  1153. var rulebar=new Rulebar();
  1154. rulebar.init(ruleUrl);
  1155. });
  1156. }
  1157. let customUrlsTitle=document.createElement("h2");
  1158. customUrlsTitle.innerHTML=i18n("customUrls");
  1159. configCon.insertBefore(customUrlsTitle, insertPos);
  1160. let customUrlsInput=document.createElement("textarea");
  1161. customUrlsInput.style.width="100%";
  1162. customUrlsInput.placeholder="0|http://wedata.net/databases/AutoPagerize/items_all.json";
  1163. configCon.insertBefore(customUrlsInput, insertPos);
  1164.  
  1165. let opacityTitle=document.createElement("h2");
  1166. opacityTitle.innerHTML=i18n("opacity");
  1167. let opacityInput=document.createElement("input");
  1168. opacityInput.value=rulesData.opacity*100;
  1169. opacityInput.type="number";
  1170. opacityInput.style.width="50px";
  1171. opacityInput.style.margin="0 0 0 10px";
  1172. opacityTitle.appendChild(opacityInput);
  1173. configCon.insertBefore(opacityTitle, insertPos);
  1174.  
  1175.  
  1176. let hideBarTitle=document.createElement("h2");
  1177. hideBarTitle.innerHTML=i18n("hideBar");
  1178. let hideBarInput=document.createElement("input");
  1179. hideBarInput.type="checkbox";
  1180. hideBarInput.style.width="50px";
  1181. hideBarInput.style.height="20px";
  1182. hideBarInput.checked=rulesData.hideBar;
  1183. hideBarTitle.appendChild(hideBarInput);
  1184. configCon.insertBefore(hideBarTitle, insertPos);
  1185.  
  1186. let customRulesTitle=document.createElement("h2");
  1187. customRulesTitle.innerHTML=i18n("customRules")
  1188. configCon.insertBefore(customRulesTitle, insertPos);
  1189. let customRulesInput=document.createElement("textarea");
  1190. customRulesInput.style.width="100%";
  1191. customRulesInput.style.height="500px";
  1192. 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]`;
  1193. customRulesInput.value=getFormatJSON(ruleParser.customRules);
  1194. configCon.insertBefore(customRulesInput, insertPos);
  1195. let saveBtn=document.createElement("button");
  1196. saveBtn.innerHTML=i18n("save");
  1197. saveBtn.style.width="100%";
  1198. configCon.insertBefore(saveBtn, insertPos);
  1199. saveBtn.onclick=e=>{
  1200. try{
  1201. storage.setItem("hpRules", []);
  1202. if(customRulesInput.value==""){
  1203. storage.setItem("customRules", "");
  1204. }else{
  1205. let customRules=JSON.parse(customRulesInput.value);
  1206. debug(customRules);
  1207. storage.setItem("customRules", customRules);
  1208. }
  1209. }catch(e){
  1210. debug(e);
  1211. alert("JSON error, check again!");
  1212. return;
  1213. }
  1214. rulesData.opacity=opacityInput.value/100;
  1215. rulesData.hideBar=hideBarInput.checked;
  1216. storage.setItem("rulesData", rulesData);
  1217. let customUrls=customUrlsInput.value.trim();
  1218. if(customUrls){
  1219. customUrls=customUrls.split(/\n/);
  1220. for(let c=0;c<customUrls.length;c++){
  1221. let urlArr=customUrls[c].split("|"),url,type=1;
  1222. if(urlArr.length==1){
  1223. url=urlArr[0].trim();
  1224. if(!/^http/.test(url)){
  1225. alert("Wrong url, check again!");
  1226. return;
  1227. }
  1228. }else if(urlArr.length==2){
  1229. type=urlArr[0].trim();
  1230. url=urlArr[1].trim();
  1231. if(!/^http/.test(url)){
  1232. alert("Wrong url, check again!");
  1233. return;
  1234. }
  1235. }else{
  1236. break;
  1237. }
  1238. let maxId=0,hasUrl=false;;
  1239. if(!rulesData.urls){
  1240. rulesData.urls=[];
  1241. maxId=1;
  1242. }else{
  1243. rulesData.urls.forEach(u=>{
  1244. if(maxId<u.id){
  1245. maxId=u.id;
  1246. }
  1247. if(u.url==url){
  1248. hasUrl=true;
  1249. }
  1250. });
  1251. if(hasUrl)break;
  1252. }
  1253. rulesData.urls.push({id:maxId+1,url:url,type:type});
  1254. rulesData.sort.push(maxId+1);
  1255. storage.setItem("rulesData", rulesData);
  1256. }
  1257. }
  1258. alert("Modified successfully");
  1259. location.reload();
  1260. };
  1261. }
  1262.  
  1263. function objIsArr(obj) {
  1264. return obj &&
  1265. typeof obj === 'object' &&
  1266. typeof obj.length === 'number' &&
  1267. !(obj.propertyIsEnumerable('length'));
  1268. }
  1269.  
  1270. function getFormatJSON(obj){
  1271. if(!objIsArr(obj))return "";
  1272. let ret="[\n";
  1273. let len=obj.length,i=0,isLast;
  1274. obj.forEach(item=>{
  1275. ret+=" {\n";
  1276. let iLen=Object.keys(item).length,j=0;
  1277. for(let key in item){
  1278. isLast=(++j)==iLen;
  1279. let value=item[key];
  1280. if(objIsArr(value)){
  1281. let vstr="[",v=0,vIsLast=false;
  1282. value.forEach(vi=>{
  1283. vIsLast=(++v)==value.length;
  1284. vstr+="\""+vi.replace(/\\/g,"\\\\")+"\""+(vIsLast?"":",");
  1285. });
  1286. vstr+="]";
  1287. ret+=" \""+key+"\":"+vstr+""+(isLast?"":",")+"\n";
  1288. }else{
  1289. if(typeof value=="string"){
  1290. value=value.replace(/\\/g,"\\\\");
  1291. }
  1292. ret+=" \""+key+"\":\""+value+"\""+(isLast?"":",")+"\n";
  1293. }
  1294. }
  1295. isLast=(++i)==len;
  1296. ret+=" }"+(isLast?"":",")+"\n";
  1297. });
  1298. ret+="]";
  1299. return ret;
  1300. }
  1301.  
  1302. function initRules(callback) {
  1303. /*0 wedata格式,1 pagetual格式*/
  1304. ruleUrls=[
  1305. {
  1306. id:0,
  1307. url:'https://raw.githubusercontent.com/hoothin/UserScripts/master/Pagetual/pagetualRules.json',
  1308. type:1
  1309. },
  1310. {
  1311. id:1,
  1312. url:'http://wedata.net/databases/AutoPagerize/items_all.json',
  1313. type:0,
  1314. }
  1315. ];var i=0,j=0;
  1316.  
  1317. ruleParser.initSavedRules(()=>{
  1318. storage.getItem("rulesData", data=>{
  1319. if(data){
  1320. rulesData=data;
  1321. if(data.urls)ruleUrls=ruleUrls.concat(data.urls);
  1322. if(data.sort){
  1323. let urls=[];
  1324. data.sort.forEach(id=>{
  1325. for(let s=0;s<ruleUrls.length;s++){
  1326. if(id==ruleUrls[s].id){
  1327. urls.push(ruleUrls[s]);
  1328. break;
  1329. }
  1330. }
  1331. });
  1332. ruleUrls=urls;
  1333. }
  1334. }
  1335. if(typeof(rulesData.opacity)=="undefined"){
  1336. rulesData.opacity=0.3;
  1337. }
  1338. if(typeof(rulesData.hideBar)=="undefined"){
  1339. rulesData.hideBar=true;
  1340. }
  1341. storage.getItem("forceState_"+location.host, v=>{
  1342. storage.getItem("ruleLastUpdate", date=>{
  1343. forceState=v||0;
  1344. updateDate=date;
  1345. initConfig();
  1346. if(forceState==1)return;
  1347. let now=new Date().getTime();
  1348. if(!date || now-date>2*24*60*60*1000){
  1349. storage.setItem("ruleLastUpdate", now);
  1350. storage.setItem("hpRules", []);
  1351. let ruleIndex=0;
  1352. function addNextRule(){
  1353. if(ruleIndex>=ruleUrls.length){
  1354. callback();
  1355. }else{
  1356. let rule=ruleUrls[ruleIndex++];
  1357. ruleParser.addRuleByUrl(rule.url, rule.type, rule.id, ()=>{
  1358. addNextRule();
  1359. })
  1360. }
  1361. }
  1362. addNextRule();
  1363. }else{
  1364. callback();
  1365. }
  1366. });
  1367. });
  1368. });
  1369. });
  1370. }
  1371.  
  1372. function requestDoc(url, callback){
  1373. _GM_xmlhttpRequest({
  1374. url: url,
  1375. method: 'GET',
  1376. overrideMimeType:"text/html;charset="+document.charset,
  1377. onload: function(res) {
  1378. var doc=null;
  1379. try {
  1380. doc=document.implementation.createHTMLDocument('');
  1381. doc.documentElement.innerHTML=res.response;
  1382. }
  1383. catch (e) {
  1384. debug('parse error'+e.toString());
  1385. }
  1386. let pageElement=ruleParser.getPageElement(doc);
  1387. //只有1的話怕不是圖片哦
  1388. if(pageElement && (pageElement.length>1 || (pageElement.length==1 && pageElement[0].tagName!="IMG") )){
  1389. let result=ruleParser.insertPage(doc, pageElement, url, callback, false);
  1390. if(!result){
  1391. requestFromIframe(url, (doc, eles)=>{
  1392. if(eles){
  1393. ruleParser.insertPage(doc, eles, url, callback, true);
  1394. }
  1395. });
  1396. }
  1397. }else if(ruleParser.curSiteRule.singleUrl){
  1398. requestFromIframe(url, (doc, eles)=>{
  1399. if(eles){
  1400. ruleParser.insertPage(doc, eles, url, callback, true);
  1401. }
  1402. });
  1403. }
  1404. },
  1405. onerror: function(e){
  1406. debug(e);
  1407. callback(false);
  1408. }
  1409. });
  1410. }
  1411.  
  1412. var failFromIframe=0;
  1413. function requestFromIframe(url, callback){
  1414. let orgPage,curPage;
  1415. let iframe = document.createElement('iframe');
  1416. iframe.name = 'pagetual-iframe';
  1417. iframe.width = '100%';
  1418. iframe.height = '0';
  1419. iframe.frameBorder = '0';
  1420. iframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  1421. iframe.style.cssText = 'margin:0!important;padding:0!important;visibility:hidden!important;';
  1422. iframe.addEventListener("load", e=>{
  1423. setTimeout(()=>{
  1424. let tryTimes=0;
  1425. function checkIframe(){
  1426. try{
  1427. let doc=iframe.contentDocument || iframe.contentWindow.document;
  1428. let eles=ruleParser.getPageElement(doc, iframe.contentWindow);
  1429. if(eles && eles.length>0){
  1430. callback(doc, eles);
  1431. }else if(tryTimes++ < 2){
  1432. setTimeout(()=>{
  1433. checkIframe();
  1434. },500);
  1435. return;
  1436. }else{
  1437. if(failFromIframe++ > 2){
  1438. failFromIframe=0;
  1439. isPause=true;
  1440. callback(false, false);
  1441. }else{
  1442. callback(false, false);
  1443. }
  1444. }
  1445. }catch(e){
  1446. isPause=true;
  1447. callback(false, false);
  1448. }
  1449. document.body.removeChild(iframe);
  1450. }
  1451. checkIframe();
  1452. },300);
  1453. });
  1454. iframe.src=url;
  1455. document.body.appendChild(iframe);
  1456. }
  1457.  
  1458. function initPage(){
  1459. ruleParser.initPage(()=>{
  1460. initListener();
  1461. nextPage();
  1462. });
  1463. }
  1464.  
  1465. function initView(){
  1466. _GM_addStyle(`
  1467. .pagetual_pageBar.stop {
  1468. -webkit-filter: invert(100%);
  1469. filter: invert(100%);
  1470. opacity: 1!important;
  1471. }
  1472. .pagetual_pageBar.hide {
  1473. display: none!important;
  1474. }
  1475. .pagetual_pageBar:hover {
  1476. opacity: 1!important;
  1477. }
  1478. .pagetual_pageBar>span {
  1479. vertical-align: super;
  1480. }
  1481.  
  1482. .pagetual_pageBar>span>svg:hover {
  1483. animation: touhouAni 1s infinite;
  1484. }
  1485.  
  1486. @keyframes touhouAni{
  1487. from {transform: rotate(0deg) scale3d(1.2, 1.2, 1.2);}
  1488. to {transform: rotate(360deg);}
  1489. }
  1490. @-webkit-keyframes touhouAni{
  1491. from {transform: rotate(0deg) scale3d(1.2, 1.2, 1.2);}
  1492. to {transform: rotate(360deg);}
  1493. }
  1494. `);
  1495. pageBarStyle=`opacity:${rulesData.opacity};display:${rulesData.opacity==0?"none":"block"};padding:0;box-shadow: 0px 0px 10px 0px #000000aa;border-radius: 20px;background-color: rgb(240 240 240 / 80%);visibility: visible; position: initial; width: auto; height: 30px; float: none; clear: both; margin: 5px auto; text-align: center;`;
  1496. }
  1497. var loading=document.createElement("div");
  1498. loading.style.cssText="display:none;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;";
  1499. loading.innerHTML=`<p style=" display: block; position: initial; margin: auto auto 5px auto; shape-rendering: auto; vertical-align: middle; visibility: visible; width: initial; height: initial; text-align: center; color: #6e6e6e; ">${i18n("loadingText")}</p><svg width="120" height="30" viewBox="0 0 120 30" fill="#000000A0" style="display: block;position: initial;margin: auto;shape-rendering: auto;vertical-align: middle;visibility: visible;width: initial;height: initial;"><circle cx="15" cy="15" r="15"><animate attributeName="r" from="15" to="15" begin="0s" dur="0.8s" values="15;9;15" calcMode="linear" repeatCount="indefinite"></animate><animate attributeName="fill-opacity" from="1" to="1" begin="0s" dur="0.8s" values="1;.5;1" calcMode="linear" repeatCount="indefinite"></animate></circle><circle cx="60" cy="15" r="9" fill-opacity="0.3"><animate attributeName="r" from="9" to="9" begin="0s" dur="0.8s" values="9;15;9" calcMode="linear" repeatCount="indefinite"></animate><animate attributeName="fill-opacity" from="0.5" to="0.5" begin="0s" dur="0.8s" values=".5;1;.5" calcMode="linear" repeatCount="indefinite"></animate></circle><circle cx="105" cy="15" r="15"><animate attributeName="r" from="15" to="15" begin="0s" dur="0.8s" values="15;9;15" calcMode="linear" repeatCount="indefinite"></animate><animate attributeName="fill-opacity" from="1" to="1" begin="0s" dur="0.8s" values="1;.5;1" calcMode="linear" repeatCount="indefinite"></animate></circle></svg>`;
  1500. document.body.appendChild(loading);
  1501.  
  1502. var upSvg=`<svg 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>`;
  1503. var downSvg=`<svg 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>`;
  1504. var pageBarStyle;
  1505. var initStyle=`right: unset;left: unset;top: unset;bottom: unset;inset: unset;clear: both;cy: initial;d: initial;dominant-baseline: initial;empty-cells: initial;fill: initial;fill-opacity: initial;fill-rule: initial;filter: initial;flex: initial;flex-flow: initial;float: initial;flood-color: initial;flood-opacity: initial;grid: initial;grid-area: initial;height: initial;hyphens: initial;image-orientation: initial;image-rendering: initial;inline-size: initial;inset-block: initial;inset-inline: initial;isolation: initial;letter-spacing: initial;lighting-color: initial;line-break: initial;list-style: initial;margin-block: initial;margin: 0px auto;margin-inline: initial;marker: initial;mask: initial;mask-type: initial;max-block-size: initial;max-height: initial;max-inline-size: initial;max-width: initial;min-block-size: initial;min-height: initial;min-inline-size: initial;min-width: initial;mix-blend-mode: initial;object-fit: initial;object-position: initial;offset: initial;opacity: initial;order: initial;orphans: initial;outline: initial;outline-offset: initial;overflow-anchor: initial;overflow-clip-margin: initial;overflow-wrap: initial;overflow: initial;overscroll-behavior-block: initial;overscroll-behavior-inline: initial;overscroll-behavior: initial;padding-block: initial;padding: initial;padding-inline: initial;page: initial;page-orientation: initial;paint-order: initial;perspective: initial;perspective-origin: initial;pointer-events: initial;position: initial;quotes: initial;r: initial;resize: initial;ruby-position: initial;rx: initial;ry: initial;scroll-behavior: initial;scroll-margin-block: initial;scroll-margin: initial;scroll-margin-inline: initial;scroll-padding-block: initial;scroll-padding: initial;scroll-padding-inline: initial;scroll-snap-align: initial;scroll-snap-stop: initial;scroll-snap-type: initial;scrollbar-gutter: initial;shape-image-threshold: initial;shape-margin: initial;shape-outside: initial;shape-rendering: initial;size: initial;speak: initial;stop-color: initial;stop-opacity: initial;stroke: initial;stroke-dasharray: initial;stroke-dashoffset: initial;stroke-linecap: initial;stroke-linejoin: initial;stroke-miterlimit: initial;stroke-opacity: initial;stroke-width: initial;tab-size: initial;table-layout: initial;text-align: initial;text-align-last: initial;text-anchor: initial;text-combine-upright: initial;text-decoration: initial;text-decoration-skip-ink: initial;text-indent: initial;text-overflow: initial;text-shadow: initial;text-size-adjust: initial;text-transform: initial;text-underline-offset: initial;text-underline-position: initial;touch-action: initial;transform: initial;transform-box: initial;transform-origin: initial;transform-style: initial;transition: initial;user-select: initial;vector-effect: initial;vertical-align: initial;visibility: initial;border-spacing: initial;-webkit-border-image: initial;-webkit-box-align: initial;-webkit-box-decoration-break: initial;-webkit-box-direction: initial;-webkit-box-flex: initial;-webkit-box-ordinal-group: initial;-webkit-box-orient: initial;-webkit-box-pack: initial;-webkit-box-reflect: initial;-webkit-highlight: initial;-webkit-hyphenate-character: initial;-webkit-line-break: initial;-webkit-line-clamp: initial;-webkit-mask-box-image: initial;-webkit-mask: initial;-webkit-mask-composite: initial;-webkit-perspective-origin-x: initial;-webkit-perspective-origin-y: initial;-webkit-print-color-adjust: initial;-webkit-rtl-ordering: initial;-webkit-ruby-position: initial;-webkit-tap-highlight-color: initial;-webkit-text-combine: initial;-webkit-text-decorations-in-effect: initial;-webkit-text-emphasis: initial;-webkit-text-emphasis-position: initial;-webkit-text-fill-color: initial;-webkit-text-security: initial;-webkit-text-stroke: initial;-webkit-transform-origin-x: initial;-webkit-transform-origin-y: initial;-webkit-transform-origin-z: initial;-webkit-user-drag: initial;-webkit-user-modify: initial;white-space: initial;widows: initial;width: initial;will-change: initial;word-break: initial;word-spacing: initial;x: initial;y: initial;`;
  1506. 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);`;
  1507.  
  1508. var isPause=false,isLoading=false,curPage=1,forceState=0,bottomGap=1000;
  1509.  
  1510. function changeStop(stop, hide){
  1511. isPause=stop;
  1512. [].forEach.call(document.querySelectorAll(".pagetual_pageBar"), bar=>{
  1513. if(isPause){
  1514. bar.classList.add("stop");
  1515. if(hide)bar.classList.add("hide");
  1516. }else{
  1517. bar.classList.remove("stop");
  1518. if(hide)bar.classList.remove("hide");
  1519. }
  1520. });
  1521. }
  1522.  
  1523. function isInViewPort(element) {
  1524. const viewWidth = window.innerWidth || document.documentElement.clientWidth;
  1525. const viewHeight = window.innerHeight || document.documentElement.clientHeight;
  1526. const {
  1527. top,
  1528. right,
  1529. bottom,
  1530. left,
  1531. } = element.getBoundingClientRect();
  1532.  
  1533. return (
  1534. top >= 0 &&
  1535. left >= 0 &&
  1536. right <= viewWidth &&
  1537. bottom <= viewHeight
  1538. );
  1539. }
  1540.  
  1541. function initListener(){
  1542. var urlChanged=false;
  1543. var _wr = function(type) {
  1544. var orig = history[type];
  1545. return function() {
  1546. var rv = orig.apply(this, arguments);
  1547. var e = new Event(type);
  1548. e.arguments = arguments;
  1549. window.dispatchEvent(e);
  1550. return rv;
  1551. };
  1552. };
  1553. history.pushState = _wr('pushState');
  1554. window.addEventListener('pushState', function(e) {
  1555. urlChanged=true;
  1556. });
  1557. let loadmoreBtn,buttons=document.querySelectorAll("input,button,a"),loadmoreReg=/^\s*(加载更多|加載更多|load\s*more|もっと読み込む)\s*$/i,loading=true;
  1558. for(let i=0;i<buttons.length;i++){
  1559. let button=buttons[i];
  1560. if(button && loadmoreReg.test(button.innerText)){
  1561. loadmoreBtn=button;
  1562. loadmoreBtn.click();
  1563. break;
  1564. }
  1565. }
  1566. if(loadmoreBtn)setTimeout(()=>{loading=false},2000);
  1567. document.addEventListener('scroll', e=>{
  1568. if(urlChanged){
  1569. ruleParser.initPage(()=>{});
  1570. initView();
  1571. urlChanged=false;
  1572. }
  1573. if(!loading && loadmoreBtn && isInViewPort(loadmoreBtn)){
  1574. loadmoreBtn.click();
  1575. loading=true;
  1576. setTimeout(()=>{loading=false},2000);
  1577. }
  1578. setTimeout(()=>{
  1579. if(!isPause && !isLoading){
  1580. let scrolly=window.scrollY;
  1581. let windowHeight=window.innerHeight;
  1582. let scrollH=Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
  1583. if(scrollH-scrolly-windowHeight<bottomGap){
  1584. nextPage();
  1585. }
  1586. }
  1587. },100);
  1588. }, true);
  1589. document.addEventListener('dblclick', e=>{
  1590. setTimeout(()=>{
  1591. changeStop(!isPause, rulesData.hideBar);
  1592. if(!isPause){
  1593. nextPage();
  1594. }
  1595. },200);
  1596. });
  1597. }
  1598.  
  1599. function createPageBar(url){
  1600. let insert=ruleParser.getInsert();
  1601. if(!insert || !insert.parentNode)return;
  1602. curPage++;
  1603. let inTable=insert.tagName=="TR" ||
  1604. (insert.previousElementSibling && insert.previousElementSibling.tagName=="TR") ||
  1605. insert.tagName=="TBODY" || (insert.previousElementSibling && insert.previousElementSibling.tagName=="TBODY");
  1606. let inLi=insert.tagName=="LI" || (insert.previousElementSibling && insert.previousElementSibling.tagName=="LI");
  1607. let pageBar=document.createElement(inTable?"tr":"div");
  1608. let upSpan=document.createElement("span");
  1609. let downSpan=document.createElement("span");
  1610. let pageText=document.createElement("a");
  1611. pageBar.className="pagetual_pageBar";
  1612. pageBar.id="pagetual_pageBar";
  1613. if(isPause){
  1614. pageBar.classList.add("stop");
  1615. }
  1616. pageBar.style.cssText=pageBarStyle;
  1617. pageBar.title=i18n(isPause?"enable":"disable");
  1618. upSpan.innerHTML=upSvg;
  1619. upSpan.title=i18n("toTop");
  1620. downSpan.innerHTML=downSvg;
  1621. downSpan.title=i18n("toBottom");
  1622. upSpan.style.cssText=initStyle;
  1623. downSpan.style.cssText=initStyle;
  1624. pageText.href=url;
  1625. pageText.style=pageTextStyle;
  1626. pageText.innerHTML="Page "+curPage;
  1627. pageText.title=i18n("current");
  1628. pageBar.appendChild(upSpan);
  1629. pageBar.appendChild(pageText);
  1630. pageBar.appendChild(downSpan);
  1631. if(inTable){
  1632. let example=(insert.tagName=="TR" || insert.tagName=="TBODY")?insert:insert.previousElementSibling;
  1633. let tdNum=example.tagName=="TR"?example.children.length:example.querySelector("tr").children.length;
  1634. pageBar.style.display="table-row";
  1635. let td=document.createElement("td");
  1636. td.colSpan=tdNum;
  1637. td.style.textAlign="center";
  1638. td.appendChild(upSpan);
  1639. td.appendChild(pageText);
  1640. td.appendChild(downSpan);
  1641. pageBar.appendChild(td);
  1642. }
  1643.  
  1644. upSpan.addEventListener("click", e=>{
  1645. changeStop(true);
  1646. pageBar.title=i18n(isPause?"enable":"disable");
  1647. document.body.scrollTop=0;
  1648. document.documentElement.scrollTop=0;
  1649. e.preventDefault();
  1650. e.stopPropagation();
  1651. });
  1652. downSpan.addEventListener("click", e=>{
  1653. changeStop(true);
  1654. pageBar.title=i18n(isPause?"enable":"disable");
  1655. document.body.scrollTop=9999999;
  1656. document.documentElement.scrollTop=9999999;
  1657. e.preventDefault();
  1658. e.stopPropagation();
  1659. });
  1660. pageBar.addEventListener("click", e=>{
  1661. changeStop(!isPause);
  1662. pageBar.title=i18n(isPause?"enable":"disable");
  1663. });
  1664. pageText.addEventListener("click", e=>{
  1665. e.stopPropagation();
  1666. });
  1667. pageBar.style.width=parseInt(_unsafeWindow.getComputedStyle(insert.parentNode).width)*.99+"px";
  1668. if(inLi){
  1669. pageBar.style.width="90%";
  1670. pageBar.style.minWidth="150px";
  1671. pageBar.style.display="inline-block";
  1672. let line=document.createElement("li");
  1673. line.appendChild(pageBar);
  1674. if(ruleParser.curSiteRule.insertPos==2){
  1675. insert.appendChild(line);
  1676. }else{
  1677. insert.parentNode.insertBefore(line, insert);
  1678. }
  1679. }else{
  1680. if(ruleParser.curSiteRule.insertPos==2){
  1681. insert.appendChild(pageBar);
  1682. }else{
  1683. insert.parentNode.insertBefore(pageBar, insert);
  1684. }
  1685. }
  1686.  
  1687. let scrollH=Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
  1688. let actualTop = pageBar.offsetTop;
  1689. let current = pageBar.parentNode.offsetParent;
  1690. while (current !== null){
  1691. actualTop += current.offsetTop;
  1692. current = current.offsetParent;
  1693. }
  1694. bottomGap=scrollH-actualTop+800;
  1695. return pageBar;
  1696. }
  1697.  
  1698. var emuIframe;
  1699. function emuPage(callback){
  1700. let orgPage,curPage,iframeDoc,times=0;
  1701. function checkPage(){
  1702. if(isPause)return;
  1703. try{
  1704. iframeDoc=emuIframe.contentDocument || emuIframe.contentWindow.document;
  1705. }catch(e){
  1706. isPause=true;
  1707. callback(false, false);
  1708. return;
  1709. }
  1710. if(!orgPage){
  1711. orgPage=ruleParser.getPageElement(iframeDoc, iframeDoc.defaultView, true);
  1712. if(orgPage)orgPage=orgPage[0];
  1713. if(orgPage && orgPage.tagName=="UL")orgPage=orgPage.children[0];
  1714. let nextLink=ruleParser.getNextLink(iframeDoc);
  1715. if(orgPage && nextLink){
  1716. let display=iframeDoc.defaultView.getComputedStyle(nextLink).display;
  1717. if(display=="none"){
  1718. isPause=true;
  1719. callback(false, false);
  1720. }else{
  1721. nextLink.click();
  1722. setTimeout(()=>{
  1723. checkPage();
  1724. },500);
  1725. }
  1726. }else{
  1727. isPause=true;
  1728. callback(false, false);
  1729. }
  1730. return;
  1731. }
  1732. if(times++ > 10){
  1733. isPause=true;
  1734. callback(false, false);
  1735. return;
  1736. }
  1737. let eles=ruleParser.getPageElement(iframeDoc, iframeDoc.defaultView, true);
  1738. if(!eles || eles.length==0 || orgPage == eles[0]){
  1739. setTimeout(()=>{
  1740. checkPage();
  1741. },500);
  1742. }else{
  1743. callback(iframeDoc, eles);
  1744. }
  1745. }
  1746. if(!emuIframe){
  1747. emuIframe = document.createElement('iframe');
  1748. emuIframe.name = 'pagetual-iframe';
  1749. emuIframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  1750. emuIframe.width = '100%';
  1751. emuIframe.height = '0';
  1752. emuIframe.frameBorder = '0';
  1753. emuIframe.style.cssText = 'margin:0!important;padding:0!important;visibility:hidden!important;';
  1754. emuIframe.addEventListener("load", e=>{
  1755. setTimeout(()=>{
  1756. checkPage();
  1757. },300);
  1758. });
  1759. emuIframe.src=location.href;
  1760. document.body.appendChild(emuIframe);
  1761. }else{
  1762. checkPage();
  1763. }
  1764. }
  1765.  
  1766. function forceIframe(url, callback){
  1767. let curIframe = document.createElement('iframe');
  1768. curIframe.name = 'pagetual-iframe';
  1769. curIframe.sandbox="allow-same-origin allow-scripts allow-popups allow-forms";
  1770. curIframe.frameBorder = '0';
  1771. curIframe.scrolling="no";
  1772. 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;';
  1773. curIframe.addEventListener("load", e=>{
  1774. let iframeDoc;
  1775. try{
  1776. iframeDoc=curIframe.contentDocument || curIframe.contentWindow.document;
  1777. }catch(e){
  1778. isPause=true;
  1779. callback(false, false);
  1780. return;
  1781. }
  1782. ruleParser.insertPage(iframeDoc, [], url, null, true);
  1783. callback(curIframe, true);
  1784. curIframe.style.height=iframeDoc.body.scrollHeight+"px";
  1785. curIframe.style.width=iframeDoc.body.scrollWidth+"px";
  1786. setTimeout(()=>{
  1787. curIframe.style.height=iframeDoc.body.scrollHeight+"px";
  1788. curIframe.style.width=iframeDoc.body.scrollWidth+"px";
  1789. },300);
  1790. });
  1791. curIframe.src=url;
  1792. let insert=ruleParser.getInsert();
  1793. document.body.insertBefore(curIframe, loading);
  1794. return curIframe;
  1795. }
  1796.  
  1797. function nextPage(){
  1798. if(isPause || isLoading)return;
  1799. let nextLink=ruleParser.nextLinkHref;
  1800. let insert;
  1801. if(nextLink)insert=ruleParser.getInsert();
  1802. if(nextLink && insert){
  1803. let isJs=/^(javascript|#)/.test(nextLink.replace(location.href,""));
  1804. if(location.protocol=="https:" && /^http:/.test(nextLink)){
  1805. nextLink=nextLink.replace(/^http/,"https");
  1806. }
  1807. isLoading=true;
  1808. loading.style.display="";
  1809. if(ruleParser.curSiteRule.pageElementByJs){
  1810. var over=ele=>{
  1811. isLoading=false;
  1812. loading.style.display="none";
  1813. if(ele){
  1814. createPageBar(nextLink);
  1815. ruleParser.insertPage(null, ele, nextLink, null, true);
  1816. }else{
  1817. isPause=true;
  1818. }
  1819. };
  1820. try{
  1821. Function("over",'"use strict";' + ruleParser.curSiteRule.pageElementByJs)(over);
  1822. }catch(e){}
  1823. }else if(ruleParser.curSiteRule.action==1 && !isJs){
  1824. requestFromIframe(nextLink, (doc, eles)=>{
  1825. isLoading=false;
  1826. loading.style.display="none";
  1827. if(eles){
  1828. createPageBar(nextLink);
  1829. ruleParser.insertPage(doc, eles, nextLink, null, true);
  1830. }
  1831. });
  1832. }else if(forceState==2 && !isJs){
  1833. forceIframe(nextLink, (iframe, eles)=>{
  1834. isLoading=false;
  1835. loading.style.display="none";
  1836. let pageBar=createPageBar(nextLink);
  1837. iframe.parentNode.insertBefore(pageBar, iframe);
  1838. });
  1839. }else{
  1840. if(!isJs){
  1841. requestDoc(nextLink, (eles)=>{
  1842. isLoading=false;
  1843. loading.style.display="none";
  1844. if(eles){
  1845. createPageBar(nextLink);
  1846. }
  1847. });
  1848. }else{
  1849. emuPage((doc, eles)=>{
  1850. isLoading=false;
  1851. loading.style.display="none";
  1852. if(eles){
  1853. createPageBar(nextLink);
  1854. ruleParser.insertPage(doc, eles, "", null, true);
  1855. }
  1856. });
  1857. }
  1858. }
  1859. }
  1860. }
  1861.  
  1862. function init(){
  1863. initRules(()=>{
  1864. initView();
  1865. initPage();
  1866. });
  1867. }
  1868. setTimeout(()=>{
  1869. init();
  1870. },300);
  1871. })();