bg.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2018 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global singlefile, chrome, FrameTree */
  21. (() => {
  22. const STORE_URLS = ["https://chrome.google.com", "https://addons.mozilla.org"];
  23. const MENU_ID_SAVE_PAGE = "save-page";
  24. const MENU_ID_SAVE_SELECTED = "save-selected";
  25. chrome.tabs.onActivated.addListener(activeInfo => chrome.tabs.get(activeInfo.tabId, tab => singlefile.ui.active(tab.id, isAllowedURL(tab.url))));
  26. chrome.tabs.onCreated.addListener(tab => singlefile.ui.active(tab.id, isAllowedURL(tab.url)));
  27. chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => singlefile.ui.active(tab.id, isAllowedURL(tab.url)));
  28. chrome.tabs.onRemoved.addListener(tabId => singlefile.ui.removed(tabId));
  29. chrome.runtime.onMessage.addListener((request, sender) => {
  30. if (request.processStart || request.processProgress) {
  31. singlefile.ui.progress(sender.tab.id, request.index, request.maxIndex);
  32. }
  33. if (request.processEnd) {
  34. singlefile.ui.end(sender.tab.id);
  35. }
  36. if (request.processError) {
  37. singlefile.ui.error(sender.tab.id);
  38. }
  39. return false;
  40. });
  41. chrome.browserAction.onClicked.addListener(tab => {
  42. if (isAllowedURL(tab.url)) {
  43. chrome.tabs.query({ currentWindow: true, highlighted: true }, tabs => tabs.forEach(processTab));
  44. }
  45. });
  46. chrome.runtime.onInstalled.addListener(function () {
  47. chrome.contextMenus.create({
  48. id: MENU_ID_SAVE_PAGE,
  49. contexts: ["page"],
  50. title: "Save page with SingleFile"
  51. });
  52. chrome.contextMenus.create({
  53. id: MENU_ID_SAVE_SELECTED,
  54. contexts: ["selection"],
  55. title: "Save selection with SingleFile"
  56. });
  57. });
  58. chrome.contextMenus.onClicked.addListener((event, tab) => {
  59. if (event.menuItemId == MENU_ID_SAVE_PAGE) {
  60. processTab(tab);
  61. }
  62. if (event.menuItemId == MENU_ID_SAVE_SELECTED) {
  63. processTab(tab, { selected: true });
  64. }
  65. });
  66. function processTab(tab, processOptions = {}) {
  67. const options = singlefile.config.get();
  68. Object.keys(processOptions).forEach(key => options[key] = processOptions[key]);
  69. options.insertSingleFileComment = true;
  70. singlefile.ui.init(tab.id);
  71. if (options.removeFrames) {
  72. processStart(tab, options);
  73. } else {
  74. FrameTree.initialize(tab.id)
  75. .then(() => processStart(tab, options));
  76. }
  77. }
  78. function processStart(tab, options) {
  79. chrome.tabs.sendMessage(tab.id, { processStart: true, options });
  80. }
  81. function isAllowedURL(url) {
  82. return (url.startsWith("http://") || url.startsWith("https://")) && !STORE_URLS.find(storeUrl => url.startsWith(storeUrl));
  83. }
  84. })();