[ol]addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
let url = new URL(request.url);
url.hostname = 'hostloc.com';
let response = await fetch(url, request);
// 检查响应头中的内容类型
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('text')) {
// 如果是文本类型,替换响应主体中的URL
let responseBody = await response.text();
responseBody = responseBody.replace(/hostloc\.com/g, 'restless-shape-9e4f.你的.workers.dev');
// 复制响应对象并更新它的属性
let headers = new Headers(response.headers);
headers.set('Access-Control-Allow-Origin', '*');
headers.set('Access-Control-Allow-Methods', 'GET');
headers.set('Access-Control-Allow-Headers', 'Content-Type');
return new Response(responseBody, {
status: response.status,
statusText: response.statusText,
headers: headers
});
} else {
// 如果不是文本类型,直接返回响应对象
return response;
}
}[/ol]复制代码
https://pastebin.ubuntu.com/p/R6xWqs6Zcs/