1
+ package com.reactnativecommunity.webview.extension.file
2
+
3
+ /* *
4
+ * JavaScript code to detect all iFrames in the page and report their URLs
5
+ * This script runs after page load and also monitors for dynamically added iFrames
6
+ */
7
+ fun getIFrameDetectorScript (): String = """
8
+ (function() {
9
+ if (window.iframeDetectorInjected) return;
10
+ window.iframeDetectorInjected = true;
11
+
12
+ function collectIFrameUrls() {
13
+ const iframes = document.getElementsByTagName('iframe');
14
+ const urls = [];
15
+
16
+ for (let i = 0; i < iframes.length; i++) {
17
+ const iframe = iframes[i];
18
+ const src = iframe.src;
19
+
20
+ if (src && src.trim() !== '' && (src.startsWith('http://') || src.startsWith('https://') || src.startsWith('//'))) {
21
+ const normalizedUrl = src.startsWith('//') ? 'https:' + src : src;
22
+ urls.push(normalizedUrl);
23
+ }
24
+ }
25
+
26
+ return urls;
27
+ }
28
+
29
+ function reportIFrames() {
30
+ try {
31
+ const urls = collectIFrameUrls();
32
+ if (urls.length > 0) {
33
+ const message = {
34
+ type: 'IFRAME_DETECTED',
35
+ iframeUrls: urls
36
+ };
37
+
38
+ if (window.ReactNativeWebView && window.ReactNativeWebView.postMessage) {
39
+ window.ReactNativeWebView.postMessage(JSON.stringify(message));
40
+ }
41
+ }
42
+ } catch (e) {
43
+ console.error('Error reporting iFrames:', e);
44
+ }
45
+ }
46
+
47
+ // Initial check for iFrames
48
+ if (document.readyState === 'loading') {
49
+ document.addEventListener('DOMContentLoaded', reportIFrames);
50
+ } else {
51
+ // Document already loaded
52
+ setTimeout(reportIFrames, 100);
53
+ }
54
+
55
+ // Monitor for dynamically added iFrames
56
+ const observer = new MutationObserver(function(mutations) {
57
+ let shouldCheck = false;
58
+ mutations.forEach(function(mutation) {
59
+ if (mutation.type === 'childList') {
60
+ mutation.addedNodes.forEach(function(node) {
61
+ if (node.nodeType === Node.ELEMENT_NODE) {
62
+ if (node.tagName === 'IFRAME' || node.querySelector('iframe')) {
63
+ shouldCheck = true;
64
+ }
65
+ }
66
+ });
67
+ }
68
+ });
69
+
70
+ if (shouldCheck) {
71
+ setTimeout(reportIFrames, 100);
72
+ }
73
+ });
74
+
75
+ observer.observe(document.body || document.documentElement, {
76
+ childList: true,
77
+ subtree: true
78
+ });
79
+
80
+ // Also check periodically as a fallback
81
+ setInterval(reportIFrames, 5000);
82
+ })();
83
+ """ .trimIndent()
0 commit comments