I need to dynamically change about 10 letters of HTML in a webview that is hosted in the project. This seems like it should be simple. I literally just need to change about 10 characters in a .html file sitting in my project.
What seems to be making this so difficult is that I need to do it before the page starts to load in the wkwebview. The characters in question sit inside a <script>
tag used by a widget. Apparently the widget is not happy with any of characters in the script getting changed after it starts to load. This is my conclusion after trying change the characters using JavaScript after loading the wkwebview.
How can I change the 10 characters prior to the webview starting to load?
I'm thinking, perhaps I could change the html file before exposing it to the Internet... but I don't know how to do that. Or perhaps I could reload the div containing the widget, after changing the HTML..but I think it would get set back to default.
This is a basic page.html file:
<html>
<body>
<head>
<script>
setName(param) {
var myelement = document.getElementById("name");
myelement.innerHTML= param;
}
</script>
</head>
<body>
<div class="widget-container">
<div id="widget-dest"></div>
<script type="text/javascript" src="https://s3._/widget.js"></script>
<script type="text/javascript">
new widget(
{
"symbol": "omar sharif",//THIS IS THE PARAMETER I NEED TO CHANGE..<span id=name></span> would go here but JS NOT WORKING
"timezone": "Etc/UTC",
"locale": "en"
}
);
</script>
</div>
</body>
</html>
I am opening this html file with
var webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
webView.uiDelegate = self
if let url = Bundle.main.url(forResource: "page", withExtension: "html") {
webView.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
}
}
override func loadView() {
super.loadView()
self.view = webView
}
//Unsuccessful effort to set name value with JS in delegate after loading. It can set a value elsewhere on page, but widget won't load when I try to set parameter this way
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("setName("John Wayne")", completionHandler: nil)
}
I don't know if it exactly help you. But you can check here: https://www.appcoda.com/webkit-framework-tutorial/