Re: Calling javascript from Cocoa 'facelessly'

2010-07-03 Thread Izidor Jerebic


You did not spell out where exactly this code will run - who is going  
to be the host application.


To prevent all stupid errors (typing, etc.), I would suggest you start  
from the beginning - create a simple GUI app, make a window with  
WebView, and then load your stuff into this WebView. After that works,  
you can continue pruning your application until you have what you  
need. But on every step, you can make sure that everything works, and  
you can easily find out what causes problems if you make small enough  
steps.


This approach will also make easy to ask questions  - you will have  
very specific problems on every step and that makes it easy to ask/ 
google etc.


But if this code of yours already runs in a GUI app, it is all wrong -  
you should put the code doing the login (everything after [mainFrame  
loadRequest:request])  in a method of a load delegate of WebView. Look  
at the WebView documentation methods, especially setFrameLoadDelegate:  
and delegate method - (void)webView:(WebView *)sender  
didFinishLoadForFrame:(WebFrame *)frame.


After this works, you can proceed to first make your own webview  
object. AFAIK webview only works with main runloop, so make sure your  
host application has one and it works ok.


izidor


On 2.7.2010, at 22:32, Geoffrey Holden wrote:

Thanks for this.  I tried it, checking that provisionalDataSource is  
nil before continuing as per the documentation, but sadly I still  
get the same result - regardless of whether I have the real  
javascript function name or a made up one, and regardless of whether  
the URL I use is real or imaginary.  Most frustrating.  I checked  
that my NSRunLoop was working properly by using sleep - and it is.


Perplexed.  Any other ideas?


On 30 Jun 2010, at 19:30, Michael Ash wrote:


On Wed, Jun 30, 2010 at 1:01 PM, Geoffrey Holden
45rpmli...@googlemail.com wrote:
I realize that this is a hell of a big query, and I'm not entirely  
sure that I've given enough information for anyone to help answer  
it.  At the very least, I guess I'm hoping for a reference to some  
really solid documentation on how to do what I'm trying to do -  
the documentation on ADC seems to be a little light in this area.



From the code you've posted, I suspect the problem is that the page

hasn't actually loaded by the time you try to run your JS code.
WebView generally works asynchronously. Something like [mainFrame
loadRequest:request] will return quickly and the WebView will not
actually load things until the runloop runs, and then you'll get
notified later on when it's done loading.

What you'll want to do is call loadRequest:, then run the NSRunLoop  
on
the main thread until your load delegate is told that the page is  
done

loading. Then you should be able to do your JS stuff.

Note that it is possible, and not all that hard, to run an NSRunLoop
in a faceless program, it just requires a bit more manual  
intervention

than in a GUI app where it's all set up for you.

If you're already doing that and your code was just abbreviated,  
well,

ignore the above

Mike
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com




Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/45rpmlists%40googlemail.com

This email sent to 45rpmli...@googlemail.com



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/ij.cocoadev%40gmail.com

This email sent to ij.cocoa...@gmail.com


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calling javascript from Cocoa 'facelessly'

2010-07-03 Thread Geoffrey Holden
Thanks for this.  I'll try that.  I have produced WebViews before with no 
problems, although this is the first time that I've tried to do it facelessly.  
What I'd really like, of course, is a means of running JavaScript without a 
webview - i.e. provide a link to the JavaScript directly, without any of the 
html business.

In answer to your question though, this would be a client app.  The server 
component, accessing the data, can be accessed through the javascript / webpage.


