content-script.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * 计算并保存网页加载时间
  3. * @author zhaoxianlie
  4. */
  5. window.pagetimingContentScript = function () {
  6. let __importScript = (filename) => {
  7. pleaseLetJsLoaded = 100;
  8. let url = filename;
  9. if (location.protocol === 'chrome-extension:' || chrome.runtime && chrome.runtime.getURL) {
  10. url = chrome.runtime.getURL('page-timing/' + filename);
  11. }
  12. fetch(url).then(resp => resp.text()).then(jsText => {
  13. if(window.evalCore && window.evalCore.getEvalInstance){
  14. return window.evalCore.getEvalInstance(window)(jsText);
  15. }
  16. let el = document.createElement('script');
  17. el.textContent = jsText;
  18. document.head.appendChild(el);
  19. });
  20. };
  21. __importScript('timing.js');
  22. window.pagetimingNoPage = function() {
  23. let wpoInfo = {
  24. pageInfo: {
  25. title: document.title,
  26. url: location.href
  27. },
  28. time: window.timing.getTimes({simple: true})
  29. };
  30. let sendWpoInfo = function () {
  31. chrome.runtime.sendMessage({
  32. type: 'fh-dynamic-any-thing',
  33. thing: 'set-page-timing-data',
  34. wpoInfo: wpoInfo
  35. });
  36. };
  37. let getHttpHeaders = function () {
  38. if (wpoInfo.header && wpoInfo.time && wpoInfo.pageInfo) {
  39. sendWpoInfo();
  40. } else {
  41. fetch(location.href).then(resp => {
  42. let header = {};
  43. for (let pair of resp.headers.entries()) {
  44. header[pair[0]] = pair[1];
  45. }
  46. return header;
  47. }).then(header => {
  48. wpoInfo.header = header;
  49. sendWpoInfo();
  50. }).catch(console.log);
  51. }
  52. };
  53. let detect = function () {
  54. // 如果是网络地址,才去获取header
  55. if (/^((http)|(https)):\/\//.test(location.href)) {
  56. getHttpHeaders();
  57. } else {
  58. sendWpoInfo();
  59. }
  60. };
  61. detect();
  62. };
  63. };