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.

130 lines
3.9 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.getURL = chrome.runtime.getURL;
  48. /******************************************************************************/
  49. vAPI.i18n = chrome.i18n.getMessage;
  50. setScriptDirection(vAPI.i18n('@@ui_locale'));
  51. /******************************************************************************/
  52. vAPI.closePopup = function() {
  53. window.close();
  54. };
  55. /******************************************************************************/
  56. // A localStorage-like object which should be accessible from the
  57. // background page or auxiliary pages.
  58. // This storage is optional, but it is nice to have, for a more polished user
  59. // experience.
  60. // https://github.com/gorhill/uBlock/issues/2824
  61. // Use a dummy localStorage if for some reasons it's not available.
  62. // https://github.com/gorhill/uMatrix/issues/840
  63. // Always use a wrapper to seamlessly handle exceptions
  64. vAPI.localStorage = {
  65. clear: function() {
  66. try {
  67. window.localStorage.clear();
  68. } catch(ex) {
  69. }
  70. },
  71. getItem: function(key) {
  72. try {
  73. return window.localStorage.getItem(key);
  74. } catch(ex) {
  75. }
  76. return null;
  77. },
  78. removeItem: function(key) {
  79. try {
  80. window.localStorage.removeItem(key);
  81. } catch(ex) {
  82. }
  83. },
  84. setItem: function(key, value) {
  85. try {
  86. window.localStorage.setItem(key, value);
  87. } catch(ex) {
  88. }
  89. }
  90. };
  91. /******************************************************************************/
  92. vAPI.setTimeout = vAPI.setTimeout || window.setTimeout.bind(window);
  93. /******************************************************************************/
  94. })(this);
  95. /******************************************************************************/