Skip to content

feat: Inject Javascript to detect all iFrames inside the current page and report them to the React app #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.reactnativecommunity.webview.events.TopCustomMenuSelectionEvent;
import com.reactnativecommunity.webview.events.TopMessageEvent;
import com.reactnativecommunity.webview.extension.file.BlobFileDownloader;
import com.reactnativecommunity.webview.extension.file.IFrameDetectorKt;

import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -343,6 +344,10 @@ public void injectBlobFileDownloaderScript() {
evaluateJavascriptWithFallback(BlobFileDownloader.Companion.getBlobFileInterceptor());
}

public void injectIFrameDetectorScript() {
evaluateJavascriptWithFallback(IFrameDetectorKt.getIFrameDetectorScript());
}

public void callInjectedJavaScriptBeforeContentLoaded() {
if (getSettings().getJavaScriptEnabled() &&
injectedJSBeforeContentLoaded != null &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public void onPageFinished(WebView webView, String url) {

reactWebView.injectBlobFileDownloaderScript();

reactWebView.injectIFrameDetectorScript();

emitFinishEvent(webView, url);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.reactnativecommunity.webview.extension.file

/**
* JavaScript code to detect all iFrames in the page and report their URLs
* This script runs after page load and also monitors for dynamically added iFrames
*/
fun getIFrameDetectorScript(): String = """
(function() {
if (window.iframeDetectorInjected) return;
window.iframeDetectorInjected = true;

function collectIFrameUrls() {
const iframes = document.getElementsByTagName('iframe');
const urls = [];

for (let i = 0; i < iframes.length; i++) {
const iframe = iframes[i];
const src = iframe.src;

if (src && src.trim() !== '' && (src.startsWith('http://') || src.startsWith('https://') || src.startsWith('//'))) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reasoning for including src.startsWith('//') ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you specify iFrame with //, it will use current website's protocol (http or https)

const normalizedUrl = src.startsWith('//') ? 'https:' + src : src;
urls.push(normalizedUrl);
}
}

return urls;
}

function reportIFrames() {
try {
const urls = collectIFrameUrls();
if (urls.length > 0) {
const message = {
type: 'IFRAME_DETECTED',
iframeUrls: urls
};

if (window.ReactNativeWebView && window.ReactNativeWebView.postMessage) {
window.ReactNativeWebView.postMessage(JSON.stringify(message));
}
}
} catch (e) {
console.error('Error reporting iFrames:', e);
}
}

// Initial check for iFrames
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', reportIFrames);
} else {
// Document already loaded
setTimeout(reportIFrames, 100);
}

// Monitor for dynamically added iFrames
const observer = new MutationObserver(function(mutations) {
let shouldCheck = false;
mutations.forEach(function(mutation) {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === Node.ELEMENT_NODE) {
if (node.tagName === 'IFRAME' || node.querySelector('iframe')) {
shouldCheck = true;
}
}
});
}
});

if (shouldCheck) {
setTimeout(reportIFrames, 100);
}
});

observer.observe(document.body || document.documentElement, {
childList: true,
subtree: true
});

// Also check periodically as a fallback
setInterval(reportIFrames, 5000);
})();
""".trimIndent()
Loading