utils.js 4.3 KB

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