bg.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. chrome.tabs.onActivated.addListener(activeInfo => chrome.tabs.get(activeInfo.tabId, tab => singlefile.ui.notifyTabActive(tab.id, isAllowedURL(tab.url))));
  24. chrome.tabs.onCreated.addListener(tab => singlefile.ui.notifyTabActive(tab.id, isAllowedURL(tab.url)));
  25. chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => singlefile.ui.notifyTabActive(tab.id, isAllowedURL(tab.url)));
  26. chrome.tabs.onRemoved.addListener(tabId => singlefile.ui.notifyTabRemoved(tabId));
  27. chrome.runtime.onMessage.addListener((request, sender) => {
  28. if (request.processStart) {
  29. singlefile.ui.notifyProcessProgress(sender.tab.id, request.index, request.maxIndex);
  30. }
  31. if (request.processProgress) {
  32. singlefile.ui.notifyProcessProgress(sender.tab.id, request.index, request.maxIndex);
  33. }
  34. if (request.processEnd) {
  35. singlefile.ui.notifyProcessEnd(sender.tab.id);
  36. }
  37. if (request.processError) {
  38. singlefile.ui.notifyProcessError(sender.tab.id);
  39. }
  40. return false;
  41. });
  42. chrome.browserAction.onClicked.addListener(tab => {
  43. if (isAllowedURL(tab.url)) {
  44. singlefile.ui.notifyProcessInit(tab.id);
  45. chrome.tabs.sendMessage(tab.id, { processStart: true, options: singlefile.config.get() });
  46. }
  47. });
  48. function isAllowedURL(url) {
  49. return (url.startsWith("http://") || url.startsWith("https://")) && !url.startsWith(CHROME_STORE_URL);
  50. }
  51. })();