()
| 2147 | }); |
| 2148 | |
| 2149 | const hookFetch = function () { |
| 2150 | const rawSendBeacon = navigator.sendBeacon?.bind(navigator); |
| 2151 | unsafeWindow.fetch = new Proxy(fetch, { |
| 2152 | apply: function (target, thisArg, argumentsList) { |
| 2153 | let fetchReqUrl = ""; |
| 2154 | let fetchReqOptions = {}; |
| 2155 | |
| 2156 | if (typeof argumentsList[0] === "string") { |
| 2157 | fetchReqUrl = argumentsList[0]; |
| 2158 | fetchReqOptions = argumentsList[1] || {}; |
| 2159 | } else if (argumentsList[0] instanceof Request) { |
| 2160 | fetchReqOptions = argumentsList[0]; |
| 2161 | fetchReqUrl = fetchReqOptions.url; |
| 2162 | } |
| 2163 | |
| 2164 | const fetchReqMethod = ( |
| 2165 | argumentsList[1]?.method || |
| 2166 | fetchReqOptions?.method || |
| 2167 | "GET" |
| 2168 | ).toUpperCase(); |
| 2169 | |
| 2170 | try { |
| 2171 | // 取消审计1:直接返回一个空的审核结果。 |
| 2172 | if ( |
| 2173 | gv("k_closeModer", false) && |
| 2174 | typeof fetchReqUrl === "string" && |
| 2175 | /\/backend-api\/moderations(\?|$)/.test(fetchReqUrl) |
| 2176 | ) { |
| 2177 | return Promise.resolve( |
| 2178 | new Response(JSON.stringify({}), { |
| 2179 | status: 200, |
| 2180 | headers: { |
| 2181 | "Content-Type": "application/json", |
| 2182 | }, |
| 2183 | }), |
| 2184 | ); |
| 2185 | // 取消审计2:发送会话请求前,关闭 modapi 支持位。 |
| 2186 | } else if ( |
| 2187 | gv("k_closeModer", false) && |
| 2188 | typeof fetchReqUrl === "string" && |
| 2189 | /\/backend-api\/conversation(\?|$)/.test(fetchReqUrl) && |
| 2190 | argumentsList[1] && |
| 2191 | typeof argumentsList[1].body === "string" |
| 2192 | ) { |
| 2193 | const post_body = JSON.parse(argumentsList[1].body); |
| 2194 | post_body.supports_modapi = false; |
| 2195 | argumentsList[1].body = JSON.stringify(post_body); |
| 2196 | // 拦截跟踪:必须返回标准 Response,避免调用方执行 text/json 时崩溃。 |
| 2197 | } else if ( |
| 2198 | gv("k_intercepttracking", false) && |
| 2199 | isTrackingRequest(fetchReqUrl) |
| 2200 | ) { |
| 2201 | console.log( |
| 2202 | `KeepChatGPT: ${tl("拦截跟踪")}: ${fetchReqUrl}`, |
| 2203 | ); |
| 2204 | return Promise.resolve(buildBlockedTrackingResponse()); |
| 2205 | // fix openai bug:补一个稳定的 compliance 响应。 |
| 2206 | } else if ( |
no test coverage detected