forked from react-native-webview/react-native-webview
-
Notifications
You must be signed in to change notification settings - Fork 6
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
android/src/main/java/com/reactnativecommunity/webview/extension/file/IFrameDetector.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('//'))) { | ||
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() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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('//')
?There was a problem hiding this comment.
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)