On 3 Jul 2010, at 15:01, Izidor Jerebic wrote:

 
 You did not spell out where exactly this code will run - who is going to be 
 the host application.
 
 To prevent all stupid errors (typing, etc.), I would suggest you start from 
 the beginning - create a simple GUI app, make a window with WebView, and then 
 load your stuff into this WebView. After that works, you can continue pruning 
 your application until you have what you need. But on every step, you can 
 make sure that everything works, and you can easily find out what causes 
 problems if you make small enough steps.
 
 This approach will also make easy to ask questions  - you will have very 
 specific problems on every step and that makes it easy to ask/google etc.
 
 But if this code of yours already runs in a GUI app, it is all wrong - you 
 should put the code doing the login (everything after [mainFrame 
 loadRequest:request])  in a method of a load delegate of WebView. Look at the 
 WebView documentation methods, especially setFrameLoadDelegate: and delegate 
 method - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame 
 *)frame.
 
 After this works, you can proceed to first make your own webview object. 
 AFAIK webview only works with main runloop, so make sure your host 
 application has one and it works ok.
 
 izidor
 
 
 On 2.7.2010, at 22:32, Geoffrey Holden wrote:
 
 Thanks for this.  I tried it, checking that provisionalDataSource is nil 
 before continuing as per the documentation, but sadly I still get the same 
 result - regardless of whether I have the real javascript function name or a 
 made up one, and regardless of whether the URL I use is real or imaginary.  
 Most frustrating.  I checked that my NSRunLoop was working properly by using 
 sleep - and it is.
 
 Perplexed.  Any other ideas?
 
 
 On 30 Jun 2010, at 19:30, Michael Ash wrote:
 
 On Wed, Jun 30, 2010 at 1:01 PM, Geoffrey Holden
 45rpmli...@googlemail.com wrote:
 I realize that this is a hell of a big query, and I'm not entirely sure 
 that I've given enough information for anyone to help answer it.  At the 
 very least, I guess I'm hoping for a reference to some really solid 
 documentation on how to do what I'm trying to do - the documentation on 
 ADC seems to be a little light in this area.
 
 From the code you've posted, I suspect the problem is that the page
 hasn't actually loaded by the time you try to run your JS code.
 WebView generally works asynchronously. Something like [mainFrame
 loadRequest:request] will return quickly and the WebView will not
 actually load things until the runloop runs, and then you'll get
 notified later on when it's done loading.
 
 What you'll want to do is call loadRequest:, then run the NSRunLoop on
 the main thread until your load delegate is told that the page is done
 loading. Then you should be able to do your JS stuff.
 
 Note that it is possible, and not all that hard, to run an NSRunLoop
 in a faceless program, it just requires a bit more manual intervention
 than in a GUI app where it's all set up for you.
 
 If you're already doing that and your code was just abbreviated, well,
 ignore the above
 
 Mike
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 
 Help/Unsubscribe/Update your Subscription:
 http://lists.apple.com/mailman/options/cocoa-dev/45rpmlists%40googlemail.com
 
 This email sent to 45rpmli...@googlemail.com
 
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 http://lists.apple.com/mailman/options/cocoa-dev/ij.cocoadev%40gmail.com
 
 This email sent to ij.cocoa...@gmail.com
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 http://lists.apple.com/mailman/options/cocoa-dev/45rpmlists%40googlemail.com
 
 This email sent to 45rpmli...@googlemail.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not 

Calling javascript from Cocoa 'facelessly'

2010-07-02 Thread Geoffrey Holden
Thanks for this.  I tried it, checking that provisionalDataSource is nil before 
continuing as per the documentation, but sadly I still get the same result - 
regardless of whether I have the real javascript function name or a made up 
one, and regardless of whether the URL I use is real or imaginary.  Most 
frustrating.  I checked that my NSRunLoop was working properly by using sleep - 
and it is.

Perplexed.  Any other ideas?


On 30 Jun 2010, at 19:30, Michael Ash wrote:

 On Wed, Jun 30, 2010 at 1:01 PM, Geoffrey Holden
 45rpmli...@googlemail.com wrote:
 I realize that this is a hell of a big query, and I'm not entirely sure that 
 I've given enough information for anyone to help answer it.  At the very 
 least, I guess I'm hoping for a reference to some really solid documentation 
 on how to do what I'm trying to do - the documentation on ADC seems to be a 
 little light in this area.
 
 From the code you've posted, I suspect the problem is that the page
 hasn't actually loaded by the time you try to run your JS code.
 WebView generally works asynchronously. Something like [mainFrame
 loadRequest:request] will return quickly and the WebView will not
 actually load things until the runloop runs, and then you'll get
 notified later on when it's done loading.
 
 What you'll want to do is call loadRequest:, then run the NSRunLoop on
 the main thread until your load delegate is told that the page is done
 loading. Then you should be able to do your JS stuff.
 
 Note that it is possible, and not all that hard, to run an NSRunLoop
 in a faceless program, it just requires a bit more manual intervention
 than in a GUI app where it's all set up for you.
 
 If you're already doing that and your code was just abbreviated, well,
 ignore the above
 
 Mike
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 

 Help/Unsubscribe/Update your Subscription:
 http://lists.apple.com/mailman/options/cocoa-dev/45rpmlists%40googlemail.com
 
 This email sent to 45rpmli...@googlemail.com


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Calling javascript from Cocoa 'facelessly'

