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.

157 lines
4.5 KiB

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