background.js 8.5 KB

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