index.js 9.0 KB

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