2010-06-30 Thread Geoffrey Holden
My company has a webpage (internal, sadly, so I can't let you have a link to 
look at) which is entirely driven by Javascript, and which requires 
authentication to access (the authentication too is driven by Javascript, 
rather than by the more normal https sign in methods).

The website is a) ugly as hell, b) not very flexible and c) won't format 
correctly on the screen of an iPhone.  The first issue is aesthetic, the second 
is a serious issue and the third could well be a problem in the future.

The obvious answer would be 'change the site', but that isn't very practical 
sadly - political issue, don'tchaknow!  I'm working hard to increase uptake of 
Apple equipment at work, and if I can encapsulate the website as a friendly app 
on the Mac that could be a major driver.  This is just a project that I'm 
kicking around out of hours for the love of it and in the hope that Macs might 
end up being a preferred solution!

Initially, I just want to produce a proof of concept which runs at the command 
line (the connection will be implemented as a faceless plug in) and I don't 
want a webview in the UI.  So, that said, I cobbled together the following test:

//for header
IBOutlet WebView * webView;
NSDictionary* pluginPrefs;


//for body
NSString* preferencesFile = [NSHomeDirectory() 
stringByAppendingPathComponent:@/Desktop/Prefs.plist];

pluginPrefs = [[NSDictionary alloc] initWithContentsOfFile:preferencesFile];


WebFrame *mainFrame = [webView mainFrame];
NSURL *url = [NSURL URLWithString:[pluginPrefs objectForKey:@ServerURL]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[mainFrame loadRequest:request];
  
id win = [webView windowScriptObject];

NSArray *args = [NSArray arrayWithObjects:
 @signIn,
 [pluginPrefs objectForKey:@Username],
 [pluginPrefs objectForKey:@Password],
 nil];


//login (in theory!)
NSLog(@Parameters: %@, %@,[pluginPrefs 
objectForKey:@Username],[pluginPrefs objectForKey:@Password]);
id result = [win callWebScriptMethod:@muc.userAuth withArguments:args];
if(![result isMemberOfClass:[WebUndefined class]]) 
NSLog(@%@, result);
else
NSLog(@Unable to initialize connection);

The code builds correctly, and runs - but, just for starters, the result seems 
to be wrong (it should return 'Unable to initialize connection' as far as I can 
tell), because it always returns the same result whether I have a 'valid' 
javascript function or not.  Of course, once (if!) I get it to connect 
correctly, I then need to solve the problem of capturing the stream of messages 
that will be received in not-quite-real-time. I'll cross that bridge when I 
come to it.

The muc.userAuth function can be found in and external .js file, referenced in 
the page html with the following tag:
script type=text/javascript src=../js/ejc.js?ver=2.0.1.1/script

I realize that this is a hell of a big query, and I'm not entirely sure that 
I've given enough information for anyone to help answer it.  At the very least, 
I guess I'm hoping for a reference to some really solid documentation on how to 
do what I'm trying to do - the documentation on ADC seems to be a little light 
in this area.

Thanks, and regards,

Geoff___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calling javascript from Cocoa 'facelessly'

2010-06-30 Thread Michael Ash
On Wed, Jun 30, 2010 at 1:01 PM, Geoffrey Holden
45rpmli...@googlemail.com wrote:
 I realize that this is a hell of a big query, and I'm not entirely sure that 
 I've given enough information for anyone to help answer it.  At the very 
 least, I guess I'm hoping for a reference to some really solid documentation 
 on how to do what I'm trying to do - the documentation on ADC seems to be a 
 little light in this area.

From the code you've posted, I suspect the problem is that the page
hasn't actually loaded by the time you try to run your JS code.
WebView generally works asynchronously. Something like [mainFrame
loadRequest:request] will return quickly and the WebView will not
actually load things until the runloop runs, and then you'll get
notified later on when it's done loading.

What you'll want to do is call loadRequest:, then run the NSRunLoop on
the main thread until your load delegate is told that the page is done
loading. Then you should be able to do your JS stuff.

Note that it is possible, and not all that hard, to run an NSRunLoop
in a faceless program, it just requires a bit more manual intervention
than in a GUI app where it's all set up for you.

If you're already doing that and your code was just abbreviated, well,
ignore the above

Mike
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com