background.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright 2011 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile Core.
  6. *
  7. * SingleFile Core 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 Core 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 Core. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. (function() {
  21. var DEFAULT_CONFIG = {
  22. removeFrames : false,
  23. removeScripts : true,
  24. removeObjects : true,
  25. removeHidden : false,
  26. removeUnusedCSSRules : false,
  27. displayProcessedPage : false,
  28. processInBackground : true,
  29. getContent : true,
  30. getRawDoc : false
  31. };
  32. var tabs = singlefile.tabs = [], processingPagesCount = 0, pageId = 0;
  33. function executeScripts(tabId, scripts, callback, index) {
  34. if (!index)
  35. index = 0;
  36. if (index < scripts.length)
  37. chrome.tabs.executeScript(tabId, {
  38. file : scripts[index].file,
  39. code : scripts[index].code,
  40. allFrames : true
  41. }, function() {
  42. executeScripts(tabId, scripts, callback, index + 1);
  43. });
  44. else if (callback)
  45. callback();
  46. }
  47. function processInit(tabId, port, message) {
  48. var pageData = tabs[tabId][message.pageId];
  49. pageData.portsId.push(port.portId_);
  50. if (!pageData.getDocData(message.winId))
  51. pageData.processDoc(port, message.topWindow, message.winId, message.index, message.content, message.title, message.url, message.baseURI,
  52. message.characterSet, message.canvasData, message.contextmenuTime, {
  53. init : docInit,
  54. progress : docProgress,
  55. end : docEnd
  56. });
  57. }
  58. function setContentResponse(tabId, pageId, docData, content) {
  59. var pageData = tabs[tabId][pageId];
  60. processingPagesCount--;
  61. chrome.extension.sendMessage(pageData.senderId, {
  62. processEnd : true,
  63. tabId : tabId,
  64. pageId : pageId,
  65. blockingProcess : !pageData.config.processInBackground || pageData.config.displayProcessedPage,
  66. processingPagesCount : processingPagesCount,
  67. content : pageData.config.getContent ? content : null,
  68. url : pageData.url,
  69. title : pageData.title
  70. });
  71. if (!pageData.config.processInBackground || pageData.config.displayProcessedPage) {
  72. pageData.processing = false;
  73. tabs[tabId].processing = false;
  74. }
  75. if (pageData.pendingDelete)
  76. deletePageData(pageData);
  77. }
  78. function docInit(pageData, docData, maxIndex) {
  79. function pageInit() {
  80. delete pageData.timeoutPageInit;
  81. pageData.processableDocs = pageData.initializedDocs;
  82. pageData.initProcess();
  83. processingPagesCount++;
  84. chrome.extension.sendMessage(pageData.senderId, {
  85. processStart : true,
  86. tabId : pageData.tabId,
  87. pageId : pageData.pageId,
  88. blockingProcess : !pageData.config.processInBackground || pageData.config.displayProcessedPage,
  89. processingPagesCount : processingPagesCount
  90. });
  91. if (pageData.config.processInBackground && !pageData.config.displayProcessedPage) {
  92. tabs[pageData.tabId].processing = false;
  93. pageData.processing = false;
  94. }
  95. }
  96. if (!docData.initialized) {
  97. docData.initialized = true;
  98. if (pageData.initializedDocs != pageData.processableDocs) {
  99. docData.progressMax = maxIndex;
  100. pageData.initializedDocs++;
  101. if (pageData.timeoutPageInit)
  102. clearTimeout(pageData.timeoutPageInit);
  103. pageData.timeoutPageInit = setTimeout(pageInit, 5000);
  104. if (pageData.initializedDocs == pageData.processableDocs || pageData.processSelection || pageData.config.removeFrames
  105. || pageData.config.getRawDoc) {
  106. clearTimeout(pageData.timeoutPageInit);
  107. pageInit();
  108. }
  109. }
  110. }
  111. }
  112. function docProgress(pageData, docData, index) {
  113. var progressIndex = 0, progressMax = 0;
  114. docData.progressIndex = index;
  115. tabs.forEach(function(tabData) {
  116. if (tabData) {
  117. tabData.progressIndex = 0;
  118. tabData.progressMax = 0;
  119. tabData.forEach(function(pageData) {
  120. if (pageData) {
  121. pageData.computeProgress();
  122. tabData.progressIndex += pageData.progressIndex;
  123. tabData.progressMax += pageData.progressMax;
  124. }
  125. });
  126. progressIndex += tabData.progressIndex;
  127. progressMax += tabData.progressMax;
  128. }
  129. });
  130. chrome.extension.sendMessage(pageData.senderId, {
  131. processProgress : true,
  132. tabId : pageData.tabId,
  133. pageId : pageData.pageId,
  134. pageIndex : pageData.progressIndex,
  135. pageMaxIndex : pageData.progressMax,
  136. tabIndex : tabs[pageData.tabId].progressIndex,
  137. tabMaxIndex : tabs[pageData.tabId].progressMax,
  138. index : progressIndex,
  139. maxIndex : progressMax
  140. });
  141. }
  142. function docEnd(pageData, docData, content) {
  143. pageData.setDocContent(docData, content, setContentResponse);
  144. }
  145. function process(tabId, senderId, config, processSelection, processFrame) {
  146. var pageData, configScript;
  147. if (processFrame) {
  148. config.processInBackground = true;
  149. config.removeFrames = false;
  150. }
  151. configScript = "singlefile.config = " + JSON.stringify(config) + "; singlefile.pageId = " + pageId + ";"
  152. + (processSelection ? "singlefile.processSelection = " + processSelection : "");
  153. if (tabs[tabId] && tabs[tabId].processing)
  154. return;
  155. tabs[tabId] = tabs[tabId] || [];
  156. tabs[tabId].processing = true;
  157. pageData = new singlefile.PageData(tabId, pageId, senderId, config, processSelection, processFrame, function() {
  158. executeScripts(tabId, [ {
  159. code : "var singlefile = {};"
  160. }, {
  161. file : "scripts/common/util.js"
  162. }, {
  163. file : "scripts/common/docprocessor.js"
  164. }, {
  165. code : configScript
  166. }, {
  167. file : "scripts/content/content.js"
  168. } ]);
  169. });
  170. tabs[tabId][pageId] = pageData;
  171. pageId++;
  172. }
  173. function deletePageData(pageData) {
  174. delete tabs[pageData.tabId][pageData.pageId];
  175. tabs[pageData.tabId] = tabs[pageData.tabId].filter(function(pageData) {
  176. return pageData;
  177. });
  178. if (!tabs[pageData.tabId].length)
  179. delete tabs[pageData.tabId];
  180. }
  181. function onConnect(port) {
  182. var tabId = port.sender.tab.id, portPageId = [];
  183. function onDisconnect() {
  184. var pageData = tabs[tabId][portPageId[port.portId_]];
  185. if (!pageData)
  186. return;
  187. pageData.portsId = pageData.portsId.filter(function(id) {
  188. return id != port.portId_;
  189. });
  190. if (!pageData.portsId.length)
  191. if (pageData.processing)
  192. pageData.pendingDelete = true;
  193. else
  194. deletePageData(pageData);
  195. }
  196. function onMessage(message) {
  197. var pageData, docData;
  198. // if (!message.getResourceContentRequest && !message.docProgress)
  199. // console.log("onMessage", message, port.portId_);
  200. if (message.winId) {
  201. portPageId[port.portId_] = message.pageId;
  202. if (message.processInit)
  203. processInit(tabId, port, message);
  204. else {
  205. pageData = tabs[tabId][message.pageId];
  206. docData = pageData.getDocData(message.winId);
  207. if (message.processDocFragment)
  208. pageData.processDocFragment(docData, message.mutationEventId, message.content);
  209. if (message.getResourceContentRequest)
  210. pageData
  211. .getResourceContentRequest(message.url, message.requestId, message.winId, message.characterSet, message.mediaTypeParam, docData);
  212. if (message.docInit)
  213. docInit(pageData, docData, message.maxIndex);
  214. if (message.docProgress)
  215. docProgress(pageData, docData, message.index);
  216. if (message.docEnd)
  217. docEnd(pageData, docData, message.content);
  218. if (message.setFrameContentResponse)
  219. docData.children[message.index].setFrameContentCallback();
  220. if (message.getContentResponse) {
  221. docData.content = message.content;
  222. docData.getContentCallback();
  223. }
  224. if (message.setContentResponse)
  225. setContentResponse(tabId, message.pageId, docData, message.content);
  226. }
  227. }
  228. }
  229. if (port.name == "singlefile") {
  230. port.onMessage.addListener(onMessage);
  231. port.onDisconnect.addListener(onDisconnect);
  232. }
  233. }
  234. function onMessageExternal(request, sender, sendResponse) {
  235. // console.log("onMessageExternal", request);
  236. var property, config = JSON.parse(JSON.stringify(DEFAULT_CONFIG));
  237. if (request.config)
  238. for (property in request.config)
  239. config[property] = request.config[property];
  240. if (request.processSelection)
  241. process(request.id, sender.id, config, true, false);
  242. else if (request.processFrame)
  243. process(request.id, sender.id, config, false, true);
  244. else if (request.tabIds)
  245. request.tabIds.forEach(function(tabId) {
  246. process(tabId, sender.id, config, false, false);
  247. });
  248. else
  249. process(request.id, sender.id, config, false, false);
  250. sendResponse({});
  251. }
  252. chrome.extension.onConnect.addListener(onConnect);
  253. chrome.extension.onMessageExternal.addListener(onMessageExternal);
  254. })();