Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Problem injecting JavaScript into WebView with Cocoa

Options
  • 01-09-2012 9:14pm
    #1
    Registered Users 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