index.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. showResult: function(data){
  32. if (data && data.screenshots) {
  33. this.totalWidth = data.totalWidth;
  34. this.totalHeight = data.totalHeight;
  35. let canvas = document.querySelector('#imageEditor>canvas');
  36. let ctx = canvas.getContext('2d');
  37. data.screenshots.forEach((ss) => {
  38. let img = new Image();
  39. img.dx = ss.left;
  40. img.dy = ss.top;
  41. img.onload = function(event){
  42. ctx.drawImage(this,this.dx,this.dy,this.width,this.height);
  43. };
  44. img.src = ss.dataUri;
  45. });
  46. this.defaultFilename = data.filename;
  47. this.$nextTick(() => {
  48. this.$refs.resultBox.scrollIntoView();
  49. });
  50. }
  51. },
  52. save: function () {
  53. // 请求权限
  54. chrome.permissions.request({
  55. permissions: ['downloads']
  56. }, (granted) => {
  57. if (granted) {
  58. chrome.downloads.download({
  59. url: document.querySelector('#imageEditor>canvas').toDataURL(),
  60. saveAs: true,
  61. conflictAction: 'overwrite',
  62. filename: this.defaultFilename
  63. });
  64. } else {
  65. alert('必须接受授权,才能正常下载!');
  66. }
  67. });
  68. }
  69. }
  70. });