wininfo.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. var wininfo = {};
  21. (function() {
  22. var EXT_ID = "wininfo";
  23. var contentRequestCallbacks, executeSetFramesWinIdString = executeSetFramesWinId.toString(), processLength, processIndex, timeoutProcess, timeoutInit;
  24. function addListener(onMessage) {
  25. function windowMessageListener(event) {
  26. var data = event.data;
  27. if (typeof data === "string" && data.indexOf(EXT_ID + "::") == 0)
  28. onMessage(JSON.parse(data.substr(EXT_ID.length + 2)));
  29. }
  30. this.addEventListener("message", windowMessageListener, false);
  31. }
  32. function executeSetFramesWinId(extensionId, selector, index, winId) {
  33. function execute(extensionId, elements, index, winId, win) {
  34. var i, framesInfo = [], stringify = JSON.stringify || JSON.encode, parse = JSON.parse || JSON.decode;
  35. function getDoctype(doc) {
  36. var docType = doc.doctype, docTypeStr;
  37. if (docType) {
  38. docTypeStr = "<!DOCTYPE " + docType.nodeName;
  39. if (docType.publicId) {
  40. docTypeStr += " PUBLIC \"" + docType.publicId + "\"";
  41. if (docType.systemId)
  42. docTypeStr += " \"" + docType.systemId + "\"";
  43. } else if (docType.systemId)
  44. docTypeStr += " SYSTEM \"" + docType.systemId + "\"";
  45. if (docType.internalSubset)
  46. docTypeStr += " [" + docType.internalSubset + "]";
  47. return docTypeStr + ">\n";
  48. }
  49. return "";
  50. }
  51. function addListener(onMessage) {
  52. function windowMessageListener(event) {
  53. var data = event.data;
  54. if (typeof data === "string" && data.indexOf(extensionId + "::") == 0)
  55. onMessage(parse(data.substr(extensionId.length + 2)));
  56. }
  57. top.addEventListener("message", windowMessageListener, false);
  58. }
  59. for (i = 0; i < elements.length; i++) {
  60. framesInfo.push({
  61. sameDomain : elements[i].contentDocument != null,
  62. src : elements[i].src,
  63. winId : winId + "." + i,
  64. index : i
  65. });
  66. }
  67. if (win != top)
  68. win.postMessage(extensionId + "::" + stringify({
  69. initResponse : true,
  70. winId : winId,
  71. index : index
  72. }), "*");
  73. top.postMessage(extensionId + "::" + stringify({
  74. initResponse : true,
  75. frames : framesInfo,
  76. winId : winId,
  77. index : index
  78. }), "*");
  79. for (i = 0; i < elements.length; i++)
  80. (function(index) {
  81. var frameElement = elements[i], frameWinId = winId + "." + index, frameDoc = frameElement.contentDocument;
  82. function onMessage(message) {
  83. if (message.getContentRequest) {
  84. var customEvent, doctype;
  85. if (message.winId == frameWinId) {
  86. doctype = getDoctype(frameDoc);
  87. top.postMessage(extensionId + "::" + stringify({
  88. getContentResponse : true,
  89. contentRequestId : message.contentRequestId,
  90. winId : frameWinId,
  91. content : doctype + frameDoc.documentElement.outerHTML,
  92. title : frameDoc.title,
  93. baseURI : frameDoc.baseURI,
  94. url : frameDoc.location.href,
  95. characterSet : "UTF-8"
  96. }), "*");
  97. }
  98. }
  99. }
  100. if (frameDoc && top.addEventListener) {
  101. execute(extensionId, frameDoc.querySelectorAll(selector), index, frameWinId, frameElement.contentWindow);
  102. addListener(onMessage);
  103. } else {
  104. frameElement.contentWindow.postMessage(extensionId + "::" + stringify({
  105. initRequest : true,
  106. winId : frameWinId,
  107. index : index
  108. }), "*");
  109. }
  110. })(i);
  111. }
  112. execute(extensionId, document.querySelectorAll(selector), index, winId, window);
  113. }
  114. function getContent(frame, callback) {
  115. if (frame.sameDomain) {
  116. top.postMessage(EXT_ID + "::" + JSON.stringify({
  117. getContentRequest : true,
  118. winId : frame.winId,
  119. contentRequestId : contentRequestCallbacks.length
  120. }), "*");
  121. contentRequestCallbacks.push(callback);
  122. } else
  123. callback();
  124. }
  125. function getContentResponse(message) {
  126. var id = message.contentRequestId;
  127. delete message.contentRequestId;
  128. delete message.getContentResponse;
  129. contentRequestCallbacks[id](message);
  130. }
  131. function initRequest(message) {
  132. wininfo.winId = message.winId;
  133. wininfo.index = message.index;
  134. timeoutInit = setTimeout(function() {
  135. initResponse({
  136. initResponse : true,
  137. frames : [],
  138. winId : message.winId,
  139. index : message.index
  140. });
  141. }, 3000);
  142. location.href = "javascript:(" + executeSetFramesWinIdString + ")('" + EXT_ID + "','iframe, frame'," + wininfo.index + ",'" + wininfo.winId
  143. + "'); void 0;";
  144. }
  145. function initResponse(message) {
  146. function process() {
  147. bgPort = chrome.extension.connect({
  148. name : "wininfo"
  149. });
  150. wininfo.frames = wininfo.frames.filter(function(frame) {
  151. return frame.winId;
  152. });
  153. bgPort.postMessage({
  154. initResponse : true,
  155. processableDocs : wininfo.frames.length + 1
  156. });
  157. }
  158. if (timeoutInit) {
  159. clearTimeout(timeoutInit);
  160. timeoutInit = null;
  161. }
  162. if (window == top) {
  163. message.frames = message.frames instanceof Array ? message.frames : JSON.parse(message.frames);
  164. if (message.winId != "0")
  165. processIndex++;
  166. wininfo.frames = wininfo.frames.concat(message.frames);
  167. processLength += message.frames.length;
  168. if (timeoutProcess)
  169. clearTimeout(timeoutProcess);
  170. if (processIndex == processLength)
  171. process();
  172. else
  173. timeoutProcess = setTimeout(function() {
  174. process();
  175. }, 200);
  176. } else {
  177. wininfo.winId = message.winId;
  178. wininfo.index = message.index;
  179. }
  180. }
  181. function onRequest(request) {
  182. // console.log("onRequest", request);
  183. if (request.initRequest && this == top && document.documentElement instanceof HTMLHtmlElement) {
  184. contentRequestCallbacks = [];
  185. processLength = 0;
  186. processIndex = 0;
  187. timeoutProcess = null;
  188. wininfo.frames = [];
  189. initRequest(request);
  190. }
  191. }
  192. function onMessage(message) {
  193. // console.log("wininfo", "onMessage", message, window.location.href);
  194. if (message.initRequest)
  195. initRequest(message);
  196. if (message.initResponse)
  197. initResponse(message);
  198. if (message.getContentResponse)
  199. getContentResponse(message);
  200. }
  201. if (window == top)
  202. wininfo.getContent = getContent;
  203. chrome.extension.onRequest.addListener(onRequest);
  204. addEventListener("contextmenu", function() {
  205. window.contextmenuTime = (new Date()).getTime();
  206. }, false);
  207. addListener(onMessage);
  208. })();