You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
6.7 KiB

10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. /*******************************************************************************
  2. uMatrix - a Chromium browser extension to black/white list requests.
  3. Copyright (C) 2014-2016 Raymond Hill
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see {http://www.gnu.org/licenses/}.
  14. Home: https://github.com/gorhill/uMatrix
  15. */
  16. /* global chrome, µMatrix */
  17. 'use strict';
  18. /******************************************************************************/
  19. (function() {
  20. var µm = µMatrix;
  21. µm.pMatrix = new µm.Matrix();
  22. µm.pMatrix.setSwitch('matrix-off', 'localhost', 1);
  23. µm.pMatrix.setSwitch('matrix-off', 'about-scheme', 1);
  24. µm.pMatrix.setSwitch('matrix-off', 'chrome-extension-scheme', 1);
  25. µm.pMatrix.setSwitch('matrix-off', 'chrome-scheme', 1);
  26. µm.pMatrix.setSwitch('matrix-off', µm.behindTheSceneScope, 1);
  27. µm.pMatrix.setSwitch('matrix-off', 'opera-scheme', 1);
  28. µm.pMatrix.setSwitch('referrer-spoof', 'behind-the-scene', 2);
  29. µm.pMatrix.setSwitch('ua-spoof', 'behind-the-scene', 2);
  30. µm.pMatrix.setSwitch('https-strict', 'behind-the-scene', 2);
  31. µm.pMatrix.setCell('*', '*', '*', µm.Matrix.Red);
  32. µm.pMatrix.setCell('*', '*', 'css', µm.Matrix.Green);
  33. µm.pMatrix.setCell('*', '*', 'image', µm.Matrix.Green);
  34. µm.pMatrix.setCell('*', '*', 'frame', µm.Matrix.Red);
  35. µm.pMatrix.setCell('*', '1st-party', '*', µm.Matrix.Green);
  36. µm.pMatrix.setCell('*', '1st-party', 'frame', µm.Matrix.Green);
  37. µm.tMatrix = new µm.Matrix();
  38. µm.tMatrix.assign(µm.pMatrix);
  39. })();
  40. /******************************************************************************/
  41. µMatrix.hostnameFromURL = function(url) {
  42. var hn = this.URI.hostnameFromURI(url);
  43. return hn === '' ? '*' : hn;
  44. };
  45. µMatrix.scopeFromURL = µMatrix.hostnameFromURL;
  46. /******************************************************************************/
  47. µMatrix.evaluateURL = function(srcURL, desHostname, type) {
  48. var srcHostname = this.URI.hostnameFromURI(srcURL);
  49. return this.tMatrix.evaluateCellZXY(srcHostname, desHostname, type);
  50. };
  51. /******************************************************************************/
  52. // Whitelist something
  53. µMatrix.whitelistTemporarily = function(srcHostname, desHostname, type) {
  54. this.tMatrix.whitelistCell(srcHostname, desHostname, type);
  55. };
  56. µMatrix.whitelistPermanently = function(srcHostname, desHostname, type) {
  57. if ( this.pMatrix.whitelistCell(srcHostname, desHostname, type) ) {
  58. this.saveMatrix();
  59. }
  60. };
  61. /******************************************************************************/
  62. // Auto-whitelisting the `all` cell is a serious action, hence this will be
  63. // done only from within a scope.
  64. µMatrix.autoWhitelistAllTemporarily = function(pageURL) {
  65. var srcHostname = this.URI.hostnameFromURI(pageURL);
  66. if ( this.mustBlock(srcHostname, '*', '*') === false ) {
  67. return false;
  68. }
  69. this.tMatrix.whitelistCell(srcHostname, '*', '*');
  70. return true;
  71. };
  72. /******************************************************************************/
  73. // Blacklist something
  74. µMatrix.blacklistTemporarily = function(srcHostname, desHostname, type) {
  75. this.tMatrix.blacklistCell(srcHostname, desHostname, type);
  76. };
  77. µMatrix.blacklistPermanently = function(srcHostname, desHostname, type) {
  78. if ( this.pMatrix.blacklist(srcHostname, desHostname, type) ) {
  79. this.saveMatrix();
  80. }
  81. };
  82. /******************************************************************************/
  83. // Remove something from both black and white lists.
  84. µMatrix.graylistTemporarily = function(srcHostname, desHostname, type) {
  85. this.tMatrix.graylistCell(srcHostname, desHostname, type);
  86. };
  87. µMatrix.graylistPermanently = function(srcHostname, desHostname, type) {
  88. if ( this.pMatrix.graylistCell(srcHostname, desHostname, type) ) {
  89. this.saveMatrix();
  90. }
  91. };
  92. /******************************************************************************/
  93. // TODO: Should type be transposed by the caller or in place here? Not an
  94. // issue at this point but to keep in mind as this function is called
  95. // more and more from different places.
  96. µMatrix.filterRequest = function(fromURL, type, toURL) {
  97. // Block request?
  98. var srcHostname = this.hostnameFromURL(fromURL);
  99. var desHostname = this.hostnameFromURL(toURL);
  100. // If no valid hostname, use the hostname of the source.
  101. // For example, this case can happen with data URI.
  102. if ( desHostname === '' ) {
  103. desHostname = srcHostname;
  104. }
  105. // Blocked by matrix filtering?
  106. return this.mustBlock(srcHostname, desHostname, type);
  107. };
  108. /******************************************************************************/
  109. µMatrix.mustBlock = function(srcHostname, desHostname, type) {
  110. return this.tMatrix.mustBlock(srcHostname, desHostname, type);
  111. };
  112. µMatrix.mustAllow = function(srcHostname, desHostname, type) {
  113. return this.mustBlock(srcHostname, desHostname, type) === false;
  114. };
  115. /******************************************************************************/
  116. // Commit temporary permissions.
  117. µMatrix.commitPermissions = function(persist) {
  118. this.pMatrix.assign(this.tMatrix);
  119. if ( persist ) {
  120. this.saveMatrix();
  121. }
  122. };
  123. /******************************************************************************/
  124. // Reset all rules to their default state.
  125. µMatrix.revertAllRules = function() {
  126. this.tMatrix.assign(this.pMatrix);
  127. };
  128. /******************************************************************************/
  129. µMatrix.turnOff = function() {
  130. vAPI.app.start();
  131. };
  132. µMatrix.turnOn = function() {
  133. vAPI.app.stop();
  134. };
  135. /******************************************************************************/
  136. µMatrix.formatCount = function(count) {
  137. if ( typeof count !== 'number' ) {
  138. return '';
  139. }
  140. var s = count.toFixed(0);
  141. if ( count >= 1000 ) {
  142. if ( count < 10000 ) {
  143. s = '>' + s.slice(0,1) + 'K';
  144. } else if ( count < 100000 ) {
  145. s = s.slice(0,2) + 'K';
  146. } else if ( count < 1000000 ) {
  147. s = s.slice(0,3) + 'K';
  148. } else if ( count < 10000000 ) {
  149. s = s.slice(0,1) + 'M';
  150. } else {
  151. s = s.slice(0,-6) + 'M';
  152. }
  153. }
  154. return s;
  155. };