|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2023 DuckDuckGo |
| 2 | + * Copyright (c) 2025 DuckDuckGo |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package com.duckduckgo.contentscopescripts.impl
|
18 | 18 |
|
19 |
| -import android.annotation.SuppressLint |
20 |
| -import android.webkit.WebView |
21 |
| -import androidx.webkit.ScriptHandler |
22 |
| -import com.duckduckgo.app.browser.api.WebViewCapabilityChecker |
23 |
| -import com.duckduckgo.app.browser.api.WebViewCapabilityChecker.WebViewCapability.DocumentStartJavaScript |
24 |
| -import com.duckduckgo.browser.api.webviewcompat.WebViewCompatWrapper |
25 |
| -import com.duckduckgo.common.utils.DispatcherProvider |
26 | 19 | import com.duckduckgo.contentscopescripts.api.contentscopeExperiments.ContentScopeExperiments
|
27 | 20 | import com.duckduckgo.di.scopes.FragmentScope
|
28 | 21 | import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScriptPlugin
|
| 22 | +import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScriptScriptStrategy |
| 23 | +import com.duckduckgo.js.messaging.api.AddDocumentStartScriptDelegate |
29 | 24 | import com.squareup.anvil.annotations.ContributesMultibinding
|
30 | 25 | import dagger.SingleInstanceIn
|
31 |
| -import kotlinx.coroutines.withContext |
32 | 26 | import javax.inject.Inject
|
33 | 27 |
|
34 | 28 | @SingleInstanceIn(FragmentScope::class)
|
35 | 29 | @ContributesMultibinding(FragmentScope::class)
|
36 | 30 | class ContentScopeScriptsAddDocumentStartJavaScriptPlugin @Inject constructor(
|
37 |
| - private val webViewCompatContentScopeScripts: WebViewCompatContentScopeScripts, |
38 |
| - private val dispatcherProvider: DispatcherProvider, |
39 |
| - private val webViewCapabilityChecker: WebViewCapabilityChecker, |
40 |
| - private val webViewCompatWrapper: WebViewCompatWrapper, |
41 |
| - private val contentScopeExperiments: ContentScopeExperiments, |
42 |
| -) : AddDocumentStartJavaScriptPlugin { |
43 |
| - private var script: ScriptHandler? = null |
44 |
| - private var currentScriptString: String? = null |
45 |
| - |
46 |
| - @SuppressLint("RequiresFeature") |
47 |
| - override suspend fun addDocumentStartJavaScript(webView: WebView) { |
48 |
| - if (!webViewCompatContentScopeScripts.isEnabled() || |
49 |
| - !webViewCapabilityChecker.isSupported( |
50 |
| - DocumentStartJavaScript, |
51 |
| - ) |
52 |
| - ) { |
53 |
| - return |
| 31 | + webViewCompatContentScopeScripts: WebViewCompatContentScopeScripts, |
| 32 | + contentScopeExperiments: ContentScopeExperiments, |
| 33 | + scriptInjectorDelegate: AddDocumentStartScriptDelegate, |
| 34 | +) : AddDocumentStartJavaScriptPlugin by scriptInjectorDelegate.createPlugin( |
| 35 | + object : AddDocumentStartJavaScriptScriptStrategy { |
| 36 | + override suspend fun canInject(): Boolean { |
| 37 | + return webViewCompatContentScopeScripts.isEnabled() |
54 | 38 | }
|
55 | 39 |
|
56 |
| - val activeExperiments = contentScopeExperiments.getActiveExperiments() |
57 |
| - val scriptString = webViewCompatContentScopeScripts.getScript(activeExperiments) |
58 |
| - if (scriptString == currentScriptString) { |
59 |
| - return |
60 |
| - } |
61 |
| - script?.let { |
62 |
| - withContext(dispatcherProvider.main()) { |
63 |
| - it.remove() |
64 |
| - } |
65 |
| - script = null |
| 40 | + override suspend fun getScriptString(): String { |
| 41 | + val activeExperiments = contentScopeExperiments.getActiveExperiments() |
| 42 | + return webViewCompatContentScopeScripts.getScript(activeExperiments) |
66 | 43 | }
|
67 | 44 |
|
68 |
| - webViewCompatWrapper |
69 |
| - .addDocumentStartJavaScript( |
70 |
| - webView, |
71 |
| - scriptString, |
72 |
| - setOf("*"), |
73 |
| - )?.let { |
74 |
| - script = it |
75 |
| - currentScriptString = scriptString |
76 |
| - } |
77 |
| - } |
| 45 | + override val allowedOriginRules: Set<String> = setOf("*") |
78 | 46 |
|
79 |
| - override val context: String |
80 |
| - get() = "contentScopeScripts" |
81 |
| -} |
| 47 | + override val context: String |
| 48 | + get() = "contentScopeScripts" |
| 49 | + }, |
| 50 | +) |
0 commit comments