Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Problem injecting JavaScript into WebView with Cocoa

  • 01-09-2012 08: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
Advertisement