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.

206 lines
6.3 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-2018 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. vAPI.setTimeout = vAPI.setTimeout || window.setTimeout.bind(window);
  30. /******************************************************************************/
  31. vAPI.webextFlavor = {
  32. major: 0,
  33. soup: new Set()
  34. };
  35. (function() {
  36. var ua = navigator.userAgent,
  37. flavor = vAPI.webextFlavor,
  38. soup = flavor.soup;
  39. var dispatch = function() {
  40. window.dispatchEvent(new CustomEvent('webextFlavor'));
  41. };
  42. // This is always true.
  43. soup.add('ublock');
  44. if ( /\bMobile\b/.test(ua) ) {
  45. soup.add('mobile');
  46. }
  47. // Asynchronous
  48. var async = self.browser instanceof Object &&
  49. typeof self.browser.runtime.getBrowserInfo === 'function';
  50. if ( async ) {
  51. self.browser.runtime.getBrowserInfo().then(function(info) {
  52. flavor.major = parseInt(info.version, 10) || 0;
  53. soup.add(info.vendor.toLowerCase())
  54. .add(info.name.toLowerCase());
  55. if ( flavor.major >= 53 ) { soup.add('user_stylesheet'); }
  56. if ( flavor.major >= 57 ) { soup.add('html_filtering'); }
  57. dispatch();
  58. });
  59. }
  60. // Synchronous
  61. var match = /Firefox\/([\d.]+)/.exec(ua);
  62. if ( match !== null ) {
  63. flavor.major = parseInt(match[1], 10) || 0;
  64. soup.add('mozilla')
  65. .add('firefox');
  66. if ( flavor.major >= 53 ) { soup.add('user_stylesheet'); }
  67. if ( flavor.major >= 57 ) { soup.add('html_filtering'); }
  68. } else {
  69. match = /OPR\/([\d.]+)/.exec(ua);
  70. if ( match !== null ) {
  71. var reEx = /Chrom(?:e|ium)\/([\d.]+)/;
  72. if ( reEx.test(ua) ) { match = reEx.exec(ua); }
  73. flavor.major = parseInt(match[1], 10) || 0;
  74. soup.add('opera').add('chromium');
  75. } else {
  76. match = /Chromium\/([\d.]+)/.exec(ua);
  77. if ( match !== null ) {
  78. flavor.major = parseInt(match[1], 10) || 0;
  79. soup.add('chromium');
  80. } else {
  81. match = /Chrome\/([\d.]+)/.exec(ua);
  82. if ( match !== null ) {
  83. flavor.major = parseInt(match[1], 10) || 0;
  84. soup.add('google').add('chromium');
  85. }
  86. }
  87. }
  88. // https://github.com/gorhill/uBlock/issues/3588
  89. if ( soup.has('chromium') && flavor.major >= 67 ) {
  90. soup.add('user_stylesheet');
  91. }
  92. }
  93. // Don't starve potential listeners
  94. if ( !async ) {
  95. vAPI.setTimeout(dispatch, 97);
  96. }
  97. })();
  98. /******************************************************************************/
  99. // http://www.w3.org/International/questions/qa-scripts#directions
  100. var setScriptDirection = function(language) {
  101. document.body.setAttribute(
  102. 'dir',
  103. ['ar', 'he', 'fa', 'ps', 'ur'].indexOf(language) !== -1 ? 'rtl' : 'ltr'
  104. );
  105. };
  106. /******************************************************************************/
  107. vAPI.download = function(details) {
  108. if ( !details.url ) {
  109. return;
  110. }
  111. var a = document.createElement('a');
  112. a.href = details.url;
  113. a.setAttribute('download', details.filename || '');
  114. a.dispatchEvent(new MouseEvent('click'));
  115. };
  116. /******************************************************************************/
  117. vAPI.getURL = chrome.runtime.getURL;
  118. /******************************************************************************/
  119. vAPI.i18n = chrome.i18n.getMessage;
  120. setScriptDirection(vAPI.i18n('@@ui_locale'));
  121. /******************************************************************************/
  122. vAPI.closePopup = function() {
  123. window.close();
  124. };
  125. /******************************************************************************/
  126. // A localStorage-like object which should be accessible from the
  127. // background page or auxiliary pages.
  128. // This storage is optional, but it is nice to have, for a more polished user
  129. // experience.
  130. // https://github.com/gorhill/uBlock/issues/2824
  131. // Use a dummy localStorage if for some reasons it's not available.
  132. // https://github.com/gorhill/uMatrix/issues/840
  133. // Always use a wrapper to seamlessly handle exceptions
  134. vAPI.localStorage = {
  135. clear: function() {
  136. try {
  137. window.localStorage.clear();
  138. } catch(ex) {
  139. }
  140. },
  141. getItem: function(key) {
  142. try {
  143. return window.localStorage.getItem(key);
  144. } catch(ex) {
  145. }
  146. return null;
  147. },
  148. removeItem: function(key) {
  149. try {
  150. window.localStorage.removeItem(key);
  151. } catch(ex) {
  152. }
  153. },
  154. setItem: function(key, value) {
  155. try {
  156. window.localStorage.setItem(key, value);
  157. } catch(ex) {
  158. }
  159. }
  160. };
  161. /******************************************************************************/
  162. })(this);
  163. /******************************************************************************/