使用可可将JavaScript注入网站时出现问题

Trouble injecting JavaScript into website with cocoa

本文关键字:问题 网站 注入 可可 JavaScript      更新时间:2023-09-26

我正试图将一些javascript注入到我不拥有的网站中。我试图注入的代码如下:

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
        });
    });
}

这不起作用。我使用的方法stringByEvaluatingJavaScriptFromString似乎不适用于嵌套括号或其他什么方法。它适用于简单的代码。

问题是根本没有注入代码

我的完整代码如下:

[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);"]];

如有任何帮助,我们将不胜感激。感谢

没有太多麻烦,我可以看到一些小东西。我重写了,也许你可以试试。

[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) $(this).parent().find('form').each(function(i, obj){ obj.submit(); }); }); } document.getElementsByTagName('head')[0].appendChild(script);"]];

我希望它能这样工作。

基本上,我去掉了一些双引号,用单引号替换了一些,这样就不必转义了。我把所有的剧本都写在一行。

如果表单是类,则AND find('form')应为find('.form');如果表单是id,则AND find('#form')。