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.

135 lines
3.9 KiB

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. self.vAPI = self.vAPI || {};
  22. /******************************************************************************/
  23. // http://www.w3.org/International/questions/qa-scripts#directions
  24. var setScriptDirection = function(language) {
  25. document.body.setAttribute(
  26. 'dir',
  27. ['ar', 'he', 'fa', 'ps', 'ur'].indexOf(language) !== -1 ? 'rtl' : 'ltr'
  28. );
  29. };
  30. /******************************************************************************/
  31. vAPI.download = function(details) {
  32. if ( !details.url ) {
  33. return;
  34. }
  35. var a = document.createElement('a');
  36. if ( 'download' in a ) {
  37. a.href = details.url;
  38. a.setAttribute('download', details.filename || '');
  39. a.dispatchEvent(new MouseEvent('click'));
  40. return;
  41. }
  42. var request = {
  43. what: 'gotoURL',
  44. details: {
  45. url: details.url,
  46. index: -1
  47. }
  48. };
  49. if ( vAPI.isMainProcess ) {
  50. vAPI.tabs.open(request.details);
  51. return;
  52. }
  53. var messager = vAPI.messaging.channel('_download');
  54. messager.send(request);
  55. messager.close();
  56. };
  57. /******************************************************************************/
  58. vAPI.insertHTML = (function() {
  59. const {classes: Cc, interfaces: Ci} = Components;
  60. const parser = Cc['@mozilla.org/parserutils;1'].getService(Ci.nsIParserUtils);
  61. const io = Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOService);
  62. return function(node, html) {
  63. while ( node.firstChild ) {
  64. node.removeChild(node.firstChild);
  65. }
  66. node.appendChild(parser.parseFragment(
  67. html,
  68. parser.SanitizerAllowStyle,
  69. false,
  70. io.newURI(document.baseURI, null, null),
  71. document.documentElement
  72. ));
  73. };
  74. })();
  75. /******************************************************************************/
  76. vAPI.getURL = function(path) {
  77. return 'chrome://' + location.host + '/content/' + path.replace(/^\/+/, '');
  78. };
  79. /******************************************************************************/
  80. vAPI.i18n = (function() {
  81. var stringBundle = Components.classes['@mozilla.org/intl/stringbundle;1']
  82. .getService(Components.interfaces.nsIStringBundleService)
  83. .createBundle('chrome://' + location.host + '/locale/messages.properties');
  84. return function(s) {
  85. try {
  86. return stringBundle.GetStringFromName(s);
  87. } catch (ex) {
  88. return '';
  89. }
  90. };
  91. })();
  92. setScriptDirection(navigator.language);
  93. /******************************************************************************/
  94. vAPI.closePopup = function() {
  95. sendAsyncMessage(location.host + ':closePopup');
  96. };
  97. /******************************************************************************/
  98. })();
  99. /******************************************************************************/