2011-04-22 81 views
2

我不知道這是否可能在phonegap childbrowser窗口內執行JavaScript,以便我們可以在phonegap應用程序下操作網站?PhoneGap ChildBrowser執行JavaScript

查看大圖可以在Objective-C中創建一個函數,該函數將該JS執行到childbrowser(修改childbrowser.m和childbrowser.h文件)並創建其JS包裝器,以便可以調用JS函數來執行JS裏面的childbrowser。

我想讓你修改ChildBrowser讓我有這個功能,所以我不應該失去它。至少給我初步的步驟。

回答

7

好吧我只是試過,它在一個工作。那太精彩了!我剛剛修改了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!');"); 

我希望我對此的貢獻能帶給你笑臉。

+0

你可以分享一些你做的修改ChildBrowser插件嗎? – 2011-04-24 02:29:06

+0

是的,我確定我可以分享它,但是如何? – Neutralizer 2011-04-24 13:27:49

+1

分叉GitHub項目 - http://help.github.com/fork-a-repo/ – metadaddy 2011-05-03 06:42:20

相關問題