一些网站不接受将Java脚本注入到webview中

时间:2019-06-17 13:54:50

标签: javascript swift webkit

我正在将Java脚本注入到webviewkit中,以使用户在单击长按时从网站检索图像,所以我的问题是某些网站对javascript注入没有响应,但是大多数网站都响应完美。我希望你们中的一个人也遇到了同样的问题。任何帮助表示赞赏。谢谢 ! 这是我的代码:

 private func initializeWebView() {
    webView.navigationDelegate = self

    let newJavascript = "window.onload = function() {
        if (document.addEventListener){
        document.addEventListener("click", function(evt) {
            var tagClicked = document.elementFromPoint(evt.pageX - window.pageXOffset, evt.pageY - window.pageYOffset);
            window.webkit.messageHandlers.jsMessenger.postMessage(tagClicked.outerHTML.toString());
        });
        }
    else if (document.attachEvent){

            document.attachEvent("onclick", function(evt) {
            var tagClicked = document.elementFromPoint(evt.pageX - window.pageXOffset, evt.pageY - window.pageYOffset);
            window.webkit.messageHandlers.jsMessenger.postMessage(tagClicked.innerHTML.toString());
        });

        }
        document.body.style.webkitTouchCallout='none';
        document.body.style.webkitUserSelect='none';
        document.body.style.pointerEvents='auto';

    };"

    window.onload = function() {
        if (document.addEventListener){
        document.addEventListener("click", function(evt) {
            var tagClicked = document.elementFromPoint(evt.pageX - window.pageXOffset, evt.pageY - window.pageYOffset);
            window.webkit.messageHandlers.jsMessenger.postMessage(tagClicked.outerHTML.toString());
        });
        }
    else if (document.attachEvent){

            document.attachEvent("onclick", function(evt) {
            var tagClicked = document.elementFromPoint(evt.pageX - window.pageXOffset, evt.pageY - window.pageYOffset);
            window.webkit.messageHandlers.jsMessenger.postMessage(tagClicked.innerHTML.toString());
        });

        }
        document.body.style.webkitTouchCallout='none';
        document.body.style.webkitUserSelect='none';
        document.body.style.pointerEvents='auto';

    };


    let userScript = WKUserScript.init(source: newJavascript,
                                       injectionTime: .atDocumentEnd , forMainFrameOnly: false)
    webView.configuration.userContentController.addUserScript(userScript)
    webView.configuration.userContentController.add(self, name: "jsMessenger")
    longGesture = UILongPressGestureRecognizer(target: self, action: #selector(WebViewController.longPress(_:)))
    longGesture.minimumPressDuration = 1
    longGesture.delegate = self
   // let clickGesture=UipresG
    self.webView.scrollView.addGestureRecognizer(longGesture)
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(viewTap) )
    tapGesture.delegate = self
    self.webView.scrollView.addGestureRecognizer(tapGesture)
    tapGesture.delegate = self
    webView.addGestureRecognizer(tapGesture)

}
 private func loadData() {
    let request  = URLRequest(url: url)
    webView.load(URLRequest.init(url: url))
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
    print(message.body)
    let html: String = message.body as! String
    do{
    let doc: Document = try SwiftSoup.parse(html)
        if(html.contains("img")){
    var link: Element = try doc.select("img").first()!

    contentInfo=message.body as! String
            var linkHref: String = try link.attr("src")
            if linkHref.contains("https://www.google.com") {
                linkHref=String(linkHref.dropFirst(22))
                linkHref="http:"+linkHref
            } else {
                linkHref =  String(format: "%@%@", (self.url.absoluteString), linkHref)
            }
            if (linkHref.contains("https://www.google.com")) {
                linkHref=String(linkHref.dropFirst(22))
                if(!linkHref.contains("http")){
                linkHref="http:"+linkHref
                }
            }

    self.itemImgUrl=linkHref
        }
    }catch{
    }
}

1 个答案:

答案 0 :(得分:0)

我发现了问题,原因是在我的脚本“ window.onload = function(){”中有时未执行,因此当我删除所有网站都接受该脚本时,脚本不会继续执行。 希望我的回答对别人有帮助。干杯!