utils.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. window.baidu = {
  2. namespace: {
  3. /**
  4. * 注册命名空间
  5. * @param {String} fullNS 完整的命名空间字符串,如baidu.libs.Firefox
  6. * @example baidu.namespace.register("baidu.libs.Firefox");
  7. */
  8. register: function (fullNS) {
  9. //命名空间合法性校验依据
  10. var reg = /^[_$a-z]+[_$a-z0-9]*/i;
  11. // 将命名空间切成N部分, 比如baidu.libs.Firefox等
  12. var nsArray = fullNS.split('.');
  13. var sEval = "";
  14. var sNS = "";
  15. var _tmpObj = [window];
  16. for (var i = 0; i < nsArray.length; i++) {
  17. //命名空间合法性校验
  18. if (!reg.test(nsArray[i])) {
  19. throw new Error("Invalid namespace:" + nsArray[i] + "");
  20. return;
  21. }
  22. _tmpObj[i + 1] = _tmpObj[i][nsArray[i]];
  23. if (typeof _tmpObj[i + 1] == 'undefined') {
  24. _tmpObj[i + 1] = new Object();
  25. }
  26. }
  27. }
  28. }
  29. };
  30. /**
  31. * 获取某字符串的字节数
  32. */
  33. if (!String.prototype.getBytes) {
  34. String.prototype.getBytes = function () {
  35. var stream = this.replace(/\n/g, 'xx').replace(/\t/g, 'x');
  36. var escapedStr = encodeURIComponent(stream);
  37. return escapedStr.replace(/%[A-Z0-9][A-Z0-9]/g, 'x').length;
  38. };
  39. }
  40. /**
  41. * 日期格式化
  42. * @param {Object} pattern
  43. */
  44. if (!Date.prototype.format) {
  45. Date.prototype.format = function (pattern) {
  46. let pad = function (source, length) {
  47. let pre = "",
  48. negative = (source < 0),
  49. string = String(Math.abs(source));
  50. if (string.length < length) {
  51. pre = (new Array(length - string.length + 1)).join('0');
  52. }
  53. return (negative ? "-" : "") + pre + string;
  54. };
  55. if ('string' !== typeof pattern) {
  56. return this.toString();
  57. }
  58. let replacer = function (patternPart, result) {
  59. pattern = pattern.replace(patternPart, result);
  60. };
  61. let year = this.getFullYear(),
  62. month = this.getMonth() + 1,
  63. date2 = this.getDate(),
  64. hours = this.getHours(),
  65. minutes = this.getMinutes(),
  66. seconds = this.getSeconds(),
  67. milliSec = this.getMilliseconds();
  68. replacer(/yyyy/g, pad(year, 4));
  69. replacer(/yy/g, pad(parseInt(year.toString().slice(2), 10), 2));
  70. replacer(/MM/g, pad(month, 2));
  71. replacer(/M/g, month);
  72. replacer(/dd/g, pad(date2, 2));
  73. replacer(/d/g, date2);
  74. replacer(/HH/g, pad(hours, 2));
  75. replacer(/H/g, hours);
  76. replacer(/hh/g, pad(hours % 12, 2));
  77. replacer(/h/g, hours % 12);
  78. replacer(/mm/g, pad(minutes, 2));
  79. replacer(/m/g, minutes);
  80. replacer(/ss/g, pad(seconds, 2));
  81. replacer(/s/g, seconds);
  82. replacer(/SSS/g, pad(milliSec,3));
  83. replacer(/S/g, milliSec);
  84. return pattern;
  85. };
  86. }
  87. /**
  88. * 自动消失的Alert弹窗
  89. * @param content
  90. */
  91. window.toast = function (content) {
  92. window.clearTimeout(window.feHelperAlertMsgTid);
  93. var safe = String(content).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
  94. let elAlertMsg = document.querySelector("#fehelper_alertmsg");
  95. if (!elAlertMsg) {
  96. let elWrapper = document.createElement('div');
  97. elWrapper.innerHTML = '<div id="fehelper_alertmsg" style="position:fixed;top:5px;right:5px;z-index:1000000">' +
  98. '<p style="background:#000;display:inline-block;color:#fff;text-align:center;' +
  99. 'padding:10px 10px;margin:0 auto;font-size:14px;border-radius:4px;">' + safe + '</p></div>';
  100. elAlertMsg = elWrapper.childNodes[0];
  101. document.body.appendChild(elAlertMsg);
  102. } else {
  103. elAlertMsg.querySelector('p').innerHTML = safe;
  104. elAlertMsg.style.display = 'block';
  105. }
  106. window.feHelperAlertMsgTid = window.setTimeout(function () {
  107. elAlertMsg.style.display = 'none';
  108. }, 3000);
  109. };
  110. /**
  111. * 获取当前脚本的绝对路径
  112. * @returns {string}
  113. */
  114. window.getCurrAbsPath = function () {
  115. let rExtractUri = /((?:http|https|file|chrome-extension):\/\/.*?\/[^:]+)(?::\d+)?:\d+/;
  116. let stack;
  117. try {
  118. a.b();
  119. }
  120. catch (e) {
  121. stack = e.fileName || e.sourceURL || e.stack || e.stacktrace;
  122. }
  123. if (stack) {
  124. return rExtractUri.exec(stack)[1];
  125. }
  126. };