Skip to content

Commit 80725e2

Browse files
add iframe example
1 parent 8c7ba6d commit 80725e2

File tree

7 files changed

+127
-0
lines changed

7 files changed

+127
-0
lines changed

iframe.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Frame Demo</title>
7+
<script src="/scripts/actionLogger.js" defer></script>
8+
</head>
9+
<body>
10+
<h1>Frame Demo</h1>
11+
<p>Check the console to see where the clicks happen.</p>
12+
<pre>not in frame</pre>
13+
<iframe id=A src="iframeA.html" style="width: 100%; height: 12em;"></iframe>
14+
<script>
15+
setTimeout(() => {
16+
const a = document.getElementById("A");
17+
a.insertAdjacentHTML("afterend", `<iframe id=B src="iframeB.html" style="width: 100%;"></iframe>`)
18+
}, 1000);
19+
</script>
20+
<iframe id=C src="iframeC.html" style="width: 100%;"></iframe>
21+
</body>
22+
</html>

iframeA.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>IFrame A</title>
7+
<script src="/scripts/actionLogger.js" defer></script>
8+
</head>
9+
<body>
10+
<h2>IFrame A</h2>
11+
<pre>index 0, position 0</pre>
12+
<iframe id=A1 src="iframeA1.html" style="width: 40%"></iframe>
13+
<iframe id=A2 src="iframeA2.html" style="width: 40%"></iframe>
14+
</body>
15+
</html>

iframeA1.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>IFrame A1</title>
7+
<script src="/scripts/actionLogger.js" defer></script>
8+
</head>
9+
<body>
10+
<h3>IFrame A1</h3>
11+
<pre>index 0, position 0</pre>
12+
</body>
13+
</html>

iframeA2.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>IFrame A2</title>
7+
<script src="/scripts/actionLogger.js" defer></script>
8+
</head>
9+
<body>
10+
<h3>IFrame A2</h3>
11+
<pre>index 1, position 1</pre>
12+
</body>
13+
</html>

iframeB.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>IFrame B</title>
7+
<script src="/scripts/actionLogger.js" defer></script>
8+
</head>
9+
<body>
10+
<h2>IFrame B</h2>
11+
<p><em>added asynchronously</em></p>
12+
<pre>index 2, position 1</pre>
13+
</body>
14+
</html>

iframeC.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>IFrame C</title>
7+
<script src="/scripts/actionLogger.js" defer></script>
8+
</head>
9+
<body>
10+
<h2>IFrame C</h2>
11+
<pre>index 1, position 2</pre>
12+
</body>
13+
</html>

scripts/actionLogger.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// @ts-check
2+
3+
/**
4+
* @param {Window} context
5+
* @returns {Window[]}
6+
*/
7+
function getFramePath(context) {
8+
if(!context.frameElement) return [];
9+
return [...getFramePath(context.parent), context];
10+
}
11+
12+
/** @param {Window} context */
13+
function getFrameIndex(context) {
14+
return Array.from(context.parent.frames).indexOf(context);
15+
}
16+
/** @param {HTMLIFrameElement} iframe */
17+
function getFrameDomPosition(iframe) {
18+
return [...iframe.ownerDocument.querySelectorAll("iframe")].indexOf(iframe);
19+
}
20+
21+
/**
22+
* @param {MouseEvent} ev
23+
*/
24+
const logAction = (ev) => {
25+
const framePath = getFramePath(window);
26+
const frameElementsPath = framePath.map(win => /** @type {HTMLIFrameElement} */ (win.frameElement)).filter(el => !!el);
27+
if(!frameElementsPath.every(el => (el.tagName === "IFRAME"))) console.error("Not all elements are iframes", frameElementsPath);
28+
29+
console.log(ev.type, ev.target);
30+
console.log("framePath", framePath);
31+
console.log("frameElementsPath", frameElementsPath);
32+
console.log("frameIndex", framePath.map(getFrameIndex));
33+
console.log("frameDomPosition", frameElementsPath.map(getFrameDomPosition));
34+
console.log("")
35+
}
36+
37+
window.addEventListener("click", logAction);

0 commit comments

Comments
 (0)