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.

186 lines
5.4 KiB

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