add javascript interface to android webView so messages can be sent from web to android
This commit is contained in:
3
Makefile
3
Makefile
@ -10,6 +10,7 @@ apple_extension_gen:
|
|||||||
droid:
|
droid:
|
||||||
studio android/Omnivore
|
studio android/Omnivore
|
||||||
|
|
||||||
apple_webview_gen:
|
webview_gen:
|
||||||
yarn workspace @omnivore/appreader build
|
yarn workspace @omnivore/appreader build
|
||||||
cp packages/appreader/build/bundle.js apple/OmnivoreKit/Sources/Views/Resources/bundle.js
|
cp packages/appreader/build/bundle.js apple/OmnivoreKit/Sources/Views/Resources/bundle.js
|
||||||
|
cp packages/appreader/build/bundle.js android/Omnivore/app/src/main/assets/bundle.js
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@ -5,6 +5,7 @@ import android.content.Context
|
|||||||
import android.graphics.Rect
|
import android.graphics.Rect
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.view.*
|
import android.view.*
|
||||||
|
import android.webkit.JavascriptInterface
|
||||||
import android.webkit.WebView
|
import android.webkit.WebView
|
||||||
import android.webkit.WebViewClient
|
import android.webkit.WebViewClient
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
@ -13,6 +14,8 @@ import androidx.compose.runtime.getValue
|
|||||||
import androidx.compose.runtime.livedata.observeAsState
|
import androidx.compose.runtime.livedata.observeAsState
|
||||||
import androidx.compose.ui.viewinterop.AndroidView
|
import androidx.compose.ui.viewinterop.AndroidView
|
||||||
import app.omnivore.omnivore.R
|
import app.omnivore.omnivore.R
|
||||||
|
import org.json.JSONObject
|
||||||
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun WebReaderLoadingContainer(slug: String, webReaderViewModel: WebReaderViewModel) {
|
fun WebReaderLoadingContainer(slug: String, webReaderViewModel: WebReaderViewModel) {
|
||||||
@ -63,6 +66,7 @@ fun WebReader(params: WebReaderParams) {
|
|||||||
webViewClient = object : WebViewClient() {
|
webViewClient = object : WebViewClient() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addJavascriptInterface(WebMessageHandler(), "WebMessageHandler")
|
||||||
loadDataWithBaseURL("file:///android_asset/", styledContent, "text/html; charset=utf-8", "utf-8", null);
|
loadDataWithBaseURL("file:///android_asset/", styledContent, "text/html; charset=utf-8", "utf-8", null);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -133,3 +137,14 @@ class OmnivoreWebView(context: Context) : WebView(context) {
|
|||||||
return super.startActionMode(currentActionModeCallback, type)
|
return super.startActionMode(currentActionModeCallback, type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class WebMessageHandler {
|
||||||
|
@JavascriptInterface
|
||||||
|
fun handleMessage(jsonString: String) {
|
||||||
|
val message = JSONObject(jsonString)
|
||||||
|
Log.d("Loggo", "Handling message: $message")
|
||||||
|
val actionID = message["actionID"]
|
||||||
|
Log.d("Loggo", "Received message with ID: $actionID and jsonValue: $message")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -104,8 +104,6 @@ data class WebReaderContent(
|
|||||||
</html>
|
</html>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
Log.d("Loggos", content)
|
|
||||||
|
|
||||||
return content
|
return content
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@ -7,13 +7,19 @@ import '@omnivore/web/styles/globals.css'
|
|||||||
import '@omnivore/web/styles/articleInnerStyling.css'
|
import '@omnivore/web/styles/articleInnerStyling.css'
|
||||||
|
|
||||||
const mutation = async (name, input) => {
|
const mutation = async (name, input) => {
|
||||||
const result =
|
const message = { actionID: name, ...input }
|
||||||
await window?.webkit?.messageHandlers.articleAction?.postMessage({
|
|
||||||
actionID: name,
|
if (window.webkit) {
|
||||||
...input,
|
// Send iOS a message
|
||||||
})
|
const result =
|
||||||
console.log('action result', result, result.result)
|
await window?.webkit?.messageHandlers.articleAction?.postMessage(message)
|
||||||
return result.result
|
console.log('action result', result, result.result)
|
||||||
|
return result.result
|
||||||
|
} else {
|
||||||
|
// Send android a message
|
||||||
|
console.log('sending android a message', message)
|
||||||
|
WebMessageHandler.handleMessage(JSON.stringify(message))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user