使用Objective-C进行JavaScript注入的方法

Con*_*lor 12 javascript iphone objective-c code-injection

我正在寻找将JavaScript注入WebView(在Cocoa中)的不同方法.

我试图将一些javascript注入<head>已加载到WebView的HTML文件的标记中.

以下方法对我不起作用.它似乎只适用于非常简单的JavaScript,没有嵌套括号(来自我测试的):

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var script = document.createElement('script');"
                                                     "script.type = 'text/javascript';"
                                                     "script.text = \"some simple JavaScript"
                                                     "}\";"
                                                     "document.getElementsByTagName('head')[0].appendChild(script);"]];
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以使用更复杂的JavaScript/jQuery函数?

我试图注入的完整代码如下:

function submitFormForTime(time){
    $(\".t_h\").each(function(i, obj) {
        if ($(this).text() != time) return;
        $(this).parent().find(\"form\").each(function(i, obj){
            obj.submit();
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

nie*_*bot 8

您不能使用jQuery添加<script>标签.这已被问过很多次了.看到:

您可以使用浏览器内置函数来执行此操作,但是:

是否可以将您的函数分配给页面上的变量然后进行评估?就像是:

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    // add function to page:
    NSString * js = @"myFunction = function(){ return \"success!\"; }" ;
    [ webView stringByEvaluatingJavaScriptFromString:js ] ;

    // execute newly added function:
    NSString * result = [ webView stringByEvaluatingJavaScriptFromString:@"myFunction();"] ;
    NSLog(@"result=\"%@\"", result) ;
}
Run Code Online (Sandbox Code Playgroud)

更新显式示例:

你应该使用我上面的例子作为模板!

无论如何,这个过程分为两部分...... Javascript部分和Objective-C部分.在Javascript级别,您正在执行此操作:

// assign a function to the variable "someFunction":
var someFunction = function(){...};

// call the function in the variable "someFunction":
someFunction();
Run Code Online (Sandbox Code Playgroud)

您也可以在不将函数分配给变量的情况下执行此操作,例如,如果您不关心将来引用该函数.在没有变量和使用匿名函数的情况下执行此操作如下所示:

( function(){...} )() ;
Run Code Online (Sandbox Code Playgroud)

即而不是var A = <the function>; A();你正在做(<the function>)();

最后,如果您的匿名函数接受参数,就像您的那样,模式如下所示:

( function(var0, ..., varN){...} )(arg0, ..., argN);
Run Code Online (Sandbox Code Playgroud)

使用它作为代码的模板,你的JS最终会像:

( function(time) {
    $(\".t_h\").each(function(i, obj) 
    {
        if ($(this).text() != time)  return;

        $(this).parent().find(\"form\").each(function(i, obj) {
            obj.submit();
        });
    });
})( 123.456 );
Run Code Online (Sandbox Code Playgroud)

所以,这就是你希望web视图执行的内容......这就是我们传递给webview的代码.但是你必须在你的Obj-C程序中构建该字符串,其中包含插入当前时间参数的"复杂性"(123.456在上面的代码中).

它看起来像这样:

NSString * jsTemplate =
    @"( function(time) {"
    @"        $(\".t_h\").each(function(i, obj) {"
    @"            if ($(this).text() != time)  return;"
    @"            $(this).parent().find(\"form\").each(function(i, obj) {"
    @"                obj.submit();"
    @"            });"
    @"        });"
    @"    })( %g );" ;
NSString * js = [ NSString stringWithFormat:jsTemplate, 123.456 ] ;
NSString * result = [ webView stringByEvaluatingJavaScriptFromString:js ] ;
Run Code Online (Sandbox Code Playgroud)

我想我现在已经写得够了:)