bg.js 2.9 KB

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