index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * FeHelper,截图后的保存界面
  3. */
  4. new Vue({
  5. el: '#pageContainer',
  6. data: {
  7. tabList: [],
  8. capturedImage: '',
  9. imageHTML: '',
  10. defaultFilename: Date.now() + '.png'
  11. },
  12. mounted: function () {
  13. this.updateTabList();
  14. this.bindEvent();
  15. },
  16. methods: {
  17. bindEvent: function () {
  18. chrome.tabs.onCreated.addListener(tab => {
  19. if (/^http(s)?:\/\//.test(tab.url)) {
  20. this.tabList.push({
  21. id: tab.id,
  22. title: tab.title,
  23. url: tab.url
  24. });
  25. }
  26. });
  27. chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
  28. this.tabList.some((t, index) => {
  29. if (t.id === tabId) {
  30. t.title = tab.title;
  31. t.url = tab.url;
  32. return true;
  33. }
  34. return false;
  35. });
  36. });
  37. chrome.tabs.onRemoved.addListener(tabId => {
  38. this.tabList.some((tab, index) => {
  39. if (tab.id === tabId) {
  40. this.tabList.splice(index, 1);
  41. return true;
  42. }
  43. return false;
  44. });
  45. });
  46. chrome.runtime.onMessage.addListener((request, sender, callback) => {
  47. if (request.type === 'page-screenshot-done') {
  48. let capturedData = request.data;
  49. if (capturedData && capturedData.fileSystemUrl) {
  50. this.capturedImage = capturedData.fileSystemUrl;
  51. this.imageHTML = `<img class="img-result" src="${this.capturedImage}" />`;
  52. this.defaultFilename = capturedData.filename;
  53. this.$nextTick(() => {
  54. this.$refs.resultBox.scrollIntoView();
  55. });
  56. chrome.tabs.get(capturedData.pageInfo.id, tab => {
  57. this.tabList.some(t => {
  58. if (t.id === tab.id) {
  59. t.title = tab.title;
  60. return true;
  61. }
  62. return false;
  63. });
  64. });
  65. }
  66. }
  67. callback && callback();
  68. return true;
  69. });
  70. },
  71. updateTabList: function () {
  72. chrome.tabs.query({windowId: chrome.windows.WINDOW_ID_CURRENT}, tabs => {
  73. this.tabList = tabs.filter(tab => {
  74. return /^http(s)?:\/\//.test(tab.url)
  75. }).map(tab => {
  76. return {
  77. id: tab.id,
  78. title: tab.title,
  79. url: tab.url
  80. };
  81. });
  82. });
  83. },
  84. goCapture: function (tabId, captureType) {
  85. chrome.tabs.getCurrent(curTab => {
  86. chrome.tabs.query({windowId: chrome.windows.WINDOW_ID_CURRENT}, (tabs) => {
  87. let found = tabs.some(tab => tab.id === tabId);
  88. if (found) {
  89. chrome.tabs.update(tabId, {highlighted: true, active: true}, tab => {
  90. // 没有文件就只注入脚本
  91. // chrome.scripting.executeScript({
  92. // target: {tab.id, allFrames: false},
  93. // func: function(code){evalCore.getEvalInstance(window)(code)},
  94. // args: [codeConfig.code]
  95. // }, function () {
  96. // try {
  97. // callback && callback.apply(this, arguments);
  98. // } catch (e) {
  99. // callback && callback(null);
  100. // }
  101. // });
  102. chrome.tabs.executeScript(tab.id, {
  103. code: '(' + (function (tabInfo, captureInfo) {
  104. chrome.runtime.sendMessage({
  105. type: 'fh-dynamic-any-thing',
  106. params: {
  107. tabInfo: tabInfo,
  108. captureInfo: captureInfo
  109. },
  110. func: ((params, callback) => {
  111. try {
  112. callback && callback(params);
  113. } catch (e) {
  114. callback && callback(null);
  115. }
  116. return true;
  117. }).toString()
  118. }, (params) => {
  119. let func = window['screenshotContentScript'];
  120. func && func(params)();
  121. });
  122. }).toString() + ')(' + JSON.stringify(tab) + ',' + JSON.stringify({
  123. resultTab: curTab.id,
  124. captureType: captureType
  125. }) + ')',
  126. allFrames: false
  127. });
  128. });
  129. } else {
  130. alert('页面已关闭');
  131. }
  132. });
  133. });
  134. },
  135. save: function () {
  136. // 请求权限
  137. chrome.permissions.request({
  138. permissions: ['downloads']
  139. }, (granted) => {
  140. if (granted) {
  141. chrome.downloads.download({
  142. url: this.capturedImage,
  143. saveAs: true,
  144. conflictAction: 'overwrite',
  145. filename: this.defaultFilename
  146. });
  147. } else {
  148. alert('必须接受授权,才能正常下载!');
  149. }
  150. });
  151. }
  152. }
  153. });