@0x77 I do not know if it helped this time but I often find that creating that minimal script removes all the cruft and allows you to see where the problem(s) lie. Sherlock Holmes once said something similar.
@ccc Thanks for your response. It worked well, except that I couldn't get it to work without displaying the WebView, which I was hoping to avoid. I ended up using the method suggested by @JonB to get around this.