@@ -73,6 +73,7 @@ const fetchScriptBody = async (url: string, { onProgress }: { [key: string]: any
7373 const response = await fetch ( url , {
7474 headers : {
7575 "Cache-Control" : "no-cache" ,
76+ // 参考:加权 Accept-Encoding 值说明
7677 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Accept-Encoding#weighted_accept-encoding_values
7778 "Accept-Encoding" : "br;q=1.0, gzip;q=0.8, *;q=0.1" ,
7879 } ,
@@ -89,23 +90,11 @@ const fetchScriptBody = async (url: string, { onProgress }: { [key: string]: any
8990 throw new Error ( "Response is text/html, not a valid UserScript" ) ;
9091 }
9192
92- const contentLength = + ( response . headers . get ( "Content-Length" ) || 0 ) ;
93- if ( contentLength < 30 ) {
94- throw new Error ( `Content-Length ${ contentLength } is too small for a valid UserScript` ) ;
95- }
96-
97- // 檢查 Content-Type 中的 charset
98- const contentType = response . headers . get ( "content-type" ) || "" ;
99- const charsetMatch = contentType . match ( / c h a r s e t = ( [ ^ ; ] + ) / i) ;
100- const charset = charsetMatch ? charsetMatch [ 1 ] . toLowerCase ( ) : "utf-8" ;
101-
10293 const reader = response . body . getReader ( ) ;
10394
104- // Step 2: 合計の長さを取得します
105-
106- // Step 3: データを読み込みます
107- let receivedLength = 0 ; // その時点の長さ
108- const chunks = [ ] ; // 受信したバイナリチャンクの配列(本文を構成します)
95+ // 读取数据
96+ let receivedLength = 0 ; // 当前已接收的长度
97+ const chunks = [ ] ; // 已接收的二进制分片数组(用于组装正文)
10998 while ( true ) {
11099 const { done, value } = await reader . read ( ) ;
111100
@@ -115,22 +104,27 @@ const fetchScriptBody = async (url: string, { onProgress }: { [key: string]: any
115104
116105 chunks . push ( value ) ;
117106 receivedLength += value . length ;
118- onProgress ?.( { receivedLength, contentLength } ) ;
107+ onProgress ?.( { receivedLength } ) ;
119108 }
120109
110+ // 检查 Content-Type 中的 charset
111+ const contentType = response . headers . get ( "content-type" ) || "" ;
112+ const charsetMatch = contentType . match ( / c h a r s e t = ( [ ^ ; ] + ) / i) ;
113+ const charset = charsetMatch ? charsetMatch [ 1 ] . toLowerCase ( ) : "utf-8" ;
114+
121115 if ( response . status !== 200 ) {
122116 throw new Error ( "fetch script info failed" ) ;
123117 }
124118
125- // 合併 chunks
119+ // 合并分片( chunks)
126120 const chunksAll = new Uint8Array ( receivedLength ) ; // (4.1)
127121 let position = 0 ;
128122 for ( const chunk of chunks ) {
129123 chunksAll . set ( chunk , position ) ; // (4.2)
130124 position += chunk . length ;
131125 }
132- // 使用檢測到的 charset 解碼
133126
127+ // 使用检测到的 charset 解码
134128 let code ;
135129 try {
136130 code = new TextDecoder ( charset ) . decode ( chunksAll ) ;
@@ -659,7 +653,7 @@ function App() {
659653 const loadURLAsync = async ( urlHref : string ) => {
660654 try {
661655 const { code, metadata } = await fetchScriptBody ( urlHref , {
662- onProgress : ( info : { receivedLength : number ; contentLength : number } ) => {
656+ onProgress : ( info : { receivedLength : number } ) => {
663657 setFetchingState ( ( prev ) => ( {
664658 ...prev ,
665659 loadingStatus : `Downloading. Received ${ formatBytes ( info . receivedLength ) } .` ,
0 commit comments