gpt4 book ai didi

iphone - PhoneGap ChildBrowser 执行 JavaScript

转载 作者:行者123 更新时间:2023-12-03 20:26:03 26 4
gpt4 key购买 nike

我想知道是否可以在phonegap childbrowser窗口中执行JavaScript,以便我们可以在phonegap应用程序下操作网站?

从大局来看,我们可以在 Objective-C 中创建一个函数,将 JS 执行到 childbrowser 中(修改 childbrowser.m 和 childbrowser.h 文件),并创建它的 JS 包装器,这样就可以调用 JS 函数来执行 JS在子浏览器内。

我希望您为我修改 ChildBrowser 以获得该功能,这样我就不会迷失方向。至少给我初步的步骤。

最佳答案

好吧,我刚刚尝试过,一次就成功了。那太精彩了!我刚刚修改了PhoneGap的ChildBrowser插件,它起作用了。

已更新

我终于有几分钟的时间为那些遇到同样问题的人更新答案。

ChildBrowserCommand.h

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

ChildBrowserCommand.m

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

ChildBrowserViewController.h

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

ChildBrowserViewController.m

- (void) executeJS:(NSString *)js {
[webView stringByEvaluatingJavaScriptFromString: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 事件上。

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

我可以使用以下命令在网页中执行任何 JS。

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

我希望我对此所做的贡献能给您带来微笑。

关于iphone - PhoneGap ChildBrowser 执行 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5757066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com