background.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 extensionDetected = [];
  24. function detectExtension(extensionId, callback) {
  25. var img;
  26. if (extensionDetected[extensionId])
  27. callback(true);
  28. else {
  29. img = new Image();
  30. img.src = "chrome-extension://" + extensionId + "/resources/icon_16.png";
  31. img.onload = function() {
  32. extensionDetected[extensionId] = true;
  33. callback(true);
  34. };
  35. img.onerror = function() {
  36. extensionDetected[extensionId] = false;
  37. callback(false);
  38. };
  39. }
  40. }
  41. function processable(url) {
  42. return ((navigator.vendor.indexOf("Opera") != -1 && url.indexOf("https://addons.opera.com") != 0) || (navigator.vendor.indexOf("Google") != -1 && url.indexOf("https://chrome.google.com") != 0)) && (url.indexOf("http://") == 0 || url.indexOf("https://") == 0);
  43. }
  44. function process(tabId, url, processSelection, processFrame) {
  45. var SINGLE_FILE_CORE_EXT_ID = dev ? "onlinihoegnbbcmeeocfeplgbkmoidla" : isChrome ? "jemlklgaibiijojffihnhieihhagocma" : "ejmpikefailopkdnglnenfhpepfoghnn";
  46. detectExtension(SINGLE_FILE_CORE_EXT_ID, function(detected) {
  47. if (detected) {
  48. if (processable(url)) {
  49. singlefile.ui.notifyProcessInit(tabId);
  50. chrome.extension.sendMessage(SINGLE_FILE_CORE_EXT_ID, {
  51. processSelection : processSelection,
  52. processFrame : processFrame,
  53. id : tabId,
  54. config : singlefile.config.get()
  55. });
  56. }
  57. } else
  58. chrome.tabs.create({
  59. url : "pages/missingcore.html"
  60. });
  61. });
  62. }
  63. function notifyProcessable(tabId, url, reset) {
  64. singlefile.ui.notifyProcessable(tabId, processable(url), reset);
  65. }
  66. function notifyPageArchiver(request) {
  67. var PAGEARCHIVER_EXT_ID = dev ? "gneihhijimfbdmoieljdpjldkfbfijaa" : "ihkkeoeinpbomhnpkmmkpggkaefincbn";
  68. if (singlefile.config.get().sendToPageArchiver && request.content)
  69. detectExtension(PAGEARCHIVER_EXT_ID, function(detected) {
  70. if (detected)
  71. chrome.extension.sendMessage(PAGEARCHIVER_EXT_ID, request);
  72. });
  73. }
  74. chrome.tabs.onActivated.addListener(function(activeInfo) {
  75. chrome.tabs.get(activeInfo.tabId, function(tab) {
  76. notifyProcessable(tab.id, tab.url);
  77. });
  78. });
  79. chrome.tabs.onCreated.addListener(function(tab) {
  80. notifyProcessable(tab.id, tab.url);
  81. });
  82. chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  83. if (changeInfo.status = "loading" && tab.url)
  84. notifyProcessable(tab.id, tab.url, true);
  85. });
  86. chrome.tabs.onRemoved.addListener(function(tabId) {
  87. singlefile.ui.notifyTabRemoved(tabId);
  88. });
  89. chrome.extension.onMessageExternal.addListener(function(request, sender, sendResponse) {
  90. var blob, url;
  91. if (request.processStart) {
  92. singlefile.ui.notifyProcessStart(request.tabId, request.processingPagesCount);
  93. if (request.blockingProcess)
  94. chrome.tabs.sendMessage(request.tabId, {
  95. processStart : true
  96. });
  97. notifyPageArchiver(request);
  98. }
  99. if (request.processProgress) {
  100. singlefile.ui.notifyProcessProgress(request.index, request.maxIndex);
  101. notifyPageArchiver(request);
  102. }
  103. if (request.processEnd) {
  104. if (request.blockingProcess)
  105. chrome.tabs.sendMessage(request.tabId, {
  106. processEnd : true
  107. });
  108. blob = new Blob([ (new Uint8Array([ 0xEF, 0xBB, 0xBF ])), request.content ], {
  109. type : "text/html"
  110. });
  111. url = webkitURL.createObjectURL(blob);
  112. singlefile.ui.notifyProcessEnd(request.tabId, request.processingPagesCount, 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(sender.tab.id, 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. })();