Closed Sec-ant closed 1 year ago
还有一点,Tampermonkey 加了 stream
类型的 responseType
后,作者提供了一种方法来判断 Gm_xmlhttpRequest
是否支持这种响应类型:
You can check streaming support this way:
console.log('streaming ' + (GM_xmlhttpRequest.RESPONSE_TYPE_STREAM === 'stream' ? 'supported' : 'not supported');
vite-plugin-monkey
提供的 responseType
类型中包含了 stream
,所以能否也让类型支持 GM_xmlhttpRequest.RESPONSE_TYPE_STREAM
?
我不太确定这样实现是不是对的:
diff --git a/packages/vite-plugin-monkey/src/client/types/index.ts b/packages/vite-plugin-monkey/src/client/types/index.ts
index c90c953..442ac91 100644
--- a/packages/vite-plugin-monkey/src/client/types/index.ts
+++ b/packages/vite-plugin-monkey/src/client/types/index.ts
@@ -339,6 +339,14 @@ export type GmXhrRequest<TContext, TResponseType extends GmResponseType> = {
onload?: GmRequestEventListener<GmResponseEvent<TContext, TResponseType>>;
};
+declare function GmXhr<TContext, TResponseType extends GmResponseType = 'text'>(
+ details: GmXhrRequest<TContext, TResponseType>,
+): GmAbortHandle;
+
+declare namespace GmXhr {
+ export const RESPONSE_TYPE_STREAM: "stream" | undefined;
+}
+
export type MonkeyWindow = typeof window & {
unsafeWindow: typeof window;
@@ -589,9 +597,7 @@ export type MonkeyWindow = typeof window & {
* @see https://www.tampermonkey.net/documentation.php#GM_xmlhttpRequest
* @see https://violentmonkey.github.io/api/gm/#gm_xmlhttprequest
*/
- GM_xmlhttpRequest: <TContext, TResponseType extends GmResponseType = 'text'>(
- details: GmXhrRequest<TContext, TResponseType>,
- ) => GmAbortHandle;
+ GM_xmlhttpRequest: typeof GmXhr
/**
* @see https://www.tampermonkey.net/documentation.php#GM_download
目前
GM_xmlhttpRequest
的onload
等回调函数中response
的类型是any
,希望能直接利用responseType
的值来限定 response 的类型,大概这个意思: