index.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * FeHelper Image Base64 Tools
  3. */
  4. new Vue({
  5. el: '#pageContainer',
  6. data: {
  7. sizeOri: '暂无数据',
  8. sizeBase: '暂无数据',
  9. previewSrc: '',
  10. resultContent: '',
  11. toolName: {'image': '图片转Base64', 'base64': 'Base64转图片'},
  12. curType: 'image',
  13. nextType: 'base64',
  14. txtBase64Input: '',
  15. txtBase64Output: '',
  16. error:''
  17. },
  18. watch: {
  19. txtBase64Input:{
  20. immediate: true,
  21. handler(newVal, oldVal) {
  22. this.error = ''
  23. this.txtBase64Output = ''
  24. if(newVal.length === 0) return
  25. if(newVal.indexOf("data:") === -1) {
  26. this.txtBase64Output = "data:image/jpeg;base64,"+newVal
  27. } else {
  28. this.txtBase64Output = newVal
  29. }
  30. },
  31. }
  32. },
  33. mounted: function () {
  34. let MSG_TYPE = Tarp.require('../static/js/msg_type');
  35. // 在tab创建或者更新时候,监听事件,看看是否有参数传递过来
  36. chrome.runtime.onMessage.addListener((request, sender, callback) => {
  37. if (request.type === MSG_TYPE.TAB_CREATED_OR_UPDATED && request.event === MSG_TYPE.IMAGE_BASE64) {
  38. if (request.content) {
  39. if(this.curType !== 'image') {
  40. this.trans();
  41. }
  42. this.convertOnline(request.content, flag => {
  43. if (!flag) {
  44. chrome.extension.getBackgroundPage().BgPageInstance.notify({
  45. message: '抱歉,' + request.content + ' 对应的图片未转码成功!'
  46. });
  47. }
  48. });
  49. }
  50. }
  51. });
  52. //监听paste事件
  53. document.addEventListener('paste', (event) => {
  54. if (this.curType !== 'image') return;
  55. this.paste(event);
  56. }, false);
  57. // 监听拖拽
  58. document.addEventListener('drop', (event) => {
  59. event.preventDefault();
  60. event.stopPropagation();
  61. if (this.curType !== 'image') return;
  62. let files = event.dataTransfer.files;
  63. if (files.length) {
  64. if (/image\//.test(files[0].type)) {
  65. this._getDataUri(files[0]);
  66. } else {
  67. alert('请选择图片文件!');
  68. }
  69. }
  70. }, false);
  71. document.addEventListener('dragover', (event) => {
  72. if (this.curType !== 'image') return;
  73. event.preventDefault();
  74. event.stopPropagation();
  75. }, false);
  76. },
  77. methods: {
  78. _sizeFormat: function (num) {
  79. if (isNaN(num)) {
  80. return '暂无数据';
  81. }
  82. num = +num;
  83. if (num < 1024) {
  84. return num + ' B';
  85. } else if (num < 1024 * 1024) {
  86. return (num / 1024).toFixed(2) + ' KB';
  87. } else {
  88. return (num / 1024 / 1024).toFixed(2) + ' MB';
  89. }
  90. },
  91. _getDataUri: function (file) {
  92. let reader = new FileReader();
  93. reader.onload = (evt) => {
  94. this.resultContent = evt.target.result;
  95. this.previewSrc = evt.target.result;
  96. this.$refs.panelBox.style.backgroundImage = 'none';
  97. this.sizeOri = this._sizeFormat(file.size);
  98. this.sizeBase = this._sizeFormat(evt.target.result.length);
  99. };
  100. reader.readAsDataURL(file);
  101. },
  102. convertOnline: function (onlineSrc, callback) {
  103. let that = this;
  104. that.previewSrc = onlineSrc;
  105. let image = new Image();
  106. image.src = onlineSrc;
  107. image.onload = function () {
  108. let width = this.naturalWidth;
  109. let height = this.naturalHeight;
  110. // url方式解码失败,再转换成data uri后继续解码
  111. (function createCanvasContext(img, t, l, w, h) {
  112. let canvas = document.createElement('canvas');
  113. canvas.setAttribute('id', 'qr-canvas');
  114. canvas.height = h + 100;
  115. canvas.width = w + 100;
  116. let context = canvas.getContext('2d');
  117. context.fillStyle = 'rgb(255,255,255)';
  118. context.fillRect(0, 0, canvas.width, canvas.height);
  119. context.drawImage(img, l, t, w, h, 50, 50, w, h);
  120. that.resultContent = canvas.toDataURL();
  121. that.$refs.panelBox.style.backgroundImage = 'none';
  122. that.sizeOri = width + 'x' + height;
  123. that.sizeBase = that._sizeFormat(that.resultContent.length);
  124. callback && callback(true);
  125. })(image, 0, 0, width, height);
  126. };
  127. image.onerror = function () {
  128. callback && callback(false);
  129. };
  130. },
  131. convert: function () {
  132. if (this.$refs.fileBox.files.length) {
  133. this._getDataUri(this.$refs.fileBox.files[0]);
  134. this.$refs.fileBox.value = '';
  135. }
  136. },
  137. select: function () {
  138. this.$refs.resultBox.select();
  139. },
  140. upload: function (evt) {
  141. evt.preventDefault();
  142. this.$refs.fileBox.click();
  143. },
  144. paste: function (event) {
  145. let items = event.clipboardData.items || {};
  146. // 优先处理图片
  147. for (let index in items) {
  148. let item = items[index];
  149. if (/image\//.test(item.type)) {
  150. let file = item.getAsFile();
  151. return this._getDataUri(file);
  152. }
  153. }
  154. // 然后处理url
  155. try {
  156. // 逐个遍历
  157. (async () => {
  158. for (let index in items) {
  159. let item = items[index];
  160. if (/text\/plain/.test(item.type)) {
  161. let url = await new Promise(resolve => {
  162. item.getAsString(url => resolve(url))
  163. });
  164. let flag = await new Promise(resolve => {
  165. this.convertOnline(url, flag => resolve(flag));
  166. });
  167. if (flag) break;
  168. }
  169. }
  170. })();
  171. } catch (ex) {
  172. // 只能处理一个了
  173. for (let index in items) {
  174. let item = items[index];
  175. if (/text\/plain/.test(item.type)) {
  176. return item.getAsString(url => {
  177. this.convertOnline(url);
  178. });
  179. }
  180. }
  181. }
  182. },
  183. trans: function () {
  184. this.curType = {image: 'base64', base64: 'image'}[this.curType];
  185. this.nextType = {image: 'base64', base64: 'image'}[this.nextType];
  186. },
  187. loadError: function (e) {
  188. if(this.curType === 'base64' && this.txtBase64Input.trim().length) {
  189. this.error ='无法识别的Base64编码,请确认是正确的图片Data URI?';
  190. }
  191. }
  192. }
  193. });