endecode.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * 注册命名空间:baidu.ed
  3. */
  4. baidu.namespace.register("baidu.ed");
  5. baidu.ed = (function () {
  6. /**
  7. * 转码
  8. */
  9. var _convert = function () {
  10. var srcEl = jQuery("#srcText");
  11. var srcText = srcEl.val();
  12. jQuery("#rst").show();
  13. var rstCode = jQuery("#rstCode");
  14. if (jQuery("#uniEncode").attr("checked") == true) {
  15. rstCode.val(baidu.endecode.uniEncode(srcText));
  16. } else if (jQuery("#uniDecode").attr("checked") == true) {
  17. srcEl.val(srcEl.val().replace(/\\U/g,'\\u'));
  18. rstCode.val(baidu.endecode.uniDecode(srcEl.val()));
  19. } else if (jQuery("#utf8Encode").attr("checked") == true) {
  20. rstCode.val(encodeURIComponent(srcText));
  21. } else if (jQuery("#utf8Decode").attr("checked") == true) {
  22. rstCode.val(decodeURIComponent(srcText));
  23. } else if (jQuery("#base64Encode").attr("checked") == true) {
  24. rstCode.val(baidu.endecode.base64Encode(baidu.endecode.utf8Encode(srcText)));
  25. } else if (jQuery("#base64Decode").attr("checked") == true) {
  26. rstCode.val(baidu.endecode.utf8Decode(baidu.endecode.base64Decode(srcText)));
  27. } else if (jQuery("#md5Encode").attr("checked") == true) {
  28. rstCode.val(hex_md5(srcText));
  29. } else if (jQuery("#html2js").attr("checked") == true) {
  30. rstCode.val(html2js(srcText));
  31. }
  32. };
  33. /**
  34. * 将html代码拼接为js代码
  35. * @returns {string}
  36. */
  37. var html2js = function (txt) {
  38. var htmlArr = txt.replace(/\\/g, "\\\\").replace(/\\/g, "\\/").replace(/\'/g, "\\\'").split('\n');
  39. var len = htmlArr.length;
  40. var outArr = [];
  41. outArr.push("var htmlCodes = [\n");
  42. jQuery.each(htmlArr, function (index, value) {
  43. if (value !== "") {
  44. if (index === len - 1) {
  45. outArr.push("\'" + value + "\'");
  46. } else {
  47. outArr.push("\'" + value + "\',\n");
  48. }
  49. }
  50. });
  51. outArr.push("\n].join(\"\");");
  52. return outArr.join("");
  53. };
  54. /**
  55. * 绑定按钮的点击事件
  56. */
  57. var _bindBtnEvent = function () {
  58. jQuery("#btnCodeChange").click(function () {
  59. _convert();
  60. });
  61. jQuery("#btnCodeClear").click(function () {
  62. jQuery("#srcText,#rstCode").val("")
  63. });
  64. };
  65. /**
  66. * 每个单选按钮被点击时,都自动进行转换
  67. */
  68. var _bindRadioEvent = function () {
  69. jQuery("input[type=radio],label[for]").click(function (evt) {
  70. $this = jQuery(this);
  71. setTimeout(function () {
  72. _convert();
  73. }, 150);
  74. });
  75. };
  76. /**
  77. * 鼠标划过结果框,选中
  78. */
  79. var _bindRstEvent = function () {
  80. jQuery("#rstCode").mouseover(function () {
  81. this.selectionStart = 0;
  82. this.selectionEnd = this.value.length;
  83. this.select();
  84. });
  85. };
  86. var _init = function () {
  87. // 在tab创建或者更新时候,监听事件,看看是否有参数传递过来
  88. chrome.runtime.onMessage.addListener(function (request, sender, callback) {
  89. if (request.type == MSG_TYPE.TAB_CREATED_OR_UPDATED && request.event == 'endecode') {
  90. if (request.content) {
  91. document.getElementById('srcText').value = (request.content);
  92. _convert();
  93. }
  94. }
  95. });
  96. jQuery(function () {
  97. //输入框聚焦
  98. jQuery("#srcText").focus();
  99. //绑定按钮的点击事件
  100. _bindBtnEvent();
  101. //鼠标划过结果框,选中
  102. _bindRstEvent();
  103. //单选按钮的点击事件
  104. _bindRadioEvent();
  105. });
  106. };
  107. return {
  108. init: _init
  109. };
  110. })();
  111. //初始化
  112. baidu.ed.init();