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.

84 lines
2.5 KiB

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. /******************************************************************************/
  17. µMatrix.userAgentSpoofer = (function() {
  18. /******************************************************************************/
  19. var userAgentRandomPicker = function() {
  20. var µm = µMatrix;
  21. var userAgents = µm.userSettings.spoofUserAgentWith.split(/[\n\r]+/);
  22. var i, s, pos;
  23. while ( userAgents.length ) {
  24. i = Math.floor(userAgents.length * Math.random());
  25. s = userAgents[i];
  26. if ( s.charAt(0) === '#' ) {
  27. s = '';
  28. } else {
  29. s = s.trim();
  30. }
  31. if ( s !== '' ) {
  32. return s;
  33. }
  34. userAgents.splice(i, 1);
  35. }
  36. return '';
  37. };
  38. /******************************************************************************/
  39. var userAgentSpoofer = function(force) {
  40. var µm = µMatrix;
  41. var uaStr = µm.userAgentReplaceStr;
  42. var obsolete = Date.now();
  43. if ( !force ) {
  44. obsolete -= µm.userSettings.spoofUserAgentEvery * 60 * 1000;
  45. }
  46. if ( µm.userAgentReplaceStrBirth < obsolete ) {
  47. uaStr = '';
  48. }
  49. if ( uaStr === '' ) {
  50. µm.userAgentReplaceStr = userAgentRandomPicker();
  51. µm.userAgentReplaceStrBirth = Date.now();
  52. }
  53. };
  54. // Prime spoofer
  55. userAgentSpoofer();
  56. /******************************************************************************/
  57. µMatrix.asyncJobs.add('userAgentSwitcher', null, userAgentSpoofer, 120 * 1000, true);
  58. /******************************************************************************/
  59. return {
  60. shuffle: function() {
  61. userAgentSpoofer(true);
  62. }
  63. };
  64. })();
  65. /******************************************************************************/