background.js 8.8 KB

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