index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. * FeHelper,截图后的保存界面
  3. */
  4. new Vue({
  5. el: '#pageContainer',
  6. data: {
  7. tabList: [],
  8. capturedImage: '',
  9. imageHTML: '',
  10. defaultFilename: Date.now() + '.png',
  11. totalWidth: 100,
  12. totalHeight: 100
  13. },
  14. mounted: function () {
  15. // 在tab创建或者更新时候,监听事件,看看是否有参数传递过来
  16. if (location.protocol === 'chrome-extension:') {
  17. chrome.tabs.query({currentWindow: true,active: true, }, (tabs) => {
  18. let activeTab = tabs.filter(tab => tab.active)[0];
  19. chrome.runtime.sendMessage({
  20. type: 'fh-dynamic-any-thing',
  21. thing: 'request-page-content',
  22. tabId: activeTab.id
  23. }).then(resp => {
  24. if(!resp || !resp.content) return ;
  25. this.showResult(resp.content);
  26. });
  27. });
  28. }
  29. },
  30. methods: {
  31. /**
  32. * 显示结果到Canvas画布
  33. * @param data
  34. */
  35. showResult: function(data){
  36. if (!data || !data.screenshots || !data.screenshots.length) {
  37. alert('截图数据无效,请重试!');
  38. return;
  39. }
  40. const filename = data.filename || 'fe-helper-screenshot.png';
  41. const exportFileName = filename.replace(/\.(jpg|jpeg|png|gif|bmp|webp)$/i, '') + '.png';
  42. this.defaultFilename = exportFileName;
  43. // 处理单张和多张截图的不同情况
  44. if (data.screenshots.length === 1) {
  45. // 单张截图 - 使用提供的尺寸
  46. this._processSingleScreenshot(data);
  47. } else {
  48. // 多张截图 - 垂直拼接
  49. this._processVerticalStitching(data);
  50. }
  51. },
  52. /**
  53. * 处理单张截图
  54. */
  55. _processSingleScreenshot: function(data) {
  56. const screenshot = data.screenshots[0];
  57. const dataUri = screenshot.dataUri || screenshot.dataUrl;
  58. if (!dataUri) {
  59. alert('截图数据无效,请重试!');
  60. return;
  61. }
  62. // 设置canvas尺寸
  63. const canvas = document.getElementById('fehelper_resultCanvas');
  64. if (!canvas) {
  65. alert('无法找到画布元素,请刷新页面重试!');
  66. return;
  67. }
  68. // 使用提供的尺寸或截图的尺寸
  69. const width = data.totalWidth || screenshot.width || screenshot.right - screenshot.left;
  70. const height = data.totalHeight || screenshot.height || screenshot.bottom - screenshot.top;
  71. canvas.width = width;
  72. canvas.height = height;
  73. const ctx = canvas.getContext('2d');
  74. ctx.fillStyle = '#fff';
  75. ctx.fillRect(0, 0, canvas.width, canvas.height);
  76. const img = new Image();
  77. img.onload = () => {
  78. // 绘制到画布
  79. ctx.drawImage(img, 0, 0);
  80. // 显示处理结果
  81. this._showFinalResult(canvas, this.defaultFilename);
  82. };
  83. img.onerror = () => {
  84. alert('加载截图失败,请重试!');
  85. $('#fehelper_loading').hide();
  86. };
  87. img.src = dataUri;
  88. },
  89. /**
  90. * 垂直拼接多张截图
  91. */
  92. _processVerticalStitching: function(data) {
  93. // 将数据排序,确保按正确顺序垂直拼接
  94. // 对于垂直拼接,我们主要关注y坐标和row值
  95. const sortedScreenshots = [...data.screenshots].sort((a, b) => {
  96. // 优先使用row进行排序
  97. if (a.row !== undefined && b.row !== undefined) {
  98. return a.row - b.row;
  99. }
  100. // 如果没有row,则使用top或y坐标排序
  101. if (a.top !== undefined && b.top !== undefined) {
  102. return a.top - b.top;
  103. }
  104. if (a.y !== undefined && b.y !== undefined) {
  105. return a.y - b.y;
  106. }
  107. // 如果以上都没有,则使用索引值
  108. return (a.index || 0) - (b.index || 0);
  109. });
  110. // 加载所有图像
  111. Promise.all(sortedScreenshots.map((screenshot, index) => {
  112. return new Promise((resolve, reject) => {
  113. const dataUri = screenshot.dataUri || screenshot.dataUrl;
  114. if (!dataUri) {
  115. resolve(null);
  116. return;
  117. }
  118. const img = new Image();
  119. img.onload = () => {
  120. // 添加图像对象和计算后的尺寸信息
  121. screenshot.img = img;
  122. screenshot.width = img.width;
  123. screenshot.height = img.height;
  124. resolve(screenshot);
  125. };
  126. img.onerror = () => {
  127. resolve(null);
  128. };
  129. img.src = dataUri;
  130. });
  131. }))
  132. .then(loadedScreenshots => {
  133. const validScreenshots = loadedScreenshots.filter(Boolean);
  134. if (validScreenshots.length === 0) {
  135. alert('没有有效的截图数据,请重试!');
  136. $('#fehelper_loading').hide();
  137. return;
  138. }
  139. // 计算总宽度和总高度
  140. // 总宽度取所有截图中的最大宽度
  141. const totalWidth = Math.max(...validScreenshots.map(s => s.width || s.img.width));
  142. // 总高度为所有截图高度之和
  143. const totalHeight = validScreenshots.reduce((sum, s) => sum + (s.height || s.img.height), 0);
  144. // 设置canvas尺寸
  145. const canvas = document.getElementById('fehelper_resultCanvas');
  146. if (!canvas) {
  147. alert('无法找到画布元素,请刷新页面重试!');
  148. $('#fehelper_loading').hide();
  149. return;
  150. }
  151. canvas.width = totalWidth;
  152. canvas.height = totalHeight;
  153. const ctx = canvas.getContext('2d');
  154. ctx.fillStyle = '#fff';
  155. ctx.fillRect(0, 0, canvas.width, canvas.height);
  156. // 垂直拼接图像
  157. let currentY = 0;
  158. validScreenshots.forEach((screenshot, index) => {
  159. const { img } = screenshot;
  160. const width = screenshot.width || img.width;
  161. const height = screenshot.height || img.height;
  162. // 计算绘制位置,水平居中,垂直方向按顺序拼接
  163. const x = (totalWidth - width) / 2;
  164. // 绘制图像
  165. ctx.drawImage(img, x, currentY);
  166. // 更新Y坐标为下一个位置
  167. currentY += height;
  168. });
  169. // 显示处理结果
  170. this._showFinalResult(canvas, this.defaultFilename);
  171. })
  172. .catch(() => {
  173. alert('处理截图出错,请重试!');
  174. $('#fehelper_loading').hide();
  175. });
  176. },
  177. /**
  178. * 显示最终结果
  179. */
  180. _showFinalResult: function(canvas, filename) {
  181. // 显示保存按钮
  182. $('.btn-save').text(`保存为 ${filename}`).show();
  183. $('.btn-toolbox').show();
  184. // 隐藏img元素,只显示canvas
  185. const resultImg = document.getElementById('fehelper_resultImg');
  186. if (resultImg) {
  187. resultImg.style.display = 'none';
  188. }
  189. // 显示canvas
  190. canvas.style.display = 'block';
  191. // 隐藏加载提示
  192. $('#fehelper_loading').hide();
  193. },
  194. save: function () {
  195. // 请求权限
  196. chrome.permissions.request({
  197. permissions: ['downloads']
  198. }, (granted) => {
  199. if (granted) {
  200. try {
  201. const canvas = document.getElementById('fehelper_resultCanvas');
  202. if (!canvas) {
  203. throw new Error('找不到canvas元素');
  204. }
  205. chrome.downloads.download({
  206. url: canvas.toDataURL('image/png'),
  207. saveAs: true,
  208. conflictAction: 'overwrite',
  209. filename: this.defaultFilename
  210. });
  211. } catch (e) {
  212. alert('保存图片失败: ' + e.message);
  213. }
  214. } else {
  215. alert('必须接受授权,才能正常下载!');
  216. }
  217. });
  218. },
  219. openDonateModal: function(event) {
  220. event.preventDefault();
  221. event.stopPropagation();
  222. chrome.runtime.sendMessage({
  223. type: 'fh-dynamic-any-thing',
  224. thing: 'open-donate-modal',
  225. params: { toolName: 'screenshot' }
  226. });
  227. },
  228. openOptionsPage: function(event) {
  229. event.preventDefault();
  230. event.stopPropagation();
  231. chrome.runtime.openOptionsPage();
  232. }
  233. }
  234. });