Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- addEventListener('fetch', event => {
- event.respondWith(fetchAndApply(event.request))
- })
- /**
- * If the browser is requesting an image and
- * the referer does not match your host
- * we redirect the request to your page
- */
- async function fetchAndApply(request) {
- // Fetch the response.
- let response = await fetch(request)
- // If it's an image, engage hotlink protection based on the
- // Referer header.
- let referer = request.headers.get('Referer')
- let contentType = response.headers.get('Content-Type') || ''
- let whitelist = [ 'domain1.com', 'domain2.com' ];
- if (referer && contentType.startsWith('image/')) {
- // It's an image and there's a Referer. Verify that the
- // hostnames match.
- if (
- new URL(referer).hostname !== new URL(request.url).hostname
- && !whitelist.includes(new URL(referer).hostname)
- ) {
- // Hosts don't match. This is a hotlink. Redirect the
- // user to our homepage.
- return new Response('', {
- status: 302,
- headers: {
- 'Location': '/'
- }
- })
- }
- }
- // Everything is fine, return the response normally.
- return response
- }
Add Comment
Please, Sign In to add comment