kishan
kishan

Reputation: 57

javascript injection not being executed

I have included my function and the way im calling it it does print "injecting" in console but the javascript dosent seem to be injected i have tested the js in safari console and chrome console it works but dosent work in the wkwebview

@IBAction func Register(_ sender: RoundButton) {
        let RegistrationURL = URL(string: "https://www.baitme.com")
        let newRegister = URLRequest(url: RegistrationURL!)
        registrationwebview.load(newRegister)

        DispatchQueue.main.asyncAfter(deadline: .now() + 2 ) {
            print("inside js")
            self.javascript()
        }
    }

func javascript(){
        let config = WKWebViewConfiguration()
        let userContentController = WKUserContentController()

        config.userContentController = userContentController


        guard let scriptPath = Bundle.main.path(forResource: "checkout", ofType: "js"),
            let scriptSource = try? String(contentsOfFile: scriptPath) else { return }

        let userScript = WKUserScript(source: scriptSource, injectionTime: .atDocumentStart, forMainFrameOnly: true)
        print("injecting")
        userContentController.addUserScript(userScript)
    }

Upvotes: 2

Views: 81

Answers (0)

Related Questions