|
|
@@ -18,7 +18,7 @@
|
|
|
* along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
|
|
|
*/
|
|
|
|
|
|
-/* global window, top, document, addEventListener, docHelper, timeout, MessageChannel, superFetch, fetch, TextDecoder, DOMParser, lazyLoader, setTimeout, browser */
|
|
|
+/* global window, top, document, addEventListener, docHelper, MessageChannel, lazyLoader, browser, setTimeout */
|
|
|
|
|
|
this.frameTree = this.frameTree || (() => {
|
|
|
|
|
|
@@ -29,7 +29,6 @@ this.frameTree = this.frameTree || (() => {
|
|
|
const INIT_RESPONSE_MESSAGE = "initResponse";
|
|
|
const TARGET_ORIGIN = "*";
|
|
|
const TIMEOUT_INIT_REQUEST_MESSAGE = 750;
|
|
|
- const PREFIX_VALID_FRAME_URL = /^https?:\/\//;
|
|
|
const TOP_WINDOW_ID = "0";
|
|
|
const WINDOW_ID_SEPARATOR = ".";
|
|
|
const TOP_WINDOW = window == top;
|
|
|
@@ -93,12 +92,13 @@ this.frameTree = this.frameTree || (() => {
|
|
|
|
|
|
function initRequest(message) {
|
|
|
const sessionId = message.sessionId;
|
|
|
- const frameElements = document.querySelectorAll(FRAMES_CSS_SELECTOR);
|
|
|
if (!TOP_WINDOW) {
|
|
|
windowId = message.windowId;
|
|
|
+ }
|
|
|
+ processFrames(document.querySelectorAll(FRAMES_CSS_SELECTOR), message.options, windowId, sessionId);
|
|
|
+ if (!TOP_WINDOW) {
|
|
|
sendInitResponse({ framesData: [getFrameData(document, window, windowId, message.options)], sessionId });
|
|
|
}
|
|
|
- processFrames(frameElements, message.options, windowId, sessionId);
|
|
|
}
|
|
|
|
|
|
function cleanupRequest(message) {
|
|
|
@@ -163,16 +163,7 @@ this.frameTree = this.frameTree || (() => {
|
|
|
/* ignored */
|
|
|
}
|
|
|
setTimeout(async () => {
|
|
|
- let frameDoc;
|
|
|
- if (frameElement.src && frameElement.src.match(PREFIX_VALID_FRAME_URL)) {
|
|
|
- frameDoc = await getFrameDoc(frameElement.src, parentWindowId);
|
|
|
- }
|
|
|
- if (frameDoc) {
|
|
|
- sendInitResponse({ framesData: [getFrameData(frameDoc, null, windowId, options)] });
|
|
|
- timeout.set(() => sendInitResponse({ framesData: [{ windowId, processed: true }], sessionId }));
|
|
|
- } else {
|
|
|
- sendInitResponse({ framesData: [{ windowId, processed: true }], sessionId });
|
|
|
- }
|
|
|
+ sendInitResponse({ framesData: [{ windowId, processed: true }], sessionId });
|
|
|
}, TIMEOUT_INIT_REQUEST_MESSAGE);
|
|
|
});
|
|
|
sendInitResponse({ framesData, sessionId });
|
|
|
@@ -252,57 +243,6 @@ this.frameTree = this.frameTree || (() => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- async function getFrameDoc(frameUrl, parentWindowId) {
|
|
|
- let frameContent;
|
|
|
- try {
|
|
|
- frameContent = await ((typeof superFetch !== "undefined" && superFetch.fetch) || fetch)(frameUrl);
|
|
|
- } catch (error) {
|
|
|
- /* ignored */
|
|
|
- }
|
|
|
- if (frameContent && frameContent.status >= 400 && superFetch.hostFetch) {
|
|
|
- try {
|
|
|
- frameContent = await superFetch.hostFetch(frameUrl);
|
|
|
- } catch (error) {
|
|
|
- /* ignored */
|
|
|
- }
|
|
|
- }
|
|
|
- if (frameContent) {
|
|
|
- const contentType = frameContent.headers && frameContent.headers.get("content-type");
|
|
|
- let charset, mimeType;
|
|
|
- if (contentType) {
|
|
|
- const matchContentType = contentType.toLowerCase().split(";");
|
|
|
- mimeType = matchContentType[0].trim();
|
|
|
- if (!mimeType.includes("/")) {
|
|
|
- mimeType = "text/html";
|
|
|
- }
|
|
|
- const charsetValue = matchContentType[1] && matchContentType[1].trim();
|
|
|
- if (charsetValue) {
|
|
|
- const matchCharset = charsetValue.match(/^charset=(.*)/);
|
|
|
- if (matchCharset) {
|
|
|
- charset = docHelper.removeQuotes(matchCharset[1]);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- let doc;
|
|
|
- try {
|
|
|
- const buffer = await frameContent.arrayBuffer();
|
|
|
- const content = (new TextDecoder(charset)).decode(buffer);
|
|
|
- const domParser = new DOMParser();
|
|
|
- doc = domParser.parseFromString(content, mimeType);
|
|
|
- } catch (error) {
|
|
|
- /* ignored */
|
|
|
- }
|
|
|
- if (doc) {
|
|
|
- const frameElements = doc.documentElement.querySelectorAll(FRAMES_CSS_SELECTOR);
|
|
|
- frameElements.forEach((frameElement, frameIndex) => {
|
|
|
- const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
|
|
|
- frameElement.setAttribute(docHelper.WIN_ID_ATTRIBUTE_NAME, windowId);
|
|
|
- });
|
|
|
- return doc;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
function getFrameData(document, window, windowId, options) {
|
|
|
const docData = docHelper.preProcessDoc(document, window, options);
|
|
|
const content = docHelper.serialize(document);
|