fe-decode.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * QR码解码
  3. */
  4. var qrDecode = (function () {
  5. "use strict";
  6. var _show = function(text){
  7. var el = $('#__fehelper_qr_decode__');
  8. if(!el[0]){
  9. el = $('<div id="__fehelper_qr_decode__" style="z-index:999999;position: fixed;left:0;top:0;right: 0;bottom: 0;display: none;">' +
  10. '<div style="position: fixed;left:0;top:0;right: 0;bottom: 0;background: #000;opacity: 0.5;"></div>' +
  11. '<div style="position: relative;top: 100px;left: ' + ($('body').width() / 2 - 200) + 'px;border:1px solid #000;background:#fff;width:420px;padding:15px;border-radius:5px 5px;box-shadow:2px 2px 5px #000;">' +
  12. '<div style="margin: 0 0 10px 0;font-size: 14px;font-weight: bold;">二维码解码结果:</div>' +
  13. '<textarea style="display:block;border-radius:5px 5px;width:398px;border:1px solid #aaa;min-height:80px;resize:none;box-shadow:2px 2px 5px #aaa;padding:10px;font-size:14px;color:#888;"></textarea>' +
  14. '<div style="margin-top:10px;">' +
  15. '<span id="__fehelper_qr_msg_" style="float: right;color:#f00;display:none;">复制成功!</span>' +
  16. '<a id="__fehelper_qr_copy_" style="margin-right:20px;color: #00f;text-decoration: underline" href="#">复制</a>' +
  17. '<a id="__fehelper_qr_close_" style="margin-top:10px;color: #00f;text-decoration: underline" href="#">关闭</a>' +
  18. '</div></div>' +
  19. '</div>').appendTo('body');
  20. el.find('a#__fehelper_qr_copy_').click(function(e){
  21. e.preventDefault();
  22. el.find('textarea').select();
  23. document.execCommand('Copy');
  24. el.find('#__fehelper_qr_msg_').show().delay(2000).hide('slow');
  25. });
  26. el.find('a#__fehelper_qr_close_').click(function(e){
  27. e.preventDefault();
  28. el.hide('slow');
  29. });
  30. }
  31. if(text == 'error decoding QR Code') {
  32. text = '抱歉,二维码识别失败!';
  33. }
  34. el.show('slow').find('textarea').val(text);
  35. };
  36. var _init = function () {
  37. // 在tab创建或者更新时候,监听事件,看看是否有参数传递过来
  38. chrome.runtime.onMessage.addListener(function (request, sender, callback) {
  39. if (request.type == MSG_TYPE.QR_DECODE) {
  40. _show(request.result);
  41. }
  42. });
  43. };
  44. return {
  45. init: _init
  46. };
  47. })();
  48. qrDecode.init();