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. // http://www.w3.org/International/questions/qa-scripts#directions
  30. var setScriptDirection = function(language) {
  31. document.body.setAttribute(
  32. 'dir',
  33. ['ar', 'he', 'fa', 'ps', 'ur'].indexOf(language) !== -1 ? 'rtl' : 'ltr'
  34. );
  35. };
  36. /******************************************************************************/
  37. vAPI.download = function(details) {
  38. if ( !details.url ) {
  39. return;
  40. }
  41. var a = document.createElement('a');
  42. a.href = details.url;
  43. a.setAttribute('download', details.filename || '');
  44. a.dispatchEvent(new MouseEvent('click'));
  45. };
  46. /******************************************************************************/
  47. vAPI.insertHTML = (function() {
  48. const parser = Components.classes['@mozilla.org/parserutils;1']
  49. .getService(Components.interfaces.nsIParserUtils);
  50. return function(node, html) {
  51. while ( node.firstChild ) {
  52. node.removeChild(node.firstChild);
  53. }
  54. node.appendChild(parser.parseFragment(
  55. html,
  56. parser.SanitizerAllowStyle,
  57. false,
  58. Services.io.newURI(document.baseURI, null, null),
  59. document.documentElement
  60. ));
  61. };
  62. })();
  63. /******************************************************************************/
  64. vAPI.getURL = function(path) {
  65. return 'chrome://' + location.host + '/content/' + path.replace(/^\/+/, '');
  66. };
  67. /******************************************************************************/
  68. vAPI.i18n = (function() {
  69. var stringBundle = Services.strings.createBundle(
  70. 'chrome://' + location.host + '/locale/messages.properties'
  71. );
  72. return function(s) {
  73. try {
  74. return stringBundle.GetStringFromName(s);
  75. } catch (ex) {
  76. return '';
  77. }
  78. };
  79. })();
  80. setScriptDirection(navigator.language);
  81. /******************************************************************************/
  82. vAPI.closePopup = function() {
  83. sendAsyncMessage(location.host + ':closePopup');
  84. };
  85. /******************************************************************************/
  86. // A localStorage-like object which should be accessible from the
  87. // background page or auxiliary pages.
  88. // This storage is optional, but it is nice to have, for a more polished user
  89. // experience.
  90. vAPI.localStorage = {
  91. PB: Services.prefs.getBranch('extensions.' + location.host + '.'),
  92. str: Components.classes['@mozilla.org/supports-string;1']
  93. .createInstance(Components.interfaces.nsISupportsString),
  94. getItem: function(key) {
  95. try {
  96. return this.PB.getComplexValue(
  97. key,
  98. Components.interfaces.nsISupportsString
  99. ).data;
  100. } catch (ex) {
  101. return null;
  102. }
  103. },
  104. setItem: function(key, value) {
  105. this.str.data = value;
  106. this.PB.setComplexValue(
  107. key,
  108. Components.interfaces.nsISupportsString,
  109. this.str
  110. );
  111. },
  112. removeItem: function(key) {
  113. this.PB.clearUserPref(key);
  114. },
  115. clear: function() {
  116. this.PB.deleteBranch('');
  117. }
  118. };
  119. /******************************************************************************/
  120. vAPI.setTimeout = vAPI.setTimeout || function(callback, delay) {
  121. setTimeout(function() { callback(); }, delay);
  122. };
  123. /******************************************************************************/
  124. })();
  125. /******************************************************************************/