perf.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. window.Perf = (function($, Perf) {
  2. $.extend(Perf, {
  3. reportUrl: 'stats/',
  4. revUrl: 'stats/rev.php',
  5. // should be defined before report or poll are run
  6. currentRev: undefined,
  7. report: function( data, after ) {
  8. $.extend(data, {
  9. pathname: location.pathname,
  10. agent: this.agent(),
  11. agentFull: window.navigator.userAgent,
  12. agentVersion: this.agentVersion()
  13. });
  14. $.post( this.reportUrl, data, after );
  15. },
  16. poll: function() {
  17. var self = this;
  18. setInterval(function() {
  19. $.get( self.revUrl, function( data ) {
  20. // if there's a new revision refresh or currentRev isn't being set
  21. if( self.currentRev && self.currentRev !== data ){
  22. location.href = location.href;
  23. }
  24. });
  25. }, 60000);
  26. },
  27. setCurrentRev: function() {
  28. var self = this;
  29. $.get( self.revUrl, function( data ) {
  30. self.currentRev = data;
  31. });
  32. },
  33. agent: function() {
  34. var agent = window.navigator.userAgent;
  35. for( name in this.agents ) {
  36. if( agent.indexOf( this.agents[name] ) > -1 ) {
  37. return this.agents[name];
  38. }
  39. }
  40. return agent;
  41. },
  42. agentVersion: function() {
  43. var agent = window.navigator.userAgent;
  44. agent.search(this.vRegexs[this.agent()] || "");
  45. return RegExp.$1 ? RegExp.$1 : "0.0";
  46. },
  47. agents: {
  48. ANDROID: "Android",
  49. WP: "Windows Phone OS",
  50. IPHONE: "iPhone OS",
  51. IPAD: "iPad; U; CPU OS",
  52. BLACKBERRY: "BlackBerry"
  53. },
  54. vRegexs: {}
  55. });
  56. Perf.vRegexs[Perf.agents.ANDROID] = /Android ([0-9].[0-9])(.[0-9])?/;
  57. Perf.vRegexs[Perf.agents.WP] = /Windows Phone OS ([0-9].[0-9]);/;
  58. Perf.vRegexs[Perf.agents.IPHONE] = /iPhone OS ([0-9]_[0-9])/;
  59. Perf.vRegexs[Perf.agents.IPAD] = /iPad; ?U?;? CPU OS ([0-9]_[0-9])/;
  60. Perf.vRegexs[Perf.agents.BLACKBERRY] = /BlackBerry ([0-9]{4})/;
  61. return Perf;
  62. })(jQuery, window.Perf || {});