bg.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 => {
  24. if (!chrome.runtime.lastError) {
  25. singlefile.ui.active(tab.id, isAllowedURL(tab.url));
  26. }
  27. }));
  28. chrome.tabs.onCreated.addListener(tab => singlefile.ui.active(tab.id, isAllowedURL(tab.url)));
  29. chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => singlefile.ui.active(tab.id, isAllowedURL(tab.url)));
  30. chrome.tabs.onRemoved.addListener(tabId => singlefile.ui.removed(tabId));
  31. chrome.runtime.onMessage.addListener((request, sender) => {
  32. if (request.processStart || request.processProgress) {
  33. singlefile.ui.progress(sender.tab.id, request.index, request.maxIndex);
  34. }
  35. if (request.processEnd) {
  36. singlefile.ui.end(sender.tab.id);
  37. }
  38. if (request.processError) {
  39. singlefile.ui.error(sender.tab.id);
  40. }
  41. return false;
  42. });
  43. chrome.browserAction.onClicked.addListener(tab => {
  44. if (isAllowedURL(tab.url)) {
  45. chrome.tabs.query({ currentWindow: true, highlighted: true }, tabs => tabs.forEach(tab => {
  46. singlefile.ui.init(tab.id);
  47. chrome.tabs.sendMessage(tab.id, { processStart: true, options: singlefile.config.get() });
  48. }));
  49. }
  50. });
  51. function isAllowedURL(url) {
  52. return (url.startsWith("http://") || url.startsWith("https://")) && !url.startsWith(CHROME_STORE_URL);
  53. }
  54. })();