Android variable to JavaScript - javascript

I am building an Android WebView which loads an url on a server of mine. However, now I want to use an identifier from the Android as parameter of a function on the JS code and I simply can't do it.
I already tried appending "javascript:myFunction(myParameter);" to the url on the loadUrl method and tried adding a JavascriptInterface. Neither of them did work, and it looked like it needed my files (html and js) to be local. However, I need to open a xml document and, as far as I know, you can't do it with local files, so I couldn't test it.
I am not used to web/android dev, so I don't even know if I'm not trying to do something dumb, but it seems to me that it should be simple to do and yet I'm stuck on it. Thanks in advance.

Add the variable to the URL, for example:
file://my/file.html?variable=someValue
You should then be able to parse the URL in JS using document.location, for example:
alert(document.location);

Related

Is it possible to debug JS when the URL keeps changing?

I'm trying to debug the JavaScript of a website which references some JS via a random URL path each pageload. This makes Chrome think that the JS is a different document, but I can see the same JS is being served up each time. However, breakpoints don't stay because the URL is different.
Is there a way I can get breakpoints to stay somehow between page loads of the same JS with different URL paths, or maybe break on function name instead of the normal way?
The only way i think is to put
debugger;
everywhere you wanted the breakpoint. As you have rightly understood that the browser considers it as a new file, and there is no way to tell the browser that it is same file when the URL is different.

how to change content of external javascript with javascript

im working on a userscript for youtube, so i cant change the actual file on their server, but i need a way to change one of the functions in it.
the function seems to not exist before and after it runs.
i could probably change the URL of the file in the html, for example with:
document.body.innerHTML = document.body.innerHTML.replace( /*javascript file URL before*/g, "*after*");
but im afraid the file is called from other places too, like somewhere in their obfuscated javascript i cant make out, so it would be better to not change its URL, but the code the page loads from it.
i dont know how though. is there maybe a way to listen to when a function is called from the file, and give a different code instead?
Just override the javascript function like:
yourFunction = function(a){
alert(a)
};

Parsing xml (xforms) document with javascript `document.getElementsByTagName` not working in phonegap android

