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.

128 lines
4.6 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. /*******************************************************************************
  2. µMatrix - a Chromium browser extension to black/white list requests.
  3. Copyright (C) 2014 Raymond Hill
  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. /* global vAPI */
  17. /* jshint multistr: true */
  18. // Injected into content pages
  19. /******************************************************************************/
  20. (function() {
  21. 'use strict';
  22. /******************************************************************************/
  23. // https://github.com/chrisaljoudi/uBlock/issues/464
  24. if ( document instanceof HTMLDocument === false ) {
  25. //console.debug('contentscript-start.js > not a HTLMDocument');
  26. return false;
  27. }
  28. // This can happen
  29. if ( !vAPI ) {
  30. //console.debug('contentscript-start.js > vAPI not found');
  31. return;
  32. }
  33. // https://github.com/chrisaljoudi/uBlock/issues/456
  34. // Already injected?
  35. if ( vAPI.contentscriptStartInjected ) {
  36. //console.debug('contentscript-end.js > content script already injected');
  37. return;
  38. }
  39. vAPI.contentscriptStartInjected = true;
  40. /******************************************************************************/
  41. var localMessager = vAPI.messaging.channel('contentscript-start.js');
  42. /******************************************************************************/
  43. // If you play with this code, mind:
  44. // https://github.com/gorhill/httpswitchboard/issues/261
  45. // https://github.com/gorhill/httpswitchboard/issues/252
  46. var navigatorSpoofer = " \
  47. ;(function() { \n \
  48. try { \n \
  49. /* https://github.com/gorhill/uMatrix/issues/61#issuecomment-63814351 */ \n \
  50. var navigator = window.navigator; \n \
  51. var spoofedUserAgent = {{ua-json}}; \n \
  52. if ( spoofedUserAgent === navigator.userAgent ) { \n \
  53. return; \n \
  54. } \n \
  55. var pos = spoofedUserAgent.indexOf('/'); \n \
  56. var appName = pos === -1 ? '' : spoofedUserAgent.slice(0, pos); \n \
  57. var appVersion = pos === -1 ? spoofedUserAgent : spoofedUserAgent.slice(pos + 1); \n \
  58. Object.defineProperty(navigator, 'userAgent', { get: function(){ return spoofedUserAgent; } }); \n \
  59. Object.defineProperty(navigator, 'appName', { get: function(){ return appName; } }); \n \
  60. Object.defineProperty(navigator, 'appVersion', { get: function(){ return appVersion; } }); \n \
  61. } catch (e) { \n \
  62. } \n \
  63. })();";
  64. /******************************************************************************/
  65. // Because window.userAgent is read-only, we need to create a fake Navigator
  66. // object to contain our fake user-agent string.
  67. // Because objects created by a content script are local to the content script
  68. // and not visible to the web page itself (and vice versa), we need the context
  69. // of the web page to create the fake Navigator object directly, and the only
  70. // way to do this is to inject appropriate javascript code into the web page.
  71. var injectNavigatorSpoofer = function(spoofedUserAgent) {
  72. if ( typeof spoofedUserAgent !== 'string' ) {
  73. return;
  74. }
  75. if ( spoofedUserAgent === navigator.userAgent ) {
  76. return;
  77. }
  78. var script = document.createElement('script');
  79. script.type = 'text/javascript';
  80. script.id = 'umatrix-ua-spoofer';
  81. var js = document.createTextNode(navigatorSpoofer.replace('{{ua-json}}', JSON.stringify(spoofedUserAgent)));
  82. script.appendChild(js);
  83. try {
  84. var parent = document.head || document.documentElement;
  85. parent.appendChild(script);
  86. }
  87. catch (e) {
  88. }
  89. // The port will never be used again at this point, disconnecting allows
  90. // to browser to flush this script from memory.
  91. localMessager.close();
  92. };
  93. localMessager.send({
  94. what: 'getUserAgentReplaceStr',
  95. hostname: window.location.hostname
  96. }, injectNavigatorSpoofer);
  97. /******************************************************************************/
  98. /******************************************************************************/
  99. })();
  100. /******************************************************************************/