0

I am injecting java script to webviewkit to let the user retrieve images from websites on tapping a long click so my issue is some websites are not responding to the javascript injection but the majority of the web sites are responding perfectly. I hope that one of you had passed with the same problem. any Help is appreciated. thank you ! this is my code :

 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{
    }
}
mark
  • 327
  • 1
  • 2
  • 12
  • Best answer is at, https://stackoverflow.com/questions/9515704/insert-code-into-the-page-context-using-a-content-script/9517879#9517879 – Manish Khedekar Jun 17 '19 at 13:59
  • Overlays or backgrounds? – Lain Jun 17 '19 at 13:59
  • on background, I am using the script to retrieve the selected dom element and passed it to a WKScriptMessage so if it's an image I can display it in an imageview – mark Jun 17 '19 at 14:09

1 Answers1

0

I found the issue and it was that in my script "window.onload = function() {" sometimes it was not executed so script doest not continue executing when I remove it all web sites accepted the script. Hope that my answer will help someone. Cheers !

mark
  • 327
  • 1
  • 2
  • 12