userContentController никогда не вызывается после добавления js для клика iframe, ios, swift 5 - PullRequest
0 голосов
/ 07 апреля 2020

Мне нужно отследить щелчок iframe на веб-странице, где я загружаю HTML в WKWebview, и я написал ниже код, а методы слушателя userContentController никогда не вызываются.

    cell.webView.navigationDelegate = self
    cell.webView.uiDelegate = self

    let js = "<script> \n window.addEventListener('blur', function (e) {" +
        "            if (document.activeElement.nodeName == \"IFRAME\" && document.activeElement.src != '') {\n" +
        "                var type = '';\n" +
        "                if (document.activeElement.src.indexOf('spotify') != -1) type = 'spotify';\n" +
        "                if (document.activeElement.src.indexOf('itunes') != -1) type = 'itunes';\n" +
        "                if (document.activeElement.src.indexOf('youtube') != -1) type = 'youtube';\n" +
        "                if (document.activeElement.src.indexOf('tunein') != -1) type = 'tunein';\n" +
        "                if (document.activeElement.src.indexOf('acast') != -1) type = 'acast';\n" +
        "\n" +
        " window.webkit.messageHandlers.nativeProcess.postMessage(\"result\":\"test\");}" +
        "        });\n</script>"

    print(js)


    let userScript:WKUserScript =  WKUserScript(source: js, injectionTime: WKUserScriptInjectionTime.atDocumentStart, forMainFrameOnly: false)

    cell.webView.configuration.userContentController.removeAllUserScripts()
    cell.webView.configuration.userContentController.removeScriptMessageHandler(forName: "nativeProcess")

    cell.webView.configuration.userContentController.addUserScript(userScript)

    cell.webView.configuration.userContentController.add(self, name: "nativeProcess")
    cell.webView.loadHTMLString(str as String, baseURL: URL)
...