endecode.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 {
  26. rstCode.val(hex_md5(srcText));
  27. }
  28. };
  29. /**
  30. * 绑定按钮的点击事件
  31. */
  32. var _bindBtnEvent = function () {
  33. jQuery("#btnCodeChange").click(function () {
  34. _convert();
  35. });
  36. jQuery("#btnCodeClear").click(function () {
  37. jQuery("#srcText,#rstCode").val("")
  38. });
  39. };
  40. /**
  41. * 每个单选按钮被点击时,都自动进行转换
  42. */
  43. var _bindRadioEvent = function () {
  44. jQuery("input[type=radio],label[for]").click(function (evt) {
  45. $this = jQuery(this);
  46. setTimeout(function () {
  47. _convert();
  48. }, 150);
  49. });
  50. };
  51. /**
  52. * 鼠标划过结果框,选中
  53. */
  54. var _bindRstEvent = function () {
  55. jQuery("#rstCode").mouseover(function () {
  56. this.selectionStart = 0;
  57. this.selectionEnd = this.value.length;
  58. this.select();
  59. });
  60. };
  61. var _init = function () {
  62. // 在tab创建或者更新时候,监听事件,看看是否有参数传递过来
  63. chrome.runtime.onMessage.addListener(function (request, sender, callback) {
  64. if (request.type == MSG_TYPE.TAB_CREATED_OR_UPDATED && request.event == 'endecode') {
  65. if (request.content) {
  66. document.getElementById('srcText').value = (request.content);
  67. _convert();
  68. }
  69. }
  70. });
  71. jQuery(function () {
  72. //输入框聚焦
  73. jQuery("#srcText").focus();
  74. //绑定按钮的点击事件
  75. _bindBtnEvent();
  76. //鼠标划过结果框,选中
  77. _bindRstEvent();
  78. //单选按钮的点击事件
  79. _bindRadioEvent();
  80. });
  81. };
  82. return {
  83. init: _init
  84. };
  85. })();
  86. //初始化
  87. baidu.ed.init();