添加前,网页代码如下:

<!DOCTYPE html>
<html>
<!--<script type="text/javascript">-->
<!--    function echo(str) {-->
<!--        console.log(str);-->
<!--        window.webkit.messageHandlers.testecho.postMessage("str");-->
<!--}-->
<!--</script>-->
<body>
<input type="button" onClick="echo('toast clicked')" value="toast">
<input type="button" onClick="echo('alert clicked')" value="Alert">
</body>

</html>

添加代码

    let jsStr = """
    function echo(str) {console.log(str);window.webkit.messageHandlers.testecho.postMessage("str");}
    """
    let userScript = WKUserScript.init(source: jsStr, injectionTime: .atDocumentStart, forMainFrameOnly: true)
    usecc.addUserScript(userScript)

添加后,网页代码如下

向 webview 添加 userScript

向 webview 添加 userScript

html 没有发生变化,但是可以看到有一个 user-script。 控制台中,也有输出。native 也可以收到回调。 向 webview 添加 userScript

参考

相关文章:

  • 2021-06-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-26
  • 2021-09-18
  • 2021-05-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-11
相关资源
相似解决方案