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.

184 lines
5.3 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. /******************************************************************************/
  90. // A localStorage-like object which should be accessible from the
  91. // background page or auxiliary pages.
  92. // This storage is optional, but it is nice to have, for a more polished user
  93. // experience.
  94. vAPI.localStorage = {
  95. pbName: '',
  96. pb: null,
  97. str: Components.classes['@mozilla.org/supports-string;1']
  98. .createInstance(Components.interfaces.nsISupportsString),
  99. init: function(pbName) {
  100. this.pbName = pbName;
  101. this.pb = Services.prefs.getBranch(pbName);
  102. },
  103. getItem: function(key) {
  104. try {
  105. return this.pb.getComplexValue(
  106. key,
  107. Components.interfaces.nsISupportsString
  108. ).data;
  109. } catch (ex) {
  110. return null;
  111. }
  112. },
  113. setItem: function(key, value) {
  114. this.str.data = value;
  115. this.pb.setComplexValue(
  116. key,
  117. Components.interfaces.nsISupportsString,
  118. this.str
  119. );
  120. },
  121. getBool: function(key) {
  122. try {
  123. return this.pb.getBoolPref(key);
  124. } catch (ex) {
  125. return null;
  126. }
  127. },
  128. setBool: function(key, value) {
  129. this.pb.setBoolPref(key, value);
  130. },
  131. setDefaultBool: function(key, defaultValue) {
  132. Services.prefs.getDefaultBranch(this.pbName).setBoolPref(key, defaultValue);
  133. },
  134. removeItem: function(key) {
  135. this.pb.clearUserPref(key);
  136. },
  137. clear: function() {
  138. this.pb.deleteBranch('');
  139. }
  140. };
  141. vAPI.localStorage.init('extensions.' + location.host + '.');
  142. /******************************************************************************/
  143. })();
  144. /******************************************************************************/