Skip to content

fix: Download file requested when connecting to any Dapp #58

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 2 commits into from
Jun 24, 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 @@ -123,7 +123,7 @@ class RNCWebViewManagerImpl(private val newArch: Boolean = false) {
return@DownloadListener
}
if (url.startsWith("blob:")) {
// Handled in RNCWebView.injectBlobFileDownloaderScript()
webView.evaluateJavascriptWithFallback(BlobFileDownloader.getDownloadBlobInterceptor(url))
return@DownloadListener
}
webView.setIgnoreErrFailedForThisURL(url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,55 @@ internal class BlobFileDownloader(
companion object {
const val JS_INTERFACE_TAG: String = "BlobFileDownloader"

fun getBlobFileInterceptor(): String =
"""
(function() {
const originalCreateObjectURL = URL.createObjectURL;
URL.createObjectURL = function(blob) {
const url = originalCreateObjectURL.call(URL, blob);
const reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
const base64 = reader.result;
${JS_INTERFACE_TAG}.getBase64FromBlobData(base64);
};
return url;
};
/**
* Invokes JS method downloadBlob from [getBlobFileInterceptor]
*/
fun getDownloadBlobInterceptor(url: String): String = "downloadBlob('$url');"

/**
* This script handles Blob downloading in two ways:
* 1) It intercepts clicks on elements with a Blob: href.
* 2) It defines a method, downloadBlob, which is manually invoked from Android [com.reactnativecommunity.webview.RNCWebViewManagerImpl] in cases where the href is undefined.
*/
fun getBlobFileInterceptor(): String = """
(function() {
if (window.blobDownloadInjected) return;
window.blobDownloadInjected = true;

function blobToBase64(blob, callback) {
const reader = new FileReader();
reader.onloadend = function() {
const base64 = reader.result;
callback(base64);
};
reader.readAsDataURL(blob);
}

function downloadBlobUrl(url) {
const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'blob';
xhr.onload = function() {
if (this.status === 200) {
const blob = this.response;
blobToBase64(blob, function(base64) {
${JS_INTERFACE_TAG}.getBase64FromBlobData(base64);
});
}
};
xhr.send();
}

document.addEventListener('click', function(e) {
const target = e.target;
if (target.tagName === 'A' && target.href && target.href.startsWith('blob:')) {
e.preventDefault();
downloadBlobUrl(target.href);
}
}, true);

window.downloadBlob = downloadBlobUrl;
})();
""".trimIndent()
""".trimIndent()
}
}
Loading