1+ // ==UserScript==
2+ // @name 423Down 增强
3+ // @version 1.0.0
4+ // @author X.I.U
5+ // @description 自动无缝翻页
6+ // @match *://www.423down.com/*
7+ // @exclude *://www.423down.com/*.html
8+ // @icon https://www.423down.com/favicon.ico
9+ // @grant GM_xmlhttpRequest
10+ // @grant GM_registerMenuCommand
11+ // @license GPL-3.0 License
12+ // @run -at document-end
13+ // @namespace https://github.com/XIU2/UserScript
14+ // ==/UserScript==
15+
16+ ( function ( ) {
17+ // 注册脚本菜单
18+ GM_registerMenuCommand ( '反馈 & 建议' , function ( ) { window . GM_openInTab ( 'https://github.com/XIU2/UserScript' , { active : true , insert : true , setParent : true } ) ; } ) ;
19+
20+ // 默认 ID 为 0
21+ var curSite = { SiteTypeID : 0 } ;
22+
23+ // 自动翻页规则
24+ let DBSite = {
25+ postslist : {
26+ SiteTypeID : 1 ,
27+ pager : {
28+ nextLink : '//div[@class="paging"]//a[contains(text(),"下一页")][@href]' ,
29+ pageElement : 'css;div.content-wrap ul.excerpt > li' ,
30+ HT_insert : [ 'css;div.content-wrap ul.excerpt' , 2 ] ,
31+ replaceE : 'css;div.paging' ,
32+ }
33+ }
34+ } ;
35+
36+ // 用于脚本内部判断当前 URL 类型
37+ let SiteType = {
38+ POSTSLIST : DBSite . postslist . SiteTypeID
39+ } ;
40+
41+ curSite = DBSite . postslist ;
42+ curSite . pageUrl = "" ; // 下一页URL
43+ pageLoading ( ) ; // 自动翻页
44+
45+
46+ // 自动翻页
47+ function pageLoading ( ) {
48+ if ( curSite . SiteTypeID > 0 ) {
49+ windowScroll ( function ( direction , e ) {
50+ if ( direction === "down" ) { // 下滑才准备翻页
51+ var scrollTop = document . documentElement . scrollTop || window . pageYOffset || document . body . scrollTop ;
52+ let scrollDelta = 1299 ;
53+ if ( document . documentElement . scrollHeight <= document . documentElement . clientHeight + scrollTop + scrollDelta ) {
54+ ShowPager . loadMorePage ( ) ;
55+ }
56+ }
57+ } ) ;
58+ }
59+ }
60+
61+
62+ // 滚动条事件
63+ function windowScroll ( fn1 ) {
64+ var beforeScrollTop = document . documentElement . scrollTop ,
65+ fn = fn1 || function ( ) { } ;
66+ setTimeout ( function ( ) { // 延时执行,避免刚载入到页面就触发翻页事件
67+ window . addEventListener ( "scroll" , function ( e ) {
68+ var afterScrollTop = document . documentElement . scrollTop ,
69+ delta = afterScrollTop - beforeScrollTop ;
70+ if ( delta == 0 ) return false ;
71+ fn ( delta > 0 ? "down" : "up" , e ) ;
72+ beforeScrollTop = afterScrollTop ;
73+ } , false ) ;
74+ } , 1000 )
75+ }
76+
77+
78+ var ShowPager = { // 修改自 https://greasyfork.org/scripts/14178
79+ getFullHref : function ( e ) {
80+ if ( e == null ) return '' ;
81+ "string" != typeof e && ( e = e . getAttribute ( "href" ) ) ;
82+ var t = this . getFullHref . a ;
83+ return t || ( this . getFullHref . a = t = document . createElement ( "a" ) ) , t . href = e , t . href ;
84+ } ,
85+ createDocumentByString : function ( e ) {
86+ if ( e ) {
87+ if ( "HTML" !== document . documentElement . nodeName ) return ( new DOMParser ) . parseFromString ( e , "application/xhtml+xml" ) ;
88+ var t ;
89+ try {
90+ t = ( new DOMParser ) . parseFromString ( e , "text/html" ) ;
91+ } catch ( e ) {
92+ }
93+ if ( t ) return t ;
94+ if ( document . implementation . createHTMLDocument ) t = document . implementation . createHTMLDocument ( "ADocument" ) ; else try {
95+ ( t = document . cloneNode ( ! 1 ) ) . appendChild ( t . importNode ( document . documentElement , ! 1 ) ) ,
96+ t . documentElement . appendChild ( t . createElement ( "head" ) ) , t . documentElement . appendChild ( t . createElement ( "body" ) ) ;
97+ } catch ( e ) {
98+ }
99+ if ( t ) {
100+ var r = document . createRange ( ) ;
101+ r . selectNodeContents ( document . body ) ;
102+ var n = r . createContextualFragment ( e ) ;
103+ t . body . appendChild ( n ) ;
104+ for ( var a , o = {
105+ TITLE : ! 0 ,
106+ META : ! 0 ,
107+ LINK : ! 0 ,
108+ STYLE : ! 0 ,
109+ BASE : ! 0
110+ } , i = t . body , s = i . childNodes , c = s . length - 1 ; c >= 0 ; c -- ) o [ ( a = s [ c ] ) . nodeName ] && i . removeChild ( a ) ;
111+ return t ;
112+ }
113+ } else console . error ( "没有找到要转成DOM的字符串" ) ;
114+ } ,
115+ loadMorePage : function ( ) {
116+ if ( curSite . pager ) {
117+ let curPageEle = getElementByXpath ( curSite . pager . nextLink ) ;
118+ var url = this . getFullHref ( curPageEle ) ;
119+ //console.log(`${url} ${curPageEle} ${curSite.pageUrl}`);
120+ if ( url === '' ) return ;
121+ if ( curSite . pageUrl === url ) return ; // 不会重复加载相同的页面
122+ curSite . pageUrl = url ;
123+ // 读取下一页的数据
124+ curSite . pager . startFilter && curSite . pager . startFilter ( ) ;
125+ GM_xmlhttpRequest ( {
126+ url : url ,
127+ method : "GET" ,
128+ timeout : 5000 ,
129+ onload : function ( response ) {
130+ try {
131+ var newBody = ShowPager . createDocumentByString ( response . responseText ) ;
132+ let pageElems = getAllElements ( curSite . pager . pageElement , newBody , newBody ) ;
133+ let toElement = getAllElements ( curSite . pager . HT_insert [ 0 ] ) [ 0 ] ;
134+ if ( pageElems . length >= 0 ) {
135+ let addTo = "beforeend" ;
136+ if ( curSite . pager . HT_insert [ 1 ] == 1 ) addTo = "beforebegin" ;
137+ // 插入新页面元素
138+ pageElems . forEach ( function ( one ) {
139+ toElement . insertAdjacentElement ( addTo , one ) ;
140+ } ) ;
141+ // 替换待替换元素
142+ try {
143+ let oriE = getAllElements ( curSite . pager . replaceE ) ;
144+ let repE = getAllElements ( curSite . pager . replaceE , newBody , newBody ) ;
145+ if ( oriE . length === repE . length ) {
146+ for ( var i = 0 ; i < oriE . length ; i ++ ) {
147+ oriE [ i ] . outerHTML = repE [ i ] . outerHTML ;
148+ }
149+ }
150+ } catch ( e ) {
151+ console . log ( e ) ;
152+ }
153+ }
154+ } catch ( e ) {
155+ console . log ( e ) ;
156+ }
157+ }
158+ } ) ;
159+ }
160+ } ,
161+ } ;
162+
163+
164+ function getElementByXpath ( e , t , r ) {
165+ r = r || document , t = t || r ;
166+ try {
167+ return r . evaluate ( e , t , null , XPathResult . FIRST_ORDERED_NODE_TYPE , null ) . singleNodeValue ;
168+ } catch ( t ) {
169+ return void console . error ( "无效的xpath" ) ;
170+ }
171+ }
172+
173+
174+ function getAllElements ( e , t , r , n , o ) {
175+ let getAllElementsByXpath = function ( e , t , r ) {
176+ return r = r || document , t = t || r , r . evaluate ( e , t , null , XPathResult . ORDERED_NODE_SNAPSHOT_TYPE , null ) ;
177+ }
178+
179+ var i , s = [ ] ;
180+ if ( ! e ) return s ;
181+ if ( r = r || document , n = n || window , o = o || void 0 , t = t || r , "string" == typeof e ) i = 0 === e . search ( / ^ c s s ; / i) ? function getAllElementsByCSS ( e , t ) {
182+ return ( t || document ) . querySelectorAll ( e ) ;
183+ } ( e . slice ( 4 ) , t ) : getAllElementsByXpath ( e , t , r ) ; else {
184+ if ( ! ( i = e ( r , n , o ) ) ) return s ;
185+ if ( i . nodeType ) return s [ 0 ] = i , s ;
186+ }
187+ return function makeArray ( e ) {
188+ var t , r , n , o = [ ] ;
189+ if ( e . pop ) {
190+ for ( t = 0 , r = e . length ; t < r ; t ++ ) ( n = e [ t ] ) && ( n . nodeType ? o . push ( n ) : o = o . concat ( makeArray ( n ) ) ) ;
191+ return a ( ) ( o ) ;
192+ }
193+ if ( e . item ) {
194+ for ( t = e . length ; t ; ) o [ -- t ] = e [ t ] ;
195+ return o ;
196+ }
197+ if ( e . iterateNext ) {
198+ for ( t = e . snapshotLength ; t ; ) o [ -- t ] = e . snapshotItem ( t ) ;
199+ return o ;
200+ }
201+ } ( i ) ;
202+ }
203+ } ) ( ) ;
0 commit comments