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.

122 lines
3.7 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. // This can throw in some contexts (like in devtool).
  61. try {
  62. vAPI.localStorage = self.localStorage;
  63. } catch (ex) {
  64. }
  65. // https://github.com/gorhill/uBlock/issues/2824
  66. // Use a dummy localStorage if for some reasons it's not available.
  67. if ( vAPI.localStorage instanceof Object === false ) {
  68. vAPI.localStorage = {
  69. length: 0,
  70. clear: function() {
  71. },
  72. getItem: function() {
  73. return null;
  74. },
  75. key: function() {
  76. throw new RangeError();
  77. },
  78. removeItem: function() {
  79. },
  80. setItem: function() {
  81. }
  82. };
  83. }
  84. /******************************************************************************/
  85. vAPI.setTimeout = vAPI.setTimeout || window.setTimeout.bind(window);
  86. /******************************************************************************/
  87. })(this);
  88. /******************************************************************************/