utils.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. String.prototype.getBytes = function () {
  34. var stream = this.replace(/\n/g, 'xx').replace(/\t/g, 'x');
  35. var escapedStr = encodeURIComponent(stream);
  36. return escapedStr.replace(/%[A-Z0-9][A-Z0-9]/g, 'x').length;
  37. }
  38. /**
  39. * 让所有字符串支持空白过滤功能:trim
  40. * @retrn {String} 返回两端无空白的字符串
  41. */
  42. String.prototype.trim = function () {
  43. return this.replace(/^\s*|\s*$/g, "");
  44. };
  45. /**
  46. * 日期格式化
  47. * @param {Object} pattern
  48. */
  49. Date.prototype.format = function (pattern) {
  50. let pad = function (source, length) {
  51. let pre = "",
  52. negative = (source < 0),
  53. string = String(Math.abs(source));
  54. if (string.length < length) {
  55. pre = (new Array(length - string.length + 1)).join('0');
  56. }
  57. return (negative ? "-" : "") + pre + string;
  58. };
  59. if ('string' !== typeof pattern) {
  60. return this.toString();
  61. }
  62. let replacer = function (patternPart, result) {
  63. pattern = pattern.replace(patternPart, result);
  64. };
  65. let year = this.getFullYear(),
  66. month = this.getMonth() + 1,
  67. date2 = this.getDate(),
  68. hours = this.getHours(),
  69. minutes = this.getMinutes(),
  70. seconds = this.getSeconds(),
  71. milliSec = this.getMilliseconds();
  72. replacer(/yyyy/g, pad(year, 4));
  73. replacer(/yy/g, pad(parseInt(year.toString().slice(2), 10), 2));
  74. replacer(/MM/g, pad(month, 2));
  75. replacer(/M/g, month);
  76. replacer(/dd/g, pad(date2, 2));
  77. replacer(/d/g, date2);
  78. replacer(/HH/g, pad(hours, 2));
  79. replacer(/H/g, hours);
  80. replacer(/hh/g, pad(hours % 12, 2));
  81. replacer(/h/g, hours % 12);
  82. replacer(/mm/g, pad(minutes, 2));
  83. replacer(/m/g, minutes);
  84. replacer(/ss/g, pad(seconds, 2));
  85. replacer(/s/g, seconds);
  86. replacer(/SSS/g, pad(milliSec,3));
  87. replacer(/S/g, milliSec);
  88. return pattern;
  89. };
  90. /**
  91. * 自动消失的Alert弹窗
  92. * @param content
  93. */
  94. window.toast = function (content) {
  95. window.clearTimeout(window.feHelperAlertMsgTid);
  96. let elAlertMsg = document.querySelector("#fehelper_alertmsg");
  97. if (!elAlertMsg) {
  98. let elWrapper = document.createElement('div');
  99. elWrapper.innerHTML = '<div id="fehelper_alertmsg" style="position:fixed;top:5px;right:5px;z-index:1000000">' +
  100. '<p style="background:#000;display:inline-block;color:#fff;text-align:center;' +
  101. 'padding:10px 10px;margin:0 auto;font-size:14px;border-radius:4px;">' + content + '</p></div>';
  102. elAlertMsg = elWrapper.childNodes[0];
  103. document.body.appendChild(elAlertMsg);
  104. } else {
  105. elAlertMsg.querySelector('p').innerHTML = content;
  106. elAlertMsg.style.display = 'block';
  107. }
  108. window.feHelperAlertMsgTid = window.setTimeout(function () {
  109. elAlertMsg.style.display = 'none';
  110. }, 3000);
  111. };
  112. /**
  113. * 获取当前脚本的绝对路径
  114. * @returns {string}
  115. */
  116. window.getCurrAbsPath = function () {
  117. let rExtractUri = /((?:http|https|file|chrome-extension):\/\/.*?\/[^:]+)(?::\d+)?:\d+/;
  118. let stack;
  119. try {
  120. a.b();
  121. }
  122. catch (e) {
  123. stack = e.fileName || e.sourceURL || e.stack || e.stacktrace;
  124. }
  125. if (stack) {
  126. return rExtractUri.exec(stack)[1];
  127. }
  128. };