I am having some trouble parsing xform xml with javascript.
The structure of the root of the xml is:
<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:ex="http://www.w3.org/2001/xml-events" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://openrosa.org/javarosa">
<h:head>
<h:title>Phonegap Survey</h:title>
</h:head>
<h:body>A ton more tags here</h:body>
</h:html>
I am using firefox (v.12) to develop and trying to deploy to phonegap/cordova on android. I am getting the data from an odk server using jquery ajax. I am specifiying dataType: 'xml' and it is responding with a document object as expected. In firefox I have written an app that presents data from this xml to the user. I am using built in javascript DOM functions like:
var title = surveyXML.getElementsByTagName('h:title')[0].firstChild.data
var body = surveyXML.getElementsByTagName('h:body')[0];
var text = surveyXML.getElementsByTagName('text')
I have written the full app (and it works fine) using these methods. But now that I have copied my files over to phonegap (running cordovo 1.7.0, on android emulator 2.3) on eclipse none of the DOM calls are returning elements!! I get something like:
05-31 13:21:28.686: D/CordovaLog(841): file:///android_asset/www/formcontroller.js:
line 159 : TypeError: Result of expression 'body' [undefined] is not an object.
For all the calls. I have verified that the device has the correct xml (it's not empty or anything)
So having to get this done I tried to use jQuery to navigate the document object hoping it knew something I didn't. Using calls like:
$(surveyXML).find('title');
$(surveyXML).find('h:title')
do not work at all. But tags without the h: prefix work fine, if I were to search for $(surveyXML).find('text') it returns all text elements as expected.
Seeing as the root elements are <html> I tried to specify dataType: html (just to try it even though the document is clearly marked as <?xml version="1.0"?>) and jquery is unable to parse it, as expected.
So I am wondering: How can I parse this XML cross platform so that it can work both in a browser and phonegap. And, supposing that it can't work in both using the same DOM manipulation functions, how can I at least make it work in phonegap??
As always any help is appreciated. Thank you.
EDIT: For right Now I am just manually referencing the exact locations of the elements like to get to body I am going xml.firstChild.children[1]. Because I am underdeadline. I def feel like there should be a way to use getElementsByTagName still. ty.
This has been unanswered for 2 weeks so I am just posting what I did.
For right Now I am just manually referencing the exact locations of the elements like to get to body I am going xml.firstChild.children[1].
So all I did was not search for tags with the namespace and just reference them directly.

jquery load to hide content

There is javascript on my webpage, but I need to hide it from my users (I don't want them to be able to see it because it contains some answers to the game.)
So I tried using Jquery .load in order to hide the content (I load the content from an external js file with that call). But it failed to load. So I tried ajax and it failed too.
Maybe the problem comes from the fact that I'm trying to load a file located in my root directory, while the original page is located in "root/public_html/main/pages":
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
url : "../../../secret_code.js",
dataType: "text",
success : function (data) {
$("#ajaxcontent").html(data);
}
});
});
</script>
1) Why can't I load a file from the root directory with ajax or load method?
2) Is there another way around?
PS: I'm putting the file in the root directory so people can't access it directly from their browsers...
1) if the file isn't accessible via web browsers, than it's not accessible via ajax (ajax is part of the web browsers
2) try /secret_code instead of ../../../secret_code.js
What is your system setup? Are you using a CMS?
Even if you add the javascript to the page after page load a user with a tool like firebug can go and view it. I don't think what you are doing is really going to secure it. An alternate solution is that you could minify and obfuscate the javascript that you use in your production environment. This will produce near unreadable but functioning javascript code. There are a number of tools that you can run your code through to minify and obfuscate it. Here is one tool you could use: http://www.refresh-sf.com/yui/
If that isn't enough then maybe you could put the answers to the game on your serverside and pull them via ajax. I don't know your setup so I don't know if that is viable for you.
Navigate to the URL, not the directory. Like
$.ajax({
url : "http://domain.com/js/secret_code.js",
..
Even if you load your content dynamicly, it's quite easy to see content of the file using firebug, fiddler or any kind of proxy. I suggest you to use obfuscator. It will be harder for user to find answer
Take a look at the jQuery.getScript() function, it's designed for loading Javascript files over AJAX and should do what you need.
Try jQuery's $.getScript() method for loading external
Script files, however, you can easily see the contents of the script file using Firebug or the developer toolbar!
Security first
You can't access your root directory with JavaScript because people would read out your database passwords, ftp password aso. if that would be possible.
You can only load files that are accessible directly from browsers, for example, http://www.mydomain.com/secret_code.js
If it can't be accessed directly by the browser, it can't be accessed by the browser via ajax. You can however use .htaccess to prevent users from opening up a js file directly, though that doesn't keep them from looking at it in the google chrome or firebug consoles.
If you want to keep it secret, don't let it get to the browser.

ExternalInterface passing values from swf to an iFrame on the same page

On a page I have a an iFrame and a swf, I've been trying to use ExternalInterface to pass values from the swf to the iFrame, anyone ever tried this and had any luck? I won't be able to post any code until tomorrow, will update then if needed.
Thanks in adavnce.
Here is a visualization of what I need to accomplish, perhaps if it can't be done the way I said someone will have a suggestion of another way to accomplish this.
Try to target the frame. This should work with one frame on the page or you will have to change the index.
duhFrame = window.frames[0]
targetElement = duhFrame.getElementById('someIframeElement');
If this doesn't work for you then use LocalConnection and put a hidden swf on the iframe file.
LocalConnection is Global to the browser so be aware that if two browser windows are open you will get two iframes connecting on the same name and will get some funky results. So change the connection name via query string and flashvars.
Its a real hackish workaround but it will work.
[EDIT]
One more thing make sure your javascript callback function is getting called as ExternalInterface has major domain issues when running under the file structure and not in a domain(EX: clicking run in the editor ). Try uploading to your server and change your embed code to allow for it. Also dont forget to update your crossdomain.xml(s).
You cannot access iframes from outside of them. These are security reasons, and browsers will and should block such attempts.

Categories

Resources