index.js 11 KB

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