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.

214 lines
6.5 KiB

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