Skip to content

Commit b7f188f

Browse files
committed
Simplify JS setup
1 parent 5c9f2a1 commit b7f188f

File tree

3 files changed

+7
-42
lines changed

3 files changed

+7
-42
lines changed

app/src/main/java/com/duckduckgo/app/browser/BrowserTabFragment.kt

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3453,23 +3453,6 @@ class BrowserTabFragment :
34533453
val script =
34543454
"""
34553455
(function() {
3456-
// Capture the injected object immediately
3457-
const ddgBlob = window.ddgBlobDownloadObj;
3458-
3459-
if (!ddgBlob) {
3460-
console.error('ddgBlobDownloadObj not found');
3461-
return;
3462-
}
3463-
3464-
try {
3465-
Object.defineProperty(window, 'ddgBlobDownloadObj', {
3466-
value: ddgBlob,
3467-
writable: false,
3468-
configurable: false,
3469-
});
3470-
} catch (e) {
3471-
console.warn('Could not protect ddgBlobDownloadObj:', e);
3472-
}
34733456
34743457
const urlToBlobCollection = {};
34753458
@@ -3498,17 +3481,17 @@ class BrowserTabFragment :
34983481
}
34993482
35003483
const pingMessage = 'Ping:' + window.location.href;
3501-
ddgBlob.postMessage(pingMessage);
3484+
window.ddgBlobDownloadObj.postMessage(pingMessage);
35023485
console.log('Sent ping message for blob downloads: ' + pingMessage);
35033486
3504-
ddgBlob.addEventListener('message', function(event) {
3487+
window.ddgBlobDownloadObj.addEventListener('message', function(event) {
35053488
if (event.data.startsWith('blob:')) {
35063489
console.log(event.data);
35073490
const blob = urlToBlobCollection[event.data];
35083491
if (blob) {
35093492
blobToBase64DataUrl(blob).then((dataUrl) => {
35103493
console.log('Sending data URL back to native ' + dataUrl);
3511-
ddgBlob.postMessage(dataUrl);
3494+
window.ddgBlobDownloadObj.postMessage(dataUrl);
35123495
});
35133496
} else {
35143497
console.log('No Blob found for URL: ' + event.data);

app/src/main/java/com/duckduckgo/app/browser/WebViewCompatTestHelper.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class RealWebViewCompatTestHelper @Inject constructor(
6565
private val webViewCompatFeature: WebViewCompatFeature,
6666
private val webViewCompatWrapper: WebViewCompatWrapper,
6767
moshi: Moshi,
68-
@AppCoroutineScope private val appCoroutineScope: CoroutineScope
68+
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
6969
) : WebViewCompatTestHelper {
7070

7171
private var proxy: JavaScriptReplyProxy? = null
@@ -83,7 +83,7 @@ class RealWebViewCompatTestHelper @Inject constructor(
8383
private val webViewCompatConfigDeferred by lazy {
8484
appCoroutineScope.async(dispatchers.io()) {
8585
if (!webViewCompatFeature.self().isEnabled()) return@async null
86-
WebViewCompatConfig(
86+
WebViewCompatConfig(
8787
settings = webViewCompatFeature.self().getSettings()?.let {
8888
adapter.fromJson(it)
8989
},
@@ -126,13 +126,11 @@ class RealWebViewCompatTestHelper @Inject constructor(
126126
webView.context.resources?.openRawResource(R.raw.webviewcompat_test_script)
127127
?.bufferedReader().use { it?.readText() }.orEmpty()
128128
.replace(delay, config.settings?.jsInitialPingDelay?.toString() ?: "0")
129-
.replace(com.duckduckgo.app.browser.postInitialPing, config.jsSendsInitialPing.toString())
130-
.replace(com.duckduckgo.app.browser.replyToNativeMessages, config.jsRepliesToNativeMessages.toString())
129+
.replace(postInitialPing, config.jsSendsInitialPing.toString())
130+
.replace(replyToNativeMessages, config.jsRepliesToNativeMessages.toString())
131131
.replace(objectName, if (useDedicatedListener) "webViewCompatTestObj" else "ddgBlobDownloadObj")
132132
}
133133

134-
// logcat("Cris") {"Script: $script"}
135-
136134
webViewCompatWrapper.addDocumentStartJavaScript(webView, script, setOf("*"))
137135

138136
if (useDedicatedListener) {

app/src/main/res/raw/webviewcompat_test_script.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,6 @@
22
// Capture the injected object immediately
33
const ddgObj = window.$OBJECT_NAME$;
44

5-
if (!ddgObj) {
6-
console.error('$OBJECT_NAME$ not found');
7-
return;
8-
}
9-
10-
// Make it read-only to prevent overwrites (don't delete - other scripts may need it)
11-
try {
12-
Object.defineProperty(window, '$OBJECT_NAME$', {
13-
value: ddgObj,
14-
writable: false,
15-
configurable: false,
16-
});
17-
} catch (e) {
18-
console.warn('Could not protect $OBJECT_NAME$:', e);
19-
}
20-
215
const supportedMessages = ["ContextMenuOpened", "PageStarted"];
226

237
const delay = $DELAY$;

0 commit comments

Comments
 (0)