background.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 isChrome = navigator.vendor.indexOf("Google") != -1;
  23. var isOpera = navigator.vendor.indexOf("Opera") != -1;
  24. var extensionDetected = [];
  25. function detectExtension(extensionId, callback) {
  26. var img;
  27. if (extensionDetected[extensionId])
  28. callback(true);
  29. else {
  30. img = new Image();
  31. img.src = "chrome-extension://" + extensionId + "/resources/icon_16.png";
  32. img.onload = function() {
  33. extensionDetected[extensionId] = true;
  34. callback(true);
  35. };
  36. img.onerror = function() {
  37. extensionDetected[extensionId] = false;
  38. callback(false);
  39. };
  40. }
  41. }
  42. function processable(url) {
  43. return ((isOpera && url.indexOf("https://addons.opera.com") != 0) || (isChrome && url.indexOf("https://chrome.google.com") != 0)) && (url.indexOf("http://") == 0 || url.indexOf("https://") == 0);
  44. }
  45. function process(tabId, url, processSelection, processFrame) {
  46. var SINGLE_FILE_CORE_EXT_ID = dev ? "onlinihoegnbbcmeeocfeplgbkmoidla" : isChrome ? "jemlklgaibiijojffihnhieihhagocma" : "ejmpikefailopkdnglnenfhpepfoghnn";
  47. detectExtension(SINGLE_FILE_CORE_EXT_ID, function(detected) {
  48. if (detected) {
  49. if (processable(url)) {
  50. singlefile.ui.notifyProcessInit(tabId);
  51. chrome.extension.sendMessage(SINGLE_FILE_CORE_EXT_ID, {
  52. processSelection : processSelection,
  53. processFrame : processFrame,
  54. id : tabId,
  55. config : singlefile.config.get()
  56. });
  57. }
  58. } else
  59. chrome.tabs.create({
  60. url : "pages/missingcore.html"
  61. });
  62. });
  63. }
  64. function notifyProcessable(tabId, url, reset) {
  65. singlefile.ui.notifyProcessable(tabId, processable(url), reset);
  66. }
  67. function notifyPageArchiver(request) {
  68. var PAGEARCHIVER_EXT_ID = dev ? "gneihhijimfbdmoieljdpjldkfbfijaa" : "ihkkeoeinpbomhnpkmmkpggkaefincbn";
  69. if (singlefile.config.get().sendToPageArchiver && request.content)
  70. detectExtension(PAGEARCHIVER_EXT_ID, function(detected) {
  71. if (detected)
  72. chrome.extension.sendMessage(PAGEARCHIVER_EXT_ID, request);
  73. });
  74. }
  75. chrome.tabs.onActivated.addListener(function(activeInfo) {
  76. chrome.tabs.get(activeInfo.tabId, function(tab) {
  77. notifyProcessable(tab.id, tab.url);
  78. });
  79. });
  80. chrome.tabs.onCreated.addListener(function(tab) {
  81. notifyProcessable(tab.id, tab.url);
  82. });
  83. chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  84. if (changeInfo.status = "loading" && tab.url)
  85. notifyProcessable(tab.id, tab.url, true);
  86. });
  87. chrome.tabs.onRemoved.addListener(function(tabId) {
  88. singlefile.ui.notifyTabRemoved(tabId);
  89. });
  90. chrome.extension.onMessageExternal.addListener(function(request, sender, sendResponse) {
  91. var blob, url;
  92. if (request.processStart) {
  93. singlefile.ui.notifyProcessStart(request.tabId, request.processingPagesCount);
  94. if (request.blockingProcess)
  95. chrome.tabs.sendMessage(request.tabId, {
  96. processStart : true
  97. });
  98. notifyPageArchiver(request);
  99. }
  100. if (request.processProgress) {
  101. singlefile.ui.notifyProcessProgress(request.index, request.maxIndex);
  102. notifyPageArchiver(request);
  103. }
  104. if (request.processEnd) {
  105. if (request.blockingProcess)
  106. chrome.tabs.sendMessage(request.tabId, {
  107. processEnd : true
  108. });
  109. blob = new Blob([ (new Uint8Array([ 0xEF, 0xBB, 0xBF ])), request.content ], {
  110. type : "text/html"
  111. });
  112. url = webkitURL.createObjectURL(blob);
  113. singlefile.ui.notifyProcessEnd(request.tabId, request.processingPagesCount, singlefile.config.get().displayBanner, url, request.title);
  114. notifyPageArchiver(request);
  115. }
  116. if (request.processError)
  117. singlefile.ui.notifyProcessError(request.tabId);
  118. });
  119. chrome.extension.onMessage.addListener(function(message, sender) {
  120. if (message.closeBanner)
  121. chrome.tabs.sendMessage(sender.tab.id, {
  122. closeBanner : true
  123. });
  124. else
  125. process(sender.tab.id, sender.tab.url, false, false);
  126. });
  127. chrome.browserAction.onClicked.addListener(function(tab) {
  128. process(tab.id, tab.url, false, false);
  129. });
  130. singlefile.refreshMenu = function() {
  131. if (singlefile.config.get().displayInContextMenu) {
  132. chrome.contextMenus.create({
  133. contexts : [ "page" ],
  134. title : "process page",
  135. onclick : function(info, tab) {
  136. process(tab.id, tab.url, false, false);
  137. }
  138. });
  139. chrome.contextMenus.create({
  140. contexts : [ "frame" ],
  141. title : "process frame",
  142. onclick : function(info, tab) {
  143. process(tab.id, tab.url, false, true);
  144. }
  145. });
  146. chrome.contextMenus.create({
  147. contexts : [ "selection" ],
  148. title : "process selection",
  149. onclick : function(info, tab) {
  150. process(tab.id, tab.url, true, false);
  151. }
  152. });
  153. } else
  154. chrome.contextMenus.removeAll();
  155. };
  156. singlefile.refreshMenu();
  157. })();