background.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. console.log("background.js");
  14. // The browser action
  15. function launch(){
  16. var data = {url:"html/domestic.html"}
  17. browser.tabs.create(data);
  18. }
  19. browser.browserAction.onClicked.addListener(launch);
  20. // For sumofus_main.js
  21. function redirect(requestDetails) {
  22. console.log("Redirecting: " + requestDetails.url);
  23. return {
  24. redirectUrl: "https://www.sumofus.org/campaigns/"
  25. };
  26. }
  27. browser.webRequest.onBeforeRequest.addListener(
  28. redirect,
  29. {urls: ["http://www.sumofus.org/"]},
  30. ["blocking"]
  31. );
  32. // Prevent google scripts from breaking the google drive add-on
  33. var block_urls = ["*://docs.google.com/*"];
  34. function cancel(requestDetails) {
  35. console.log("[google docs] Canceling: " + requestDetails.url);
  36. return {cancel: true};
  37. }
  38. browser.webRequest.onBeforeRequest.addListener(
  39. cancel, // function
  40. {urls: block_urls, types: ["script"]},
  41. ["blocking"]
  42. );
  43. const oldReddit = "https://old.reddit.com";
  44. const excludedPaths = [
  45. /^\/media/,
  46. /^\/poll/,
  47. /^\/rpan/,
  48. /^\/settings/,
  49. /^\/topics/,
  50. /^\/community-points/,
  51. /^\/r\/[a-zA-Z0-9_]+\/s\/.*/, // eg https://reddit.com/r/comics/s/TjDGhcl22d
  52. /^\/appeals?/,
  53. /\/r\/.*\/s\//,
  54. ];
  55. chrome.webRequest.onBeforeRequest.addListener(
  56. function (details) {
  57. const url = new URL(details.url);
  58. if (url.hostname === "old.reddit.com") return;
  59. for (const path of excludedPaths) {
  60. if (path.test(url.pathname)) return;
  61. }
  62. if (url.pathname.indexOf("/gallery") === 0) {
  63. return {
  64. redirectUrl:
  65. oldReddit + "/comments" + url.pathname.slice("/gallery".length),
  66. };
  67. }
  68. return { redirectUrl: oldReddit + url.pathname + url.search + url.hash };
  69. },
  70. {
  71. urls: [
  72. "*://reddit.com/*",
  73. "*://www.reddit.com/*",
  74. "*://np.reddit.com/*",
  75. "*://amp.reddit.com/*",
  76. "*://i.reddit.com/*",
  77. ],
  78. types: [
  79. "main_frame",
  80. "sub_frame",
  81. "stylesheet",
  82. "script",
  83. "image",
  84. "object",
  85. "xmlhttprequest",
  86. "other",
  87. ],
  88. },
  89. ["blocking"],
  90. );
  91. // Prevent reddit from rendering raw image URLs as HTML
  92. chrome.webRequest.onBeforeSendHeaders.addListener(
  93. function (details) {
  94. const url = new URL(details.url);
  95. const imageUrlHostnames = ["preview.redd.it", "i.redd.it"];
  96. for (const hostname of imageUrlHostnames) {
  97. if (url.hostname === hostname) {
  98. const headers = details.requestHeaders.filter(
  99. (h) => h.name.toLowerCase() !== "accept",
  100. );
  101. return { requestHeaders: headers };
  102. }
  103. }
  104. },
  105. {
  106. urls: ["*://i.redd.it/*", "*://preview.redd.it/*"],
  107. types: [
  108. "main_frame",
  109. "sub_frame",
  110. "stylesheet",
  111. "script",
  112. "image",
  113. "object",
  114. "xmlhttprequest",
  115. "other",
  116. ],
  117. },
  118. ["blocking", "requestHeaders"],
  119. );