Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/pages/install/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得可以直接不指定了

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个还好吧。之前没 */*
加了 */* 后就不会让 Server报错
只是 javascript 优先

// 参考:加权 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",
Expand All @@ -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文本 的回应
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得也不用管,后面会去解析的,如果有问题依旧会报错

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

避免恶意连结。如果类型不对,就不下载了

Copy link
Collaborator Author

@cyfung1031 cyfung1031 Mar 22, 2026

Choose a reason for hiding this comment

The 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();
Expand Down Expand Up @@ -113,7 +127,6 @@
}

// 检测编码:优先使用 Content-Type,回退到 chardet(仅检测前16KB)
const contentType = response.headers.get("content-type");
const encode = detectEncoding(chunksAll, contentType);

// 使用检测到的 charset 解码
Expand Down Expand Up @@ -331,7 +344,7 @@

useEffect(() => {
!loaded && initAsync();
}, [searchParams, loaded]);

Check warning on line 347 in src/pages/install/App.tsx

View workflow job for this annotation

GitHub Actions / Run tests

React Hook useEffect has a missing dependency: 'initAsync'. Either include it or remove the dependency array

const [watchFile, setWatchFile] = useState(false);
const metadataLive = useMemo(() => (scriptInfo?.metadata || {}) as SCMetadata, [scriptInfo]);
Expand Down Expand Up @@ -646,7 +659,7 @@
return () => {
unmountFileTrack(handle);
};
}, [memoWatchFile]);

Check warning on line 662 in src/pages/install/App.tsx

View workflow job for this annotation

GitHub Actions / Run tests

React Hook useEffect has missing dependencies: 'localFileHandle', 'scriptInfo?.uuid', 'setupWatchFile', and 'watchFile'. Either include them or remove the dependency array

// 检查是否有 uuid 或 file
const searchParamUrl = searchParams.get("url");
Expand Down
Loading