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.

188 lines
5.4 KiB

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