-
Notifications
You must be signed in to change notification settings - Fork 325
修正 fetchScriptBody 的 Accept 设定,避免触发 Error 406 #1306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,7 +67,8 @@ | |
| const response = await fetch(url, { | ||
| headers: { | ||
| "Cache-Control": "no-cache", | ||
| Accept: "text/javascript,application/javascript,text/plain,application/octet-stream,application/force-download", | ||
| /* 不指定 application/octet-stream 和 application/force-download 避免触发伺服器端 Error 406 */ | ||
| Accept: "text/javascript, application/javascript, */*", // prefer JavaScript, but anything is acceptable | ||
| // 参考:加权 Accept-Encoding 值说明 | ||
| // https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Accept-Encoding#weighted_accept-encoding_values | ||
| "Accept-Encoding": "br;q=1.0, gzip;q=0.8, *;q=0.1", | ||
|
|
@@ -83,8 +84,21 @@ | |
| if (!response.body || !response.headers) { | ||
| throw new Error("No response body or headers"); | ||
| } | ||
| if (response.headers.get("content-type")?.includes("text/html")) { | ||
| throw new Error("Response is text/html, not a valid UserScript"); | ||
| const contentType = response.headers.get("content-type"); | ||
|
|
||
| if (contentType) { | ||
| // 不接受非 JavaScript文本 的回应 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我觉得也不用管,后面会去解析的,如果有问题依旧会报错
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 避免恶意连结。如果类型不对,就不下载了
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个包括网址不正确的跳页。会直接不下载而不是下载后再解析 |
||
| const contentTypeLower = contentType.toLowerCase(); | ||
| const m = /^\s*([\w-]+)[^\w-]+([\w-]+)/.exec(contentTypeLower); | ||
| if (m) { | ||
| const contentTypeOK = | ||
| (m[2] === "javascript" && (m[1] === "text" || m[1] === "application")) || | ||
| (m[1] === "application" && (m[2] === "octet-stream" || m[2] === "force-download")); | ||
| if (!contentTypeOK) { | ||
| throw new Error(`Response is ${contentType}, not a valid UserScript`); | ||
| // e.g. Response is text/html, not a valid UserScript | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const reader = response.body.getReader(); | ||
|
|
@@ -113,7 +127,6 @@ | |
| } | ||
|
|
||
| // 检测编码:优先使用 Content-Type,回退到 chardet(仅检测前16KB) | ||
| const contentType = response.headers.get("content-type"); | ||
| const encode = detectEncoding(chunksAll, contentType); | ||
|
|
||
| // 使用检测到的 charset 解码 | ||
|
|
@@ -331,7 +344,7 @@ | |
|
|
||
| useEffect(() => { | ||
| !loaded && initAsync(); | ||
| }, [searchParams, loaded]); | ||
|
|
||
| const [watchFile, setWatchFile] = useState(false); | ||
| const metadataLive = useMemo(() => (scriptInfo?.metadata || {}) as SCMetadata, [scriptInfo]); | ||
|
|
@@ -646,7 +659,7 @@ | |
| return () => { | ||
| unmountFileTrack(handle); | ||
| }; | ||
| }, [memoWatchFile]); | ||
|
|
||
| // 检查是否有 uuid 或 file | ||
| const searchParamUrl = searchParams.get("url"); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我觉得可以直接不指定了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个还好吧。之前没
*/*加了
*/*后就不会让 Server报错只是 javascript 优先