PhoneGap的ChildBrowser JavaScript的执行 [英] PhoneGap ChildBrowser Executing JavaScript

查看:79
本文介绍了PhoneGap的ChildBrowser JavaScript的执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否可以执行的PhoneGap childbrowser窗口内的JavaScript,所以我们可以在PhoneGap的应用程序操作的网站?

I wonder if this is possible to execute JavaScript inside phonegap childbrowser window so we can manipulate websites under phonegap app?

纵观大局作为一个可以创建Objective-C的一个函数,它会执行JS成childbrowser(修改childbrowser.m和childbrowser.h文件)和创建它的JS封装所以可以调用JS函数来执行JS里面childbrowser。

Looking at the big picture as one can create a function in Objective-C which executes that JS into childbrowser (modifying childbrowser.m and childbrowser.h files) and creating JS wrapper of it so one can call JS function to execute JS inside childbrowser.

我要你修改ChildBrowser对我来说有一个功能,所以我不应该失去这样做。至少给我的最初步骤。

I want you to modify ChildBrowser for me to have that functionality so I shouldn't lost doing it. At least give me initial steps.

推荐答案

好吧,我只是尝试,它在一个单一的去工作。那太精彩了!我只是修改的PhoneGap的ChildBrowser插件,它的工作。

Alright I just tried and it worked in a single go. That was amazing! I just modified ChildBrowser plugin of PhoneGap and it worked.

更新时间:

我终于得到了几分钟的时间来更新这些谁都会遇到同样的问题的答案。

I finally got few minutes to update the answer for those who will encounter the same issue.

ChildBrowserCommand.h

ChildBrowserCommand.h

- (void) jsExec:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;

ChildBrowserCommand.m

ChildBrowserCommand.m

- (void) jsExec:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options; {
    [childBrowser executeJS:(NSString *)[arguments objectAtIndex:0]];
}

ChildBrowserViewController.h

ChildBrowserViewController.h

- (void)executeJS:(NSString *)js;

ChildBrowserViewController.m

ChildBrowserViewController.m

- (void) executeJS:(NSString *)js {
    [webView stringByEvaluatingJavaScriptFromString:js];
}

ChildBrowser.js

ChildBrowser.js

/* MIT licensed */
// (c) 2010 Jesse MacFadyen, Nitobi

function ChildBrowser()
{

}

// Callback when the location of the page changes
// called from native
ChildBrowser._onLocationChange = function(newLoc)
{
    window.plugins.childBrowser.onLocationChange(newLoc);
}

// Callback when the user chooses the 'Done' button
// called from native
ChildBrowser._onClose = function()
{
    window.plugins.childBrowser.onClose();
}

// Callback when the user chooses the 'open in Safari' button
// called from native
ChildBrowser._onOpenExternal = function()
{
    window.plugins.childBrowser.onOpenExternal();
}

// Pages loaded into the ChildBrowser can execute callback scripts, so be careful to 
// check location, and make sure it is a location you trust.
// Warning ... don't exec arbitrary code, it's risky and could cause your app to fail.
// called from native
ChildBrowser._onJSCallback = function(js, loc)
{
    // Not Implemented
    window.plugins.childBrowser.onJSCallback(js, loc);
}

/* The interface that you will use to access functionality */

// Show a webpage, will result in a callback to onLocationChange
ChildBrowser.prototype.showWebPage = function(loc)
{
    PhoneGap.exec("ChildBrowserCommand.showWebPage",loc);
}

// close the browser, will NOT result in close callback
ChildBrowser.prototype.close = function()
{
    PhoneGap.exec("ChildBrowserCommand.close");
}

// Not Implemented
ChildBrowser.prototype.jsExec = function(jsString)
{
    // Not Implemented!!
    PhoneGap.exec("ChildBrowserCommand.jsExec", jsString);
}

// Note: this plugin does NOT install itself, call this method some time after deviceready to install it
// it will be returned, and also available globally from window.plugins.childBrowser
ChildBrowser.install = function()
{
    if(!window.plugins)
    {
        window.plugins = {};    
    }

    window.plugins.childBrowser = new ChildBrowser();
    return window.plugins.childBrowser;
}

我的全局变量。

var CB = null;

在我的DeviceReady事件。

On my DeviceReady event.

CB = ChildBrowser.install();
if (CB != null) {
    CB.onLocationChange = onCBLocationChanged;
}

我可以用执行任何JS进入网页。

I can execute any JS into webpage using.

CB.jsExec("alert('I am from ChildBrowser!');");

我希望我的这一贡献将带给你脸上的微笑。

I hope my contribution to this will bring smile on your face.

这篇关于PhoneGap的ChildBrowser JavaScript的执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