I am trying to inject some javascript into a site I do not own. The code I am trying to inject is as follows:
function submitFormForTime(time){
$(".t_h").each(function(i, obj){ // find each element with the class t_h
if ($(this).text() != time) return; // not the right one
$(this).parent().find("form").each(function(i, obj){
obj.submit(); // submit that form
});
});
}
This does not work. It seems that the method I am using, stringByEvaluatingJavaScriptFromString, does not work with nested brackets or whatever. It works fine with simple code.
The problem is that the code is not being injected at all
My full code is as follows:
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function submitFormForTime(time){ "
"$(\".t_h\").each(function(i, obj){"
"if ($(this).text() != time) return;"
"$(this).parent().find(\"form\").each(function(i, obj){"
"obj.submit();"
"});"
"});"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);"]];
Any help would be greatly appreciated. Thanks
Well without alot of trouble i can see a couple a small things. I rewrote it, maybe you can give it a try.
I hope it works like that.
Basically i removed some double quotes, and replaced some with single quotes to not have to escape them. And i wrote all the script text on one line.
AND find(‘form’) should be find(‘.form’) if form is a class or find(‘#form’) if form is an id.