index.js 7.4 KB

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