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.

191 lines
5.6 KiB

10 years ago
10 years ago
10 years ago
  1. /*******************************************************************************
  2. uMatrix - a browser extension to block requests.
  3. Copyright (C) 2014-2017 The uMatrix/uBlock Origin authors
  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 sendAsyncMessage */
  17. // For background page or non-background pages
  18. 'use strict';
  19. /******************************************************************************/
  20. (function(self) {
  21. /******************************************************************************/
  22. const {Services} = Components.utils.import(
  23. 'resource://gre/modules/Services.jsm',
  24. null
  25. );
  26. // https://bugs.chromium.org/p/project-zero/issues/detail?id=1225&desc=6#c10
  27. if ( self.vAPI === undefined || self.vAPI.uMatrix !== true ) {
  28. self.vAPI = { uMatrix: true };
  29. }
  30. var vAPI = self.vAPI;
  31. /******************************************************************************/
  32. vAPI.setTimeout = vAPI.setTimeout || function(callback, delay, extra) {
  33. return setTimeout(function(a) { callback(a); }, delay, extra);
  34. };
  35. /******************************************************************************/
  36. // http://www.w3.org/International/questions/qa-scripts#directions
  37. var setScriptDirection = function(language) {
  38. document.body.setAttribute(
  39. 'dir',
  40. ['ar', 'he', 'fa', 'ps', 'ur'].indexOf(language) !== -1 ? 'rtl' : 'ltr'
  41. );
  42. };
  43. /******************************************************************************/
  44. vAPI.download = function(details) {
  45. if ( !details.url ) {
  46. return;
  47. }
  48. var a = document.createElement('a');
  49. a.href = details.url;
  50. a.setAttribute('download', details.filename || '');
  51. a.dispatchEvent(new MouseEvent('click'));
  52. };
  53. /******************************************************************************/
  54. vAPI.insertHTML = (function() {
  55. const parser = Components.classes['@mozilla.org/parserutils;1']
  56. .getService(Components.interfaces.nsIParserUtils);
  57. // https://github.com/gorhill/uBlock/issues/845
  58. // Apparently dashboard pages execute with `about:blank` principal.
  59. return function(node, html) {
  60. while ( node.firstChild ) {
  61. node.removeChild(node.firstChild);
  62. }
  63. node.appendChild(parser.parseFragment(
  64. html,
  65. parser.SanitizerAllowStyle,
  66. false,
  67. Services.io.newURI('about:blank', null, null),
  68. document.documentElement
  69. ));
  70. };
  71. })();
  72. /******************************************************************************/
  73. vAPI.getURL = function(path) {
  74. return 'chrome://' + location.host + '/content/' + path.replace(/^\/+/, '');
  75. };
  76. /******************************************************************************/
  77. vAPI.i18n = (function() {
  78. var stringBundle = Services.strings.createBundle(
  79. 'chrome://' + location.host + '/locale/messages.properties'
  80. );
  81. return function(s) {
  82. try {
  83. return stringBundle.GetStringFromName(s);
  84. } catch (ex) {
  85. return '';
  86. }
  87. };
  88. })();
  89. setScriptDirection(navigator.language);
  90. /******************************************************************************/
  91. vAPI.closePopup = function() {
  92. sendAsyncMessage(location.host + ':closePopup');
  93. };
  94. /******************************************************************************/
  95. // A localStorage-like object which should be accessible from the
  96. // background page or auxiliary pages.
  97. // This storage is optional, but it is nice to have, for a more polished user
  98. // experience.
  99. vAPI.localStorage = {
  100. pbName: '',
  101. pb: null,
  102. str: Components.classes['@mozilla.org/supports-string;1']
  103. .createInstance(Components.interfaces.nsISupportsString),
  104. init: function(pbName) {
  105. this.pbName = pbName;
  106. this.pb = Services.prefs.getBranch(pbName);
  107. },
  108. getItem: function(key) {
  109. try {
  110. return this.pb.getComplexValue(
  111. key,
  112. Components.interfaces.nsISupportsString
  113. ).data;
  114. } catch (ex) {
  115. return null;
  116. }
  117. },
  118. setItem: function(key, value) {
  119. this.str.data = value;
  120. this.pb.setComplexValue(
  121. key,
  122. Components.interfaces.nsISupportsString,
  123. this.str
  124. );
  125. },
  126. getBool: function(key) {
  127. try {
  128. return this.pb.getBoolPref(key);
  129. } catch (ex) {
  130. return null;
  131. }
  132. },
  133. setBool: function(key, value) {
  134. this.pb.setBoolPref(key, value);
  135. },
  136. setDefaultBool: function(key, defaultValue) {
  137. Services.prefs.getDefaultBranch(this.pbName).setBoolPref(key, defaultValue);
  138. },
  139. removeItem: function(key) {
  140. this.pb.clearUserPref(key);
  141. },
  142. clear: function() {
  143. this.pb.deleteBranch('');
  144. }
  145. };
  146. vAPI.localStorage.init('extensions.' + location.host + '.');
  147. /******************************************************************************/
  148. })(this);
  149. /******************************************************************************/