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.

164 lines
4.8 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. /* 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) {
  30. return setTimeout(function() { callback(); }, delay);
  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. PB: Services.prefs.getBranch('extensions.' + location.host + '.'),
  96. str: Components.classes['@mozilla.org/supports-string;1']
  97. .createInstance(Components.interfaces.nsISupportsString),
  98. getItem: function(key) {
  99. try {
  100. return this.PB.getComplexValue(
  101. key,
  102. Components.interfaces.nsISupportsString
  103. ).data;
  104. } catch (ex) {
  105. return null;
  106. }
  107. },
  108. setItem: function(key, value) {
  109. this.str.data = value;
  110. this.PB.setComplexValue(
  111. key,
  112. Components.interfaces.nsISupportsString,
  113. this.str
  114. );
  115. },
  116. removeItem: function(key) {
  117. this.PB.clearUserPref(key);
  118. },
  119. clear: function() {
  120. this.PB.deleteBranch('');
  121. }
  122. };
  123. /******************************************************************************/
  124. })();
  125. /******************************************************************************/