Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Problem injecting JavaScript into WebView with Cocoa

  • 01-09-2012 09:14PM
    #1
    Registered Users, Registered Users 2 Posts: 331 ✭✭


    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


Advertisement