background.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*******************************************************************************
  2. ηMatrix - a browser extension to black/white list requests.
  3. Copyright (C) 2014-2019 Raymond Hill
  4. Copyright (C) 2019-2022 Alessio Vanni
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see {http://www.gnu.org/licenses/}.
  15. Home: https://gitlab.com/vannilla/ematrix
  16. uMatrix Home: https://github.com/gorhill/uMatrix
  17. */
  18. 'use strict';
  19. var ηMatrix = (function () {
  20. Cu.import('chrome://ematrix/content/lib/LiquidDict.jsm');
  21. let oneSecond = 1000;
  22. let oneMinute = 60 * oneSecond;
  23. let oneHour = 60 * oneMinute;
  24. let oneDay = 24 * oneHour;
  25. let _RequestStats = function () {
  26. this.reset();
  27. };
  28. _RequestStats.prototype.reset = function () {
  29. this.all =
  30. this.doc =
  31. this.frame =
  32. this.script =
  33. this.css =
  34. this.image =
  35. this.media =
  36. this.xhr =
  37. this.other =
  38. this.cookie = 0;
  39. };
  40. var RequestStats = function () {
  41. this.allowed = new _RequestStats ();
  42. this.blocked = new _RequestStats ();
  43. };
  44. RequestStats.prototype.reset = function () {
  45. this.blocked.reset();
  46. this.allowed.reset();
  47. };
  48. RequestStats.prototype.record = function (type, blocked) {
  49. // Remember: always test against **false**
  50. if (blocked !== false) {
  51. this.blocked[type] += 1;
  52. this.blocked.all += 1;
  53. } else {
  54. this.allowed[type] += 1;
  55. this.allowed.all += 1;
  56. }
  57. };
  58. var requestStatsFactory = function () {
  59. return new RequestStats();
  60. };
  61. /**
  62. SVG-based icons below were extracted from
  63. fontawesome-webfont.svg v4.7. Excerpt of copyright notice at
  64. the top of the file:
  65. > Created by FontForge 20120731 at Mon Oct 24 17:37:40 2016
  66. > By ,,,
  67. > Copyright Dave Gandy 2016. All rights reserved.
  68. Excerpt of the license information in the fontawesome CSS
  69. file bundled with the package:
  70. > Font Awesome 4.7.0 by @davegandy - http://fontawesome.io - @fontawesome
  71. > License - http://fontawesome.io/license (Font: SIL OFL 1.1, CSS: MIT License)
  72. Font icons:
  73. - glyph-name: "external_link"
  74. */
  75. var rawSettingsDefault = {
  76. disableCSPReportInjection: false,
  77. placeholderBackground: [
  78. 'url("data:image/png;base64,',
  79. 'iVBORw0KGgoAAAANSUhEUgAAAAoAAAAK',
  80. 'CAAAAACoWZBhAAAABGdBTUEAALGPC/xh',
  81. 'BQAAAAJiS0dEAP+Hj8y/AAAAB3RJTUUH',
  82. '3wwIAAgyL/YaPAAAACJJREFUCFtjfMbO',
  83. 'AAQ/gZiFnQPEBAEmGIMIJgtIL8QEgtoA',
  84. 'In4D/96X1KAAAAAldEVYdGRhdGU6Y3Jl',
  85. 'YXRlADIwMTUtMTItMDhUMDA6MDg6NTAr',
  86. 'MDM6MDAasuuJAAAAJXRFWHRkYXRlOm1v',
  87. 'ZGlmeQAyMDE1LTEyLTA4VDAwOjA4OjUw',
  88. 'KzAzOjAwa+9TNQAAAABJRU5ErkJggg==',
  89. '") ',
  90. 'repeat scroll #fff'
  91. ].join(''),
  92. placeholderBorder: '1px solid rgba(0, 0, 0, 0.1)',
  93. imagePlaceholder: true,
  94. imagePlaceholderBackground: 'default',
  95. imagePlaceholderBorder: 'default',
  96. framePlaceholder: true,
  97. framePlaceholderDocument: [
  98. '<html><head>',
  99. '<meta charset="utf-8">',
  100. '<style>',
  101. 'body { ',
  102. 'background: {{bg}};',
  103. 'color: gray;',
  104. 'font: 12px sans-serif;',
  105. 'margin: 0;',
  106. 'overflow: hidden;',
  107. 'padding: 2px;',
  108. 'white-space: nowrap;',
  109. '}',
  110. 'a { ',
  111. 'color: inherit;',
  112. 'padding: 0 3px;',
  113. 'text-decoration: none;',
  114. '}',
  115. 'svg {',
  116. 'display: inline-block;',
  117. 'fill: gray;',
  118. 'height: 12px;',
  119. 'vertical-align: bottom;',
  120. 'width: 12px;',
  121. '}',
  122. '</style></head><body>',
  123. '<span><a href="{{url}}" title="{{url}}" target="_blank">',
  124. '<svg viewBox="0 0 1792 1792"><path transform="scale(1,-1) translate(0,-1536)" d="M1408 608v-320q0 -119 -84.5 -203.5t-203.5 -84.5h-832q-119 0 -203.5 84.5t-84.5 203.5v832q0 119 84.5 203.5t203.5 84.5h704q14 0 23 -9t9 -23v-64q0 -14 -9 -23t-23 -9h-704q-66 0 -113 -47t-47 -113v-832q0 -66 47 -113t113 -47h832q66 0 113 47t47 113v320q0 14 9 23t23 9h64q14 0 23 -9t9 -23zM1792 1472v-512q0 -26 -19 -45t-45 -19t-45 19l-176 176l-652 -652q-10 -10 -23 -10t-23 10l-114 114q-10 10 -10 23t10 23l652 652l-176 176q-19 19 -19 45t19 45t45 19h512q26 0 45 -19t19 -45z" /></svg>',
  125. '</a>{{url}}</span>',
  126. '</body></html>'
  127. ].join(''),
  128. framePlaceholderBackground: 'default',
  129. };
  130. return {
  131. onBeforeStartQueue: [],
  132. userSettings: {
  133. alwaysDetachLogger: false,
  134. autoUpdate: false,
  135. clearBrowserCache: true,
  136. clearBrowserCacheAfter: 60,
  137. cloudStorageEnabled: false,
  138. collapseBlacklisted: true,
  139. collapseBlocked: false,
  140. colorBlindFriendly: false,
  141. deleteCookies: false,
  142. deleteUnusedSessionCookies: false,
  143. deleteUnusedSessionCookiesAfter: 60,
  144. deleteLocalStorage: false,
  145. displayTextSize: '14px',
  146. externalHostsFiles: '',
  147. iconBadgeEnabled: false,
  148. maxLoggedRequests: 1000,
  149. popupCollapseAllDomains: false,
  150. popupCollapseBlacklistedDomains: false,
  151. popupScopeLevel: 'domain',
  152. processHyperlinkAuditing: true,
  153. processReferer: false,
  154. disableUpdateIcon: false,
  155. resolveCname: false,
  156. assetsVersion: 1,
  157. },
  158. rawSettingsDefault: rawSettingsDefault,
  159. rawSettings: Object.assign({}, rawSettingsDefault),
  160. rawSettingsWriteTime: 0,
  161. clearBrowserCacheCycle: 0,
  162. cspNoInlineScript: "script-src 'unsafe-eval' blob: *",
  163. cspNoInlineStyle: "style-src blob: *",
  164. cspNoWorker: undefined,
  165. updateAssetsEvery: 11 * oneDay + 1 * oneHour + 1 * oneMinute + 1 * oneSecond,
  166. firstUpdateAfter: 11 * oneMinute,
  167. nextUpdateAfter: 11 * oneHour,
  168. assetsBootstrapLocation: 'assets/assets.json',
  169. pslAssetKey: 'public_suffix_list.dat',
  170. // list of live hosts files
  171. liveHostsFiles: {
  172. },
  173. // urls stats are kept on the back burner while waiting to be
  174. // reactivated in a tab or another.
  175. pageStores: {},
  176. pageStoresToken: 0,
  177. pageStoreCemetery: {},
  178. // page url => permission scope
  179. tMatrix: null,
  180. pMatrix: null,
  181. ubiquitousBlacklist: new LiquidDict(),
  182. ubiquitousWhitelist: new LiquidDict(),
  183. // various stats
  184. requestStatsFactory: requestStatsFactory,
  185. requestStats: requestStatsFactory(),
  186. cookieRemovedCounter: 0,
  187. localStorageRemovedCounter: 0,
  188. cookieHeaderFoiledCounter: 0,
  189. refererHeaderFoiledCounter: 0,
  190. hyperlinkAuditingFoiledCounter: 0,
  191. browserCacheClearedCounter: 0,
  192. storageUsed: 0,
  193. // record what the browser is doing behind the scene
  194. behindTheSceneScope: 'behind-the-scene',
  195. noopFunc: function () {},
  196. };
  197. })();