background.js 721 B

123456789101112131415161718192021222324252627282930
  1. var allowed_domain = "2020census.gov";
  2. var redirect_url = "http://2020census.gov";
  3. function redirect(requestDetails) {
  4. request_url = requestDetails.url;
  5. var regex = new RegExp("^(?:http(?:s)?:\/\/)?(?:[^\.]+\.)?"+allowed_domain+"(/.*)?$");
  6. // Check if within the same domain
  7. if(request_url.match(regex)) {
  8. // If within the domain
  9. console.log('Request passed')
  10. }
  11. else
  12. {
  13. // If prohibited domain
  14. console.log('Prohibited Domain: ' + request_url)
  15. return {
  16. redirectUrl: browser.runtime.getURL(redirect_url)
  17. }
  18. }
  19. }
  20. // Intercept all requests
  21. browser.webRequest.onBeforeSendHeaders.addListener (
  22. redirect,
  23. {urls:["<all_urls>"],types:["main_frame"]},
  24. ["blocking"]
  25. );