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.

128 lines
3.8 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. /*******************************************************************************
  2. uMatrix - a browser extension to block requests.
  3. Copyright (C) 2014-2017 The uMatrix/uBlock Origin 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. // For background page or non-background pages
  17. 'use strict';
  18. /******************************************************************************/
  19. /******************************************************************************/
  20. (function(self) {
  21. /******************************************************************************/
  22. // https://bugs.chromium.org/p/project-zero/issues/detail?id=1225&desc=6#c10
  23. if ( self.vAPI === undefined || self.vAPI.uMatrix !== true ) {
  24. self.vAPI = { uMatrix: true };
  25. }
  26. var vAPI = self.vAPI;
  27. var chrome = self.chrome;
  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(node, html) {
  48. node.innerHTML = html;
  49. };
  50. /******************************************************************************/
  51. vAPI.getURL = chrome.runtime.getURL;
  52. /******************************************************************************/
  53. vAPI.i18n = chrome.i18n.getMessage;
  54. setScriptDirection(vAPI.i18n('@@ui_locale'));
  55. /******************************************************************************/
  56. vAPI.closePopup = function() {
  57. window.close();
  58. };
  59. /******************************************************************************/
  60. // A localStorage-like object which should be accessible from the
  61. // background page or auxiliary pages.
  62. // This storage is optional, but it is nice to have, for a more polished user
  63. // experience.
  64. // This can throw in some contexts (like in devtool).
  65. try {
  66. vAPI.localStorage = self.localStorage;
  67. } catch (ex) {
  68. }
  69. // https://github.com/gorhill/uBlock/issues/2824
  70. // Use a dummy localStorage if for some reasons it's not available.
  71. if ( vAPI.localStorage instanceof Object === false ) {
  72. vAPI.localStorage = {
  73. length: 0,
  74. clear: function() {
  75. },
  76. getItem: function() {
  77. return null;
  78. },
  79. key: function() {
  80. throw new RangeError();
  81. },
  82. removeItem: function() {
  83. },
  84. setItem: function() {
  85. }
  86. };
  87. }
  88. /******************************************************************************/
  89. vAPI.setTimeout = vAPI.setTimeout || window.setTimeout.bind(window);
  90. /******************************************************************************/
  91. })(this);
  92. /******************************************************************************/