设置添加以下函数即可:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Fetch and log a request
* @param {Request} request
*/
const ErrorPageHotLink = `
<!DOCTYPE html>
<html>
<body>
<h1>Error Page</h1>
<p>此网站的管理员禁止盗链此资源</p>
</body>
</html>
`;
// 防盗链错误页面
async function rawHtmlResponse(html) {
const init = {
status: 403,
headers: {
'content-type': 'text/html;charset=UTF-8',
},
};
return new Response(html, init);
}
async function handleRequest(request) {
const refer = request.headers.get('referer')
// 自定义判断refer来进行错误页面返回
if (refer === null || refer === '') {
return rawHtmlResponse(ErrorPageHotLink);
}
const response = await fetch(request);
return response;
}
演示地址
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/249499.html