endecode.js 3.9 KB

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