do external javascript on a website objective C - javascript

This is my first post so I am sorry for any mistakes I made :)
I know the title is confusing, but i will try to explain what I want.
I got this website, on the website there is a schedule, this schedule change when you change different values so that it consist with the appropriate person. When you change a value a JavaScript function is called. Is there any way for me to do this JavaScript (on a site I don't own) from objective-C.
So, what I want is this (in a very plain description):
do JavaScript call change() on website: "My website" (I know this is not valid, but this is what I want)
Also I would like to be able to change the different values and pass the function with parameters.
Thanks
Kristian
The website is here (Norwegian):
http://www.novasoftware.se/webviewer/(S(mfq2npum0th2lxb0g5oy3045))/design1.aspx?schoolid=60810&code=545354&type=3&id=1559)
Edit:
Hello I found another way to solve my problem.
The website is automatically generating an image for each of the students. The url of an image is similar to this link
(had some problems posting the URL)
Where you got &week=42 (you can change this to whatever you want and it works perfect). So I got it working for the MAC (in Safari 5).
But when I try to use this URL in the iPhone simulator I only get a white screen.
this is the code i use:
[webView loadRequest:[NSURLRequest
requestWithURL:[NSURL
URLWithString:#"http://www.novasoftware.se/ImgGen/schedulegenerator.aspx?format=png&schoolid=60810/nb-no&type=3&id={76C567C0-161E-42C3-B054-8941DDD04F52}&period=&week=42&mode=0&printer=0&colors=32&head=0&clock=0&foot=0&day=0&width=1405&height=683&maxwidth=1405&maxheight=683"]]];
It works if I use a simpler URL like "http://www.google.com" :S Does the iPhone not support for aspx?

I only know this in the context of iPhone APIs...
Assuming you have the page loaded in a UIWebView inside your app...
You can call UIWebView method stringByEvaluatingJavaScriptFromString: to send javascript to the page.
If the calling function returns anything, it will be typecast as a NSString and returned to your app, but in your case it looks like you can just ignore the return value. You might need to make your Javascript look something like:
(function(){
change();
return "hello world";
})();
because I'm not sure how well it handles void return values.

Related

Transferring a javascript variable to to Objective C (IOS)

I am working on creating an app and am a relatively new Objective C programmer. On a page, "test.php", I have a javascript variable. Here is a simplified version.
<script>
var idx = 45;
document.write(idx);
</script>
I would like to be able to view the value of the variable "idx" in my IOS app, which I have created using Xcode. What would be the optimal way to connect to the page, test.php, and transfer my Javascript variable to Objective C so I could use it as part of my IOS App?
Any help would be greatly appreciated. Thank you.
You will need to communicate between the webview and the native code using a combination of two pieces. First, you will need to "trigger" the webview into returning some value by calling stringByEvaluatingJavaScriptFromString: on the webview. When the webview tries to navigate because of the javascript call, you can intercept it on the native side with the WebView's shouldStartLoadWithRequest: method.
For details, see the specifics here Invoke method in objective c code from HTML code using UIWebView
That is only if you need some activity in the webview to trigger the fetch (or notify the native code that the value has been set). If the value is always available via JS, you can simply use stringByEvaluatingJavaScriptFromString: as follows:
NSString *returnvalue = [self.webviewForHtml stringByEvaluatingJavaScriptFromString:#"var idx = "return_value";return idx;"];

Intercepting window.external.notify call from javascript in objective C

I need to intercept a javascript call to window.external.notify which is returning a security token string that I need to get into my objective C code. The javascript is being executed in a UIWebView. The script there looks like:
<script type="text/javascript">
try{
window.external.notify('{<extremely long dictionary as a JSON string>}');
}
catch(err){
alert("Error ACS50021: window.external.notify is not registered.");
}
</script>
I need to somehow get the JSON dictionary into a string in objective C. I've tried going through the method here: http://www.stevesaxon.me/posts/2011/window-external-notify-in-ios-uiwebview/; but it just seems to interfere with the rendering of the html/javascript page and also not capture the string (I don't have a handy acs identifier to check for to know I'm intercepting the right call). Other similar questions have been asked, but I haven't been able to get any working, many seem extremely hackish, and they are usually quite out of date. I've tried accessing the webview's html content, but the token isn't present there, because it's only sent through window.external.notify, which errors out with the alert that it isn't registered.
I know there's now a native JS->objC bridge in iOS 7, and I only need to support iOS 7+, but I've never used it and I can't seem to get that up and running either. It also appears to be mainly for having your own JS source files as part of your app, and not for communicating with a server through a UIWebView, but if I'm wrong about that, let me know.
Try this method.Here the page is redirected and the token is loaded using receive data method.

Android: Calling javascript from WebView

We're coding an app for Android. It's a WebView that contains Html5 pages. We're using loadUrl() webview's method, in order to push some native OS variables to html, such as:
webview.loadUrl("javascript:myJavascriptFunc('" + myAndroidOSVar + "');");
It works pretty fine. But if we are typing on an input from page while loadUrl() is called, we lose focus of our input fields, even if javascript function called just changes a flag on cache.
Do you know other way to call a Javascript function from WebView instead of loadUrl()?
This is the closest you can get to avoiding the issue: WebView hides soft keyboard during loadUrl(), which means a keyboard cannot stay open while calling javascript
Basically you are avoiding the loadUrl call by queuing up commands on the native side and at an interval letting the JS bridge get the commands and execute using JS eval() or something.
I'm not sure exactly how it will respond if you have multiple fields in the webview, but I came up with a workaround to keep the keyboard shown: https://stackoverflow.com/a/18776064/513038. It may be worth trying.
yes, there is a way by adding a JavaScriptInterface to your WebView , refer this tutorial for more explanation and details

How to manipulate Javascript websites in Perl

I have been asked to automate the logging into a webapp(what I assume to be one, that runs a lot of .aspx and .js scripts) that, currently, can only run in IE. Now i am programming in Perl and have tried to use Win32::IE::Mechanize to run the IE browser and log in. What i did was try an extract all the forms from the webapp, and given the users information, fill out the required forms, but this is where the problem arises, when I try and run the subroutine no forms appear......
So then I transitioned into WWW::Mechanize and used the post subroutine(from LWP::UserAgent) which solved the problem for the most part. Now i've run into a problem in the response, from the server, I get this script as the content of the response and I don't know what to do with it.
So my question is: Using Perl how can I go about to manipulate a Javascript functions in a website? Would that even be a valid solution to the problem?
I am open to writing this in other programming languages as well. Thanks in advance for the help!
(So that I can fully log in to the webapp)
Update: The content of the response:
var msgTimerID;
var strForceLogOff = "false";
function WindowOnLoad(){
if ("false" == "true" && "false" == "false")
MerlinSystemMsg("",64);
if ("false"=="true")
msgTimerID = window.setInterval("MerlinSystemMsg(10095,64)", 300000,'javascript');
}
function MyShowModal(){
showModalDialog("", window, strFeatures);}
function clearMsgInterval(){
window.clearInterval(msgTimerID);
}
function WindowOnUnLoad(){
if(top.frames(0).document.getElementById("OPMODE").value =="LOGOFF"){
strFeatures = "width=1,height=1,left=1000,top=1000,toolbar=no,scrollbars=no,menubar=no,location=no,directories=no,status=yes,resizable=1";
window.open("ForceLogOff.aspx","forcelogout",strFeatures);
}
}
window.onbeforeunload = WindowOnUnLoad;
window.onload = WindowOnLoad;
There is also this Frame Title that has the src:
FRAME TITLE="Service Desk Express Navigator" SRC="options_nailogo.aspx" MARGINWIDTH=0 MARGINHEIGHT=0 NORESIZE scrolling=no
Trying to emulate the browser with a fully functioning JS engine is going to be a mighty big task. Instead, I'd suggest that you just try to emulate the actual interaction with the web site and not care what HTML/JS is actually sent back. Your server side code doesn't care how the HTTP submissions take place, only that they do. Admittedly this is more fragile if the forms change a lot, but at least you're not trying to implement a full browser.
So look at modules like LWP::UserAgent, HTTP::Request and HTTP::Response.
I'm copying and pasting my answer to your other duplicate question here
(You should consider deleting one of these?)
That content is the website source :)
How WWW::Mechanize deals with FRAME SRC as a link:
Note that <FRAME SRC="..."> tags are parsed out of the the HTML and
treated as links so this method works with them.
You'll want to use follow_link on that link.
As far as dealing with Javascript, there is support for a Firefox Add-on called MozRepl that you can use in conjunction with WWW::Mechanize::Firefox that I have used in the past to call Javascript code while crawling a page.

What does the JS function 'postMessage()' do when called on an html object tag?

I was recently searching for a way to call the print function on a PDF I was displaying in adobe air. I solved this problem with a little help from this fellow, and by calling postMessage on my PDF like so:
//this is the HTML I use to view my PDF
<object id="PDFObj" data="test.pdf" type="application/pdf"/>
...
//this actionscript lives in my air app
var pdfObj:Object = htmlLoader.window.document.getElementById("PDFObj");
pdfObj.postMessage([message]);
I've tried this in JavaScript as well, just to be sure it wasn't adobe sneaking in and helping me out...
var obj = document.getElementById("PDFObj");
obj.postMessage([message]);
Works well in JavaScript and in ActionScript.
I looked up what the MDC had to say about postMessage, but all I found was window.postMessage.
Now, the code works like a charm, and postMessage magically sends my message to my PDF's embedded JavaScript. However, I'm still not sure how I'm doing this.
I found adobe talking about this method, but not really explaining it:
HTML-PDF communication basics
JavaScript in an HTML page can send a message to JavaScript in PDF content by calling the postMessage() method of the DOM object representing the PDF content.
Any ideas how this is accomplished?
"postMessage" is essentially one half of a publish/subscribe model for JavaScript.
You can post any message you like, but it relies on something listening for that message event. So your postMessage is essentially you throwing an event over the fence, hoping that something is waiting on the other side to do something with the event. On the other side of the fence is (hopefully) an event listener like this:
window.addEventListener("message", doSomethingWithTheMessage, false);
function doSomethingWithTheMessage(event) {
alert("Do Something!");
}
More information here: https://developer.mozilla.org/en/DOM/window.postMessage
In your specific example, when you embed an object such as a PDF, Flash or something along those lines, they may well listen for events in exactly this way.

Categories

Resources