@@ -67,7 +67,8 @@ const fetchScriptBody = async (url: string, { onProgress }: { [key: string]: any
6767 const response = await fetch ( url , {
6868 headers : {
6969 "Cache-Control" : "no-cache" ,
70- Accept : "text/javascript,application/javascript,text/plain,application/octet-stream,application/force-download" ,
70+ /* 不指定 application/octet-stream 和 application/force-download 避免触发伺服器端 Error 406 */
71+ Accept : "text/javascript, application/javascript, */*" , // prefer JavaScript, but anything is acceptable
7172 // 参考:加权 Accept-Encoding 值说明
7273 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Accept-Encoding#weighted_accept-encoding_values
7374 "Accept-Encoding" : "br;q=1.0, gzip;q=0.8, *;q=0.1" ,
@@ -83,8 +84,21 @@ const fetchScriptBody = async (url: string, { onProgress }: { [key: string]: any
8384 if ( ! response . body || ! response . headers ) {
8485 throw new Error ( "No response body or headers" ) ;
8586 }
86- if ( response . headers . get ( "content-type" ) ?. includes ( "text/html" ) ) {
87- throw new Error ( "Response is text/html, not a valid UserScript" ) ;
87+ const contentType = response . headers . get ( "content-type" ) ;
88+
89+ if ( contentType ) {
90+ // 不接受非 JavaScript文本 的回应
91+ const contentTypeLower = contentType . toLowerCase ( ) ;
92+ const m = / ^ \s * ( [ \w - ] + ) [ ^ \w - ] + ( [ \w - ] + ) / . exec ( contentTypeLower ) ;
93+ if ( m ) {
94+ const contentTypeOK =
95+ ( m [ 2 ] === "javascript" && ( m [ 1 ] === "text" || m [ 1 ] === "application" ) ) ||
96+ ( m [ 1 ] === "application" && ( m [ 2 ] === "octet-stream" || m [ 2 ] === "force-download" ) ) ;
97+ if ( ! contentTypeOK ) {
98+ throw new Error ( `Response is ${ contentType } , not a valid UserScript` ) ;
99+ // e.g. Response is text/html, not a valid UserScript
100+ }
101+ }
88102 }
89103
90104 const reader = response . body . getReader ( ) ;
@@ -113,7 +127,6 @@ const fetchScriptBody = async (url: string, { onProgress }: { [key: string]: any
113127 }
114128
115129 // 检测编码:优先使用 Content-Type,回退到 chardet(仅检测前16KB)
116- const contentType = response . headers . get ( "content-type" ) ;
117130 const encode = detectEncoding ( chunksAll , contentType ) ;
118131
119132 // 使用检测到的 charset 解码
0 commit comments