fe-calc-wpo.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * 计算并保存网页加载时间
  3. * @author zhaoxianlie
  4. */
  5. baidu.calcPageLoadTime = (function(){
  6. var wpoInfo = {};
  7. /**
  8. * 获取页面的http header
  9. * @return {[type]}
  10. */
  11. var getHttpHeaders = function(){
  12. if(wpoInfo.header && wpoInfo.time) {
  13. sendWpoInfo();
  14. }else{
  15. $.ajax({
  16. type: 'GET',
  17. url : window.location.href,
  18. complete: function( xhr,data ){
  19. wpoInfo.header = {
  20. "date" : xhr.getResponseHeader('Date'),
  21. "contentEncoding" : xhr.getResponseHeader('Content-Encoding'),
  22. "connection" : xhr.getResponseHeader('Connection'),
  23. "contentLength" : xhr.getResponseHeader('Content-Length'),
  24. "server" : xhr.getResponseHeader('Server'),
  25. "vary" : xhr.getResponseHeader('Vary'),
  26. "transferEncoding" : xhr.getResponseHeader('Transfer-Encoding'),
  27. "contentType" : xhr.getResponseHeader('Content-Type'),
  28. "cacheControl" : xhr.getResponseHeader('Cache-Control'),
  29. "exprires" : xhr.getResponseHeader('Exprires'),
  30. "lastModified" : xhr.getResponseHeader('Last-Modified')
  31. };
  32. getPageLoadTime();
  33. sendWpoInfo();
  34. }
  35. });
  36. }
  37. };
  38. /**
  39. * 获取网页的加载时间
  40. */
  41. var getPageLoadTime = function(){
  42. wpoInfo.time = performance.timing;
  43. };
  44. /**
  45. * 发送wpo数据
  46. * @return {[type]}
  47. */
  48. var sendWpoInfo = function(){
  49. chrome.extension.sendMessage({
  50. type : MSG_TYPE.CALC_PAGE_LOAD_TIME,
  51. wpo : wpoInfo
  52. });
  53. };
  54. var init = function(){
  55. chrome.extension.onMessage.addListener(function(request,sender,callback){
  56. // 获取页面相关性能数据
  57. if(request.type == MSG_TYPE.GET_PAGE_WPO_INFO) {
  58. (function check() {
  59. (document.readyState == "complete") ? getHttpHeaders() : setTimeout(check, 1000);
  60. })();
  61. }
  62. });
  63. };
  64. return {
  65. init : init
  66. };
  67. })();
  68. //初始化
  69. baidu.calcPageLoadTime.init();