loghub-tracking.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // from: http://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/assets/attach/31752/cn_zh/1462870126706/loghub-tracking.js?spm=a2c4g.11186623.2.16.16a65ad1spF4TQ&file=loghub-tracking.js
  2. /* eslint-disable */
  3. (function (window, document) {
  4. function createHttpRequest() {
  5. if (window.ActiveXObject) {
  6. return new ActiveXObject("Microsoft.XMLHTTP");
  7. } else if (window.XMLHttpRequest) {
  8. return new XMLHttpRequest();
  9. }
  10. }
  11. function AliLogTracker(host, project, logstore) {
  12. this.uri_ =
  13. "https://" +
  14. project +
  15. "." +
  16. host +
  17. "/logstores/" +
  18. logstore +
  19. "/track?APIVersion=0.6.0";
  20. this.params_ = new Array();
  21. this.httpRequest_ = createHttpRequest();
  22. }
  23. AliLogTracker.prototype = {
  24. push: function (key, value) {
  25. if (!key || !value) {
  26. return;
  27. }
  28. this.params_.push(key);
  29. this.params_.push(value);
  30. },
  31. logger: async function () {
  32. var url = this.uri_;
  33. var k = 0;
  34. while (this.params_.length > 0) {
  35. if (k % 2 == 0) {
  36. url += "&" + encodeURIComponent(this.params_.shift());
  37. } else {
  38. url += "=" + encodeURIComponent(this.params_.shift());
  39. }
  40. ++k;
  41. }
  42. try {
  43. /**
  44. * 为了不阻塞页面加载,脚本会异步发送HTTP请求,如果页面加载过程中需要多次发送数据,后面的请求会覆盖前面的HTTP请求,
  45. * 看到的现象是浏览器中会显示Web Tracking请求退出。使用同步发送可以避免该问题,将脚本中的内容进行如下替换,可实现同步发送。
  46. *
  47. * https://help.aliyun.com/document_detail/31752.html
  48. */
  49. // this.httpRequest_.open("GET", url, true);
  50. // this.httpRequest_.send(null);
  51. await window.fetch(url);
  52. } catch (ex) {
  53. if (
  54. window &&
  55. window.console &&
  56. typeof window.console.log === "function"
  57. ) {
  58. console.log(
  59. "Failed to log to ali log service because of this exception:\n" + ex
  60. );
  61. console.log("Failed log data:", url);
  62. // wzj-modified: 若上传失败,则重新上传本地日志
  63. if (
  64. location.pathname.startsWith("/oe-web/login") ||
  65. location.pathname === "/oe-web"
  66. ) {
  67. localStorage.setItem("lastLogIndex", 0);
  68. }
  69. }
  70. }
  71. },
  72. };
  73. window.Tracker = AliLogTracker;
  74. })(window, document);