Inject script in WebView after page render

I’m trying to execute JS in the WebView only after the page has rendered. Since it uses react, the default waitForLoad(); method doesn’t really work. However, in my specific use case, there is an attribute that is added/ changed once React has rendered the page ‘data-gr-c-s-loaded=“true”’. So I’m trying to delay the injection of my JS until that attribute could be found in the HTML. How can I approach this?

You could do it like this:

let wv = new WenView()
// do other setup
await wv.waitForLoad()
let script = `
let interv = setInterval(() => {
    // get the attribute
    if (attribute) {
        // either run your code here, or inject it down below
}, 25) // you can adjust this value to your liking`
await wv.evaluateJavaScript(script, true)
// inject your other script
1 Like

I tried your amazing approach, but I’m having issues returning values scraped from the WebView. The WebView process never ends even after calling the completion() method, and I don’t think my return statement is being respected.

I assume you have debugged it with various calls to log (even inside the injected script)?
And I also assume you are trying to return the values by passing them to the completion() function?

It is hard to say what goes wrong without any code… maybe the completion() call never gets executed? That’s just a wild guess, I don’t know.

How do you pass in parameters to the completion method? If I’m not mistaken I thought it doesn’t receive any parameters.

As the docs state if you pass

  • false for the async parameter, then the value of the last line of the script that is not whitespace will be returned to the Scriptable script
  • true for the async parameter, then the script waits for the call to completion() and anything passed to it will be returned to the Scriptable script
1 Like