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.

83 lines
2.4 KiB

10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
  1. /*******************************************************************************
  2. uMatrix - a Chromium browser extension to black/white list requests.
  3. Copyright (C) 2014-2016 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. /******************************************************************************/
  17. µMatrix.userAgentSpoofer = (function() {
  18. "use strict";
  19. /******************************************************************************/
  20. var userAgentRandomPicker = function() {
  21. var µm = µMatrix;
  22. var userAgents = µm.userSettings.spoofUserAgentWith.split(/[\n\r]+/);
  23. var i, s, pos;
  24. while ( userAgents.length ) {
  25. i = Math.floor(userAgents.length * Math.random());
  26. s = userAgents[i];
  27. if ( s.charAt(0) === '#' ) {
  28. s = '';
  29. } else {
  30. s = s.trim();
  31. }
  32. if ( s !== '' ) {
  33. return s;
  34. }
  35. userAgents.splice(i, 1);
  36. }
  37. return '';
  38. };
  39. /******************************************************************************/
  40. var userAgentSpoofer = function(force) {
  41. var µm = µMatrix;
  42. var uaStr = µm.userAgentReplaceStr;
  43. var obsolete = Date.now();
  44. if ( !force ) {
  45. obsolete -= µm.userSettings.spoofUserAgentEvery * 60 * 1000;
  46. }
  47. if ( µm.userAgentReplaceStrBirth < obsolete ) {
  48. uaStr = '';
  49. }
  50. if ( uaStr === '' ) {
  51. µm.userAgentReplaceStr = userAgentRandomPicker();
  52. µm.userAgentReplaceStrBirth = Date.now();
  53. }
  54. vAPI.setTimeout(userAgentSpoofer, 120 * 1000);
  55. };
  56. userAgentSpoofer();
  57. /******************************************************************************/
  58. return {
  59. shuffle: function() {
  60. userAgentSpoofer(true);
  61. }
  62. };
  63. })();
  64. /******************************************************************************/