background.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2011 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. (function() {
  21. var dev = false;
  22. var extensionDetected = [];
  23. function detectExtension(extensionId, callback) {
  24. var img;
  25. if (extensionDetected[extensionId])
  26. callback(true);
  27. else {
  28. img = new Image();
  29. img.src = "chrome-extension://" + extensionId + "/resources/icon_16.png";
  30. img.onload = function() {
  31. extensionDetected[extensionId] = true;
  32. callback(true);
  33. };
  34. img.onerror = function() {
  35. extensionDetected[extensionId] = false;
  36. callback(false);
  37. };
  38. }
  39. }
  40. function processable(url) {
  41. return !url.indexOf("https://chrome.google.com") == 0 && (url.indexOf("http://") == 0 || url.indexOf("https://") == 0);
  42. }
  43. function process(tabId, url, processSelection) {
  44. var SINGLE_FILE_CORE_EXT_ID = dev ? "onlinihoegnbbcmeeocfeplgbkmoidla" : "jemlklgaibiijojffihnhieihhagocma";
  45. detectExtension(SINGLE_FILE_CORE_EXT_ID, function(detected) {
  46. if (detected) {
  47. if (processable(url)) {
  48. singlefile.ui.notifyProcessInit(tabId);
  49. chrome.extension.sendRequest(SINGLE_FILE_CORE_EXT_ID, {
  50. processSelection : processSelection,
  51. id : tabId,
  52. config : singlefile.config.get()
  53. });
  54. }
  55. } else
  56. chrome.tabs.create({
  57. url : "pages/missingcore.html"
  58. });
  59. });
  60. }
  61. function notifyProcessable(tabId, url, reset) {
  62. singlefile.ui.notifyProcessable(tabId, processable(url), reset);
  63. }
  64. function notifyScrapbook(request) {
  65. var SCRAPBOOK_EXT_ID = dev ? "imfajgkkpglkdjkjejkefllgajgmhmfp" : "ihkkeoeinpbomhnpkmmkpggkaefincbn";
  66. if (request.content)
  67. detectExtension(SCRAPBOOK_EXT_ID, function(detected) {
  68. if (detected)
  69. chrome.extension.sendRequest(SCRAPBOOK_EXT_ID, request);
  70. });
  71. }
  72. chrome.tabs.onSelectionChanged.addListener(function() {
  73. chrome.tabs.getSelected(null, function(tab) {
  74. notifyProcessable(tab.id, tab.url);
  75. });
  76. });
  77. chrome.tabs.onCreated.addListener(function(tab) {
  78. notifyProcessable(tab.id, tab.url);
  79. });
  80. chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  81. notifyProcessable(tab.id, tab.url, true);
  82. });
  83. chrome.tabs.onRemoved.addListener(function(tabId) {
  84. singlefile.ui.notifyTabRemoved(tabId);
  85. });
  86. chrome.extension.onRequestExternal.addListener(function(request, sender, sendResponse) {
  87. if (request.processStart) {
  88. singlefile.ui.notifyProcessStart(request.tabId, request.processingPagesCount);
  89. if (request.blockingProcess)
  90. chrome.tabs.sendRequest(request.tabId, {
  91. processStart : true
  92. });
  93. notifyScrapbook(request);
  94. }
  95. if (request.processProgress) {
  96. singlefile.ui.notifyProcessProgress(request.index, request.maxIndex);
  97. notifyScrapbook(request);
  98. }
  99. if (request.pageSaved)
  100. singlefile.ui.notifySavedPage(request.processed, request.filename);
  101. if (request.processEnd) {
  102. if (request.blockingProcess)
  103. chrome.tabs.sendRequest(request.tabId, {
  104. processEnd : true
  105. });
  106. singlefile.ui.notifyProcessEnd(request.tabId, request.processingPagesCount);
  107. notifyScrapbook(request);
  108. }
  109. if (request.processError)
  110. singlefile.ui.notifyProcessError(request.tabId);
  111. });
  112. chrome.extension.onRequest.addListener(function(request, sender) {
  113. process(sender.tab.id, sender.tab.url);
  114. });
  115. chrome.browserAction.onClicked.addListener(function(tab) {
  116. process(tab.id, tab.url);
  117. });
  118. singlefile.refreshMenu = function() {
  119. if (singlefile.config.get().displayInContextMenu) {
  120. chrome.contextMenus.create({
  121. title : "Process page with SingleFile",
  122. onclick : function(info, tab) {
  123. process(tab.id, tab.url);
  124. }
  125. });
  126. chrome.contextMenus.create({
  127. contexts : [ "selection" ],
  128. title : "Process selection with SingleFile",
  129. onclick : function(info, tab) {
  130. process(tab.id, tab.url, true);
  131. }
  132. });
  133. } else
  134. chrome.contextMenus.removeAll();
  135. };
  136. singlefile.refreshMenu();
  137. })();