background.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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, processFrame) {
  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.sendMessage(SINGLE_FILE_CORE_EXT_ID, {
  50. processSelection : processSelection,
  51. processFrame : processFrame,
  52. id : tabId,
  53. config : singlefile.config.get()
  54. });
  55. }
  56. } else
  57. chrome.tabs.create({
  58. url : "pages/missingcore.html"
  59. });
  60. });
  61. }
  62. function notifyProcessable(tabId, url, reset) {
  63. singlefile.ui.notifyProcessable(tabId, processable(url), reset);
  64. }
  65. function notifyPageArchiver(request) {
  66. var PAGEARCHIVER_EXT_ID = dev ? "gneihhijimfbdmoieljdpjldkfbfijaa" : "ihkkeoeinpbomhnpkmmkpggkaefincbn";
  67. if (singlefile.config.get().sendToPageArchiver && request.content)
  68. detectExtension(PAGEARCHIVER_EXT_ID, function(detected) {
  69. if (detected)
  70. chrome.extension.sendMessage(PAGEARCHIVER_EXT_ID, request);
  71. });
  72. }
  73. chrome.tabs.onActivated.addListener(function(activeInfo) {
  74. chrome.tabs.get(activeInfo.tabId, function(tab) {
  75. notifyProcessable(tab.id, tab.url);
  76. });
  77. });
  78. chrome.tabs.onCreated.addListener(function(tab) {
  79. notifyProcessable(tab.id, tab.url);
  80. });
  81. chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  82. if (changeInfo.status = "loading" && tab.url)
  83. notifyProcessable(tab.id, tab.url, true);
  84. });
  85. chrome.tabs.onRemoved.addListener(function(tabId) {
  86. singlefile.ui.notifyTabRemoved(tabId);
  87. });
  88. chrome.extension.onMessageExternal.addListener(function(request, sender, sendResponse) {
  89. var blob, url;
  90. if (request.processStart) {
  91. singlefile.ui.notifyProcessStart(request.tabId, request.processingPagesCount);
  92. if (request.blockingProcess)
  93. chrome.tabs.sendMessage(request.tabId, {
  94. processStart : true
  95. });
  96. notifyPageArchiver(request);
  97. }
  98. if (request.processProgress) {
  99. singlefile.ui.notifyProcessProgress(request.index, request.maxIndex);
  100. notifyPageArchiver(request);
  101. }
  102. if (request.processEnd) {
  103. if (request.blockingProcess)
  104. chrome.tabs.sendMessage(request.tabId, {
  105. processEnd : true
  106. });
  107. blob = new Blob([ (new Uint8Array([ 0xEF, 0xBB, 0xBF ])), request.content ], {
  108. type : "text/html"
  109. });
  110. url = webkitURL.createObjectURL(blob);
  111. singlefile.ui.notifyProcessEnd(request.tabId, request.processingPagesCount, singlefile.config.get().displayNotification,
  112. singlefile.config.get().displayBanner, url, request.title);
  113. notifyPageArchiver(request);
  114. }
  115. if (request.processError)
  116. singlefile.ui.notifyProcessError(request.tabId);
  117. });
  118. chrome.extension.onMessage.addListener(function(message, sender) {
  119. if (message.closeBanner)
  120. chrome.tabs.sendMessage(sender.tab.id, {
  121. closeBanner : true
  122. });
  123. else
  124. process(tabId, port.sender.tab.url, false, false);
  125. });
  126. chrome.browserAction.onClicked.addListener(function(tab) {
  127. process(tab.id, tab.url, false, false);
  128. });
  129. singlefile.refreshMenu = function() {
  130. if (singlefile.config.get().displayInContextMenu) {
  131. chrome.contextMenus.create({
  132. contexts : [ "page" ],
  133. title : "process page",
  134. onclick : function(info, tab) {
  135. process(tab.id, tab.url, false, false);
  136. }
  137. });
  138. chrome.contextMenus.create({
  139. contexts : [ "frame" ],
  140. title : "process frame",
  141. onclick : function(info, tab) {
  142. process(tab.id, tab.url, false, true);
  143. }
  144. });
  145. chrome.contextMenus.create({
  146. contexts : [ "selection" ],
  147. title : "process selection",
  148. onclick : function(info, tab) {
  149. process(tab.id, tab.url, true, false);
  150. }
  151. });
  152. } else
  153. chrome.contextMenus.removeAll();
  154. };
  155. singlefile.refreshMenu();
  156. })();