I'm looking for different methods of injecting JavaScript into a WebView (in Cocoa).
I am trying to inject some javascript into the <head>
tag of a HTML file that has been loaded into the WebView.
The following method doesn't work for me. It seems to only work with very simple JavaScript with no nested brackets (from what I have tested):
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"some simple JavaScript"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);"]];
Are there any other ways of doing this with more complicated JavaScript / jQuery functions?
The full code I am trying to inject is as follows:
function submitFormForTime(time){
$(\".t_h\").each(function(i, obj) {
if ($(this).text() != time) return;
$(this).parent().find(\"form\").each(function(i, obj){
obj.submit();
});
});
}