2

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

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.

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.

1 Answer 1

7

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

UPDATED

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

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

My global variable.

var CB = null;

On my DeviceReady event.

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

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.

4
  • Could you share some of what you did to modify the ChildBrowser plugin?
    – CM.
    Commented Apr 24, 2011 at 2:29
  • Yes sure I can share it but how?
    – Umair A.
    Commented Apr 24, 2011 at 13:27
  • Actually I am willing to share because it feels really good when you can help others but the problem is I can't handle Git and I am noob about it. Is there a guide for it?
    – Umair A.
    Commented May 3, 2011 at 17:56
  • I have used Git before but using old school method like "try and fail". I wanted to learn Git the professional way as I'm succeeding so far. I understand how it works now at least. I will soon update my many of the stuff to public which are just lying in my hard drive.
    – Umair A.
    Commented Dec 3, 2012 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.