vapi-common.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*******************************************************************************
  2. ηMatrix - a browser extension to black/white list requests.
  3. Copyright (C) 2014-2019 The uMatrix/uBlock Origin authors
  4. Copyright (C) 2019-2022 Alessio Vanni
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see {http://www.gnu.org/licenses/}.
  15. Home: https://gitlab.com/vannilla/ematrix
  16. uMatrix Home: https://github.com/gorhill/uMatrix
  17. */
  18. // For background page or non-background pages
  19. 'use strict';
  20. let vAPI = {};
  21. const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
  22. Cu.import('resource://gre/modules/Services.jsm');
  23. (function (self) {
  24. if (self.vAPI === undefined) {
  25. self.vAPI = vAPI;
  26. }
  27. vAPI.setTimeout = vAPI.setTimeout || function (callback, delay, extra) {
  28. return setTimeout(function (a) {
  29. callback(a);
  30. }, delay, extra);
  31. };
  32. // http://www.w3.org/International/questions/qa-scripts#directions
  33. let setScriptDirection = function(language) {
  34. let dir =
  35. ['ar', 'he', 'fa', 'ps', 'ur'].indexOf(language) !== -1
  36. ? 'rtl'
  37. : 'ltr';
  38. document.body.setAttribute('dir', dir);
  39. };
  40. vAPI.download = function (details) {
  41. if (!details.url) {
  42. return;
  43. }
  44. let a = document.createElement('a');
  45. a.href = details.url;
  46. a.setAttribute('download', details.filename || '');
  47. a.dispatchEvent(new MouseEvent('click'));
  48. };
  49. vAPI.insertHTML = (function () {
  50. const parser = Cc['@mozilla.org/parserutils;1']
  51. .getService(Ci.nsIParserUtils);
  52. // https://github.com/gorhill/uBlock/issues/845
  53. // Apparently dashboard pages execute with `about:blank` principal.
  54. return function (node, html) {
  55. while (node.firstChild) {
  56. node.removeChild(node.firstChild);
  57. }
  58. let parsed =
  59. parser.parseFragment(html,
  60. parser.SanitizerAllowStyle,
  61. false,
  62. Services.io.newURI('about:blank',
  63. null, null),
  64. document.documentElement);
  65. node.appendChild(parsed);
  66. };
  67. })();
  68. vAPI.getURL = function (path) {
  69. return 'chrome://'
  70. + location.host
  71. + '/content/'
  72. + path.replace(/^\/+/, '');
  73. };
  74. vAPI.i18n = (function () {
  75. let stringBundle =
  76. Services.strings.createBundle('chrome://'
  77. + location.host
  78. + '/locale/messages.properties');
  79. return function (s) {
  80. try {
  81. return stringBundle.GetStringFromName(s);
  82. } catch (ex) {
  83. return '';
  84. }
  85. };
  86. })();
  87. setScriptDirection(navigator.language);
  88. vAPI.closePopup = function() {
  89. sendAsyncMessage(location.host + ':closePopup');
  90. };
  91. // A localStorage-like object which should be accessible from the
  92. // background page or auxiliary pages.
  93. // This storage is optional, but it is nice to have, for a more polished user
  94. // experience.
  95. vAPI.localStorage = {
  96. pbName: '',
  97. pb: null,
  98. str: Cc['@mozilla.org/supports-string;1']
  99. .createInstance(Ci.nsISupportsString),
  100. init: function (pbName) {
  101. this.pbName = pbName;
  102. this.pb = Services.prefs.getBranch(pbName);
  103. },
  104. getItem: function (key) {
  105. try {
  106. return this.pb
  107. .getComplexValue(key,
  108. Ci.nsISupportsString).data;
  109. } catch (ex) {
  110. return null;
  111. }
  112. },
  113. setItem: function (key, value) {
  114. this.str.data = value;
  115. this.pb.setComplexValue(key,
  116. Ci.nsISupportsString,
  117. this.str);
  118. },
  119. getBool: function (key) {
  120. try {
  121. return this.pb.getBoolPref(key);
  122. } catch (ex) {
  123. return null;
  124. }
  125. },
  126. setBool: function (key, value) {
  127. this.pb.setBoolPref(key, value);
  128. },
  129. setDefaultBool: function (key, defaultValue) {
  130. Services.prefs.getDefaultBranch(this.pbName)
  131. .setBoolPref(key, defaultValue);
  132. },
  133. removeItem: function (key) {
  134. this.pb.clearUserPref(key);
  135. },
  136. clear: function () {
  137. this.pb.deleteBranch('');
  138. }
  139. };
  140. vAPI.localStorage.init('extensions.' + location.host + '.');
  141. })(this);