index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. openOptionsPage(event) {
  82. event.preventDefault();
  83. event.stopPropagation();
  84. chrome.runtime.openOptionsPage();
  85. },
  86. _sizeFormat: function (num) {
  87. if (isNaN(num)) {
  88. return '暂无数据';
  89. }
  90. num = +num;
  91. if (num < 1024) {
  92. return num + ' B';
  93. } else if (num < 1024 * 1024) {
  94. return (num / 1024).toFixed(2) + ' KB';
  95. } else {
  96. return (num / 1024 / 1024).toFixed(2) + ' MB';
  97. }
  98. },
  99. _getDataUri: function (file) {
  100. let reader = new FileReader();
  101. reader.onload = (evt) => {
  102. this.resultContent = evt.target.result;
  103. this.previewSrc = evt.target.result;
  104. this.$refs.panelBox.style.backgroundImage = 'none';
  105. this.sizeOri = this._sizeFormat(file.size);
  106. this.sizeBase = this._sizeFormat(evt.target.result.length);
  107. };
  108. reader.readAsDataURL(file);
  109. },
  110. convertOnline: function (onlineSrc, callback) {
  111. let that = this;
  112. that.previewSrc = onlineSrc;
  113. let image = new Image();
  114. image.src = onlineSrc;
  115. image.onload = function () {
  116. let width = this.naturalWidth;
  117. let height = this.naturalHeight;
  118. // url方式解码失败,再转换成data uri后继续解码
  119. (function createCanvasContext(img, t, l, w, h) {
  120. let canvas = document.createElement('canvas');
  121. canvas.setAttribute('id', 'qr-canvas');
  122. canvas.height = h + 100;
  123. canvas.width = w + 100;
  124. let context = canvas.getContext('2d');
  125. context.fillStyle = 'rgb(255,255,255)';
  126. context.fillRect(0, 0, canvas.width, canvas.height);
  127. context.drawImage(img, l, t, w, h, 50, 50, w, h);
  128. that.resultContent = canvas.toDataURL();
  129. that.$refs.panelBox.style.backgroundImage = 'none';
  130. that.sizeOri = width + 'x' + height;
  131. that.sizeBase = that._sizeFormat(that.resultContent.length);
  132. callback && callback(true);
  133. })(image, 0, 0, width, height);
  134. };
  135. image.onerror = function () {
  136. callback && callback(false);
  137. };
  138. },
  139. convert: function () {
  140. if (this.$refs.fileBox.files.length) {
  141. this._getDataUri(this.$refs.fileBox.files[0]);
  142. this.$refs.fileBox.value = '';
  143. }
  144. },
  145. select: function () {
  146. this.$refs.resultBox.select();
  147. },
  148. upload: function (evt) {
  149. evt.preventDefault();
  150. this.$refs.fileBox.click();
  151. },
  152. paste: function (event) {
  153. let items = event.clipboardData.items || {};
  154. // 优先处理图片
  155. for (let index in items) {
  156. let item = items[index];
  157. if (/image\//.test(item.type)) {
  158. let file = item.getAsFile();
  159. return this._getDataUri(file);
  160. }
  161. }
  162. // 然后处理url
  163. try {
  164. // 逐个遍历
  165. (async () => {
  166. for (let index in items) {
  167. let item = items[index];
  168. if (/text\/plain/.test(item.type)) {
  169. let url = await new Promise(resolve => {
  170. item.getAsString(url => resolve(url))
  171. });
  172. let flag = await new Promise(resolve => {
  173. this.convertOnline(url, flag => resolve(flag));
  174. });
  175. if (flag) break;
  176. }
  177. }
  178. })();
  179. } catch (ex) {
  180. // 只能处理一个了
  181. for (let index in items) {
  182. let item = items[index];
  183. if (/text\/plain/.test(item.type)) {
  184. return item.getAsString(url => {
  185. this.convertOnline(url);
  186. });
  187. }
  188. }
  189. }
  190. },
  191. trans: function () {
  192. this.curType = {image: 'base64', base64: 'image'}[this.curType];
  193. this.nextType = {image: 'base64', base64: 'image'}[this.nextType];
  194. },
  195. loadError: function (e) {
  196. if (this.curType === 'base64' && this.txtBase64Input.trim().length) {
  197. this.error = ('无法识别的Base64编码,请确认是正确的图片Data URI?');
  198. }
  199. },
  200. // 打开打赏页面
  201. openDonateModal: function(event){
  202. event.preventDefault();
  203. event.stopPropagation();
  204. chrome.runtime.sendMessage({
  205. type: 'fh-dynamic-any-thing',
  206. thing: 'open-donate-modal',
  207. params: { toolName: 'image-base64' }
  208. });
  209. },
  210. }
  211. });