1
0

background.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. }
  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.sendRequest(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) {
  147. var pageData, configScript = "singlefile.config = " + JSON.stringify(config) + "; singlefile.pageId = " + pageId + ";"
  148. + (processSelection ? "singlefile.processSelection = " + processSelection : "");
  149. if (tabs[tabId] && tabs[tabId].processing)
  150. return;
  151. tabs[tabId] = tabs[tabId] || [];
  152. tabs[tabId].processing = true;
  153. pageData = new singlefile.PageData(tabId, pageId, senderId, config, processSelection, function() {
  154. executeScripts(tabId, [ {
  155. code : "var singlefile = {};"
  156. }, {
  157. file : "scripts/common/util.js"
  158. }, {
  159. file : "scripts/common/docprocessor.js"
  160. }, {
  161. code : configScript
  162. }, {
  163. file : "scripts/content/content.js"
  164. } ]);
  165. });
  166. tabs[tabId][pageId] = pageData;
  167. pageId++;
  168. }
  169. function deletePageData(pageData) {
  170. tabs[pageData.tabId][pageData.pageId] = null;
  171. tabs[pageData.tabId] = tabs[pageData.tabId].filter(function(pageData) {
  172. return pageData;
  173. });
  174. if (!tabs[pageData.tabId].length)
  175. tabs[pageData.tabId] = null;
  176. }
  177. chrome.extension.onConnect
  178. .addListener(function(port) {
  179. var tabId = port.sender.tab.id, portPageId = [];
  180. function onDisconnect() {
  181. var pageData = tabs[tabId][portPageId[port.portId_]];
  182. if (!pageData)
  183. return;
  184. pageData.portsId = pageData.portsId.filter(function(id) {
  185. return id != port.portId_;
  186. });
  187. if (!pageData.portsId.length)
  188. if (pageData.processing)
  189. pageData.pendingDelete = true;
  190. else
  191. deletePageData(pageData);
  192. }
  193. function onMessage(message) {
  194. var pageData, docData;
  195. // if (!message.getResourceContentRequest && !message.docProgress)
  196. // console.log("onMessage", message, port.portId_);
  197. if (message.winId) {
  198. portPageId[port.portId_] = message.pageId;
  199. if (message.processInit)
  200. processInit(tabId, port, message);
  201. else {
  202. pageData = tabs[tabId][message.pageId];
  203. docData = pageData.getDocData(message.winId);
  204. if (message.processDocFragment)
  205. pageData.processDocFragment(docData, message.mutationEventId, message.content);
  206. if (message.getResourceContentRequest)
  207. pageData.getResourceContentRequest(message.url, message.requestId, message.winId, message.characterSet, message.mediaTypeParam,
  208. docData);
  209. if (message.docInit)
  210. docInit(pageData, docData, message.maxIndex);
  211. if (message.docProgress)
  212. docProgress(pageData, docData, message.index);
  213. if (message.docEnd)
  214. docEnd(pageData, docData, message.content);
  215. if (message.setFrameContentResponse)
  216. docData.children[message.index].setFrameContentCallback();
  217. if (message.getContentResponse) {
  218. docData.content = message.content;
  219. docData.getContentCallback();
  220. }
  221. if (message.setContentResponse)
  222. setContentResponse(tabId, message.pageId, docData, message.content);
  223. }
  224. }
  225. }
  226. if (port.name == "singlefile") {
  227. port.onMessage.addListener(onMessage);
  228. port.onDisconnect.addListener(onDisconnect);
  229. }
  230. });
  231. chrome.extension.onRequestExternal.addListener(function(request, sender, sendResponse) {
  232. // console.log("onRequestExternal", request);
  233. var property, config = JSON.parse(JSON.stringify(DEFAULT_CONFIG));
  234. if (request.config)
  235. for (property in request.config)
  236. config[property] = request.config[property];
  237. if (request.processSelection)
  238. process(request.id, sender.id, config, true);
  239. else if (request.tabIds)
  240. request.tabIds.forEach(function(tabId) {
  241. process(tabId, sender.id, config);
  242. });
  243. else
  244. process(request.id, sender.id, config);
  245. sendResponse({});
  246. });
  247. })();