Skip to content

Commit

Permalink
open iframe links in new window
Browse files Browse the repository at this point in the history
  • Loading branch information
pokornyd committed Nov 11, 2024
1 parent 363e8c4 commit 131194a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/components/sync/SyncDiff.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export const SyncDiff: React.FC = () => {
id="diffFrame"
srcDoc={diffResult}
title="Diff Result"
sandbox="allow-scripts"
sandbox="allow-scripts allow-popups allow-popups-to-escape-sandbox"
ref={iframeRef}
/>
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/components/sync/SyncEntities.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useContext, useState } from "react";
import { useNavigate } from "react-router-dom";

import { DiffResponse, injectResizeScript, removeElementFromHtml } from "../../utils/sync";
import { DiffResponse, injectScripts, removeElementFromHtml } from "../../utils/sync";
import { SyncEntityName, WizardContext } from "../../WizardContext";
import { Loader } from "../Loader";
import { StepNavigation } from "../menu/StepNavigation";
Expand Down Expand Up @@ -72,7 +72,7 @@ export const SyncEntities: React.FC = () => {
}

const data: DiffResponse = await response.json();
const modifiedDiffHtml = injectResizeScript(
const modifiedDiffHtml = injectScripts(
removeElementFromHtml(data.html, ".title"),
);

Expand Down
9 changes: 7 additions & 2 deletions src/utils/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ export const removeElementFromHtml = (
};

/**
* appends a script to an HTML, sending height information to parent window via message layer for dynamic resizing
* Adds <base> element to open iframe links in new window by default.
* Appends <script>, sending height information to parent window via message layer for dynamic resizing.
*/
export const injectResizeScript = (htmlString: string): string => {
export const injectScripts = (htmlString: string): string => {
const parser = new DOMParser();
const doc = parser.parseFromString(htmlString, "text/html");

const base = doc.createElement("base");
base.target = "_blank";

const script = doc.createElement("script");
script.type = "text/javascript";
// adding 5 to the final height fixed an issue wherein scrollbars sometimes appeared following recalculation
Expand Down Expand Up @@ -46,5 +50,6 @@ export const injectResizeScript = (htmlString: string): string => {
})();
`;
doc.body.appendChild(script);
doc.head.appendChild(base);
return doc.documentElement.outerHTML;
};

0 comments on commit 131194a

Please sign in to comment.