bg.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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, FrameTree */
  21. (() => {
  22. const browser = this.browser || this.chrome;
  23. const STORE_URLS = ["https://chrome.google.com", "https://addons.mozilla.org"];
  24. const MENU_ID_SAVE_PAGE = "save-page";
  25. const MENU_ID_SAVE_SELECTED = "save-selected";
  26. browser.tabs.onActivated.addListener(activeInfo => browser.tabs.get(activeInfo.tabId, tab => singlefile.ui.active(tab.id, isAllowedURL(tab.url))));
  27. browser.tabs.onCreated.addListener(tab => singlefile.ui.active(tab.id, isAllowedURL(tab.url)));
  28. browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => singlefile.ui.active(tab.id, isAllowedURL(tab.url)));
  29. browser.tabs.onRemoved.addListener(tabId => singlefile.ui.removed(tabId));
  30. browser.runtime.onMessage.addListener((request, sender) => {
  31. if (request.processStart || request.processProgress) {
  32. singlefile.ui.progress(sender.tab.id, request.index, request.maxIndex);
  33. }
  34. if (request.processEnd) {
  35. singlefile.ui.end(sender.tab.id);
  36. }
  37. if (request.processError) {
  38. singlefile.ui.error(sender.tab.id);
  39. }
  40. return false;
  41. });
  42. browser.browserAction.onClicked.addListener(tab => {
  43. if (isAllowedURL(tab.url)) {
  44. browser.tabs.query({ currentWindow: true, highlighted: true }, tabs => tabs.forEach(processTab));
  45. }
  46. });
  47. browser.runtime.onInstalled.addListener(function () {
  48. browser.contextMenus.create({
  49. id: MENU_ID_SAVE_PAGE,
  50. contexts: ["page"],
  51. title: "Save page with SingleFile"
  52. });
  53. browser.contextMenus.create({
  54. id: MENU_ID_SAVE_SELECTED,
  55. contexts: ["selection"],
  56. title: "Save selection with SingleFile"
  57. });
  58. });
  59. browser.contextMenus.onClicked.addListener((event, tab) => {
  60. if (event.menuItemId == MENU_ID_SAVE_PAGE) {
  61. processTab(tab);
  62. }
  63. if (event.menuItemId == MENU_ID_SAVE_SELECTED) {
  64. processTab(tab, { selected: true });
  65. }
  66. });
  67. function processTab(tab, processOptions = {}) {
  68. const options = singlefile.config.get();
  69. Object.keys(processOptions).forEach(key => options[key] = processOptions[key]);
  70. options.insertSingleFileComment = true;
  71. singlefile.ui.init(tab.id);
  72. if (options.removeFrames) {
  73. processStart(tab, options);
  74. } else {
  75. FrameTree.initialize(tab.id)
  76. .then(() => processStart(tab, options));
  77. }
  78. }
  79. function processStart(tab, options) {
  80. browser.tabs.sendMessage(tab.id, { processStart: true, options });
  81. }
  82. function isAllowedURL(url) {
  83. return (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file://")) && !STORE_URLS.find(storeUrl => url.startsWith(storeUrl));
  84. }
  85. })();