How to get result from a JavaScript file in Java - javascript

I downloaded a JavaScript file from the following link
I stored this code into 'goldprice.js'
Then I somehow minimized the whole HTML code from here
to the following simplified code.
<html>
<head></head>
<body>
<h1>
Gold Price:
<xyz id="gpotickerLeft_price"></xyz>
</h1>
<script src="goldprice.js"></script>
<script type="text/javascript">
var leftticker
var ticker = new GPOTicker();
ticker.addTicker(leftticker, 'gpotickerLeft');
ticker.start();
</script>
</body>
</html>
Now my question is how to get this whole thing working in Java program? I want the output of the above HTML code in my Java program so that I may be able to use that automatically updated value in jlabel.
I have seen some examples on
javax.script.ScriptEngine and javax.script.ScriptEngineManager but I don't really think my problem is that simple.

If you take a closer loook to the javascript code, you'll find, that the javascript fetches the actual data from a webservice. Look for the code that starts with
{api2:"http://api2.goldprice.org/Service.svc/GetRaw/",resourcesRootPath:"/goldprice/img/"
That seems to be their webservice, which you could try to access directly from Java. Note, however, that you ought to ask for the permission to use that service. If it is an service that is meant to be officially used by others, there should exist a documentation and all to explain how to use it. That would be the best way to go.

Related

How to embed javascript file content into html page in Thymeleaf?

I need to embed javascript directly into html page generated by Thymeleaf
Something like:
<script th:include="../static/assets/generated/scripts.js"></script>
But this simple usage leads to SAXParseException...
Is there any easy way to switch off parsing of the th:included
content? Or any other way how to embed content of resource int the result page?
I don't think that is possible out of the box. You could probably write an extension that can do it. Or maybe there is an existing one, but I couldn't find one right now.
Does it have to to be a separate JavaScript file? Can't you put your JavaScript code into a fragment and include it like any other fragment?
NB: Including JavaScript into your HTML file like that is usually bad web design und may be a sign that you have bigger problems and you haven't structured your code well. Why do you think you need to do that? Why can't you refer to an external script file?
Thats not a Thymeleaf-Thing. It's classic html:
<script src="/assets/generated/scripts.js"></script>
In version 3.0, you can do it in this way
<script th:src="#{/webjars/jquery/dist/jquery.min.js}"></script>

How to make a javascript logic not visible in page resource in angularjs?

I am working on a angularjs module in which we are trying to avoid a particular piece of logic in the view source on the browser. I have just given the skeleton code where the logic written inside script tags should not be shown in the page source of the browser.
<html ng-app="myApp">
<head>
<script>
if(something){
do something...
}
<script>
</head>
<body></body>
</html>
Is there any way in angularjs or javascript such that the logic written inside script is not visible in view page source on browser?.
This is not possible as the code needs to be executed by the browser, thus transferred to the client. Anyone can read/copy your code.
The most you can do is to use uglify or similar tool to minify your code. This will have to advantages for you. First, your code will be hard to read for others who might want to exploit your application. But keep in mind, it is still not impossible to understand your code even when minified - it just takes more time. And second, your scripts will become smaller thus making your page load slightly faster.
One more solution is make it look a bit complex so the user trying to read it does not understand it(in case you are not interested in making a minfied version)
Store all the Variables required in a separate file and access them form that file.
Even your base URL should be stored as global variable.
These are some options which you can use but making a minfied code is best practice.
You can't hide code to your client. It is executed in the client browser.
The best thing you can do here is to minify your code. It will make it unreadable without parsing it. Also, the code will be smaller, and will be loaded by the browser faster.
As a side note, luckily all the code is visible: imagine if malicious code could be executed without you know it.

reference external JavaScript template

I am using jsrender as the JavasScript template engine. The usage is quite straightforward:
In HTML Page, define a script tag with template-ed HTML content:
<script id="myTemplate" type="text/x-jsrender">
<div>{{:name}} ({{:year}})</div>
</script>
...
<body>
<div id="content"></div>
</body>
In JavaScript, define a variable, and call render function against the template:
var model = {
name: "Findekano",
year: 2012
};
$('#content').html($('#myTemplate').render(model));
The question is:
I do not want to keep the template definition in the host HTML page, I'd like to keep it in a separate file along with the JavaScript file so it can be more modular.
I'd like to have something to work as below:
<script id="myTemplate"
type="text/x-jsrender"
src="template/myTemplate.html">
</script>
where myTemplate.html defines the necessary HTML template snippet. I tried the code above but it doesn't work. Could anyone suggest an alternative way to solve the problem?
If you do not want to have to use an AJAX call and using a more memory doesn't concern you, there is at least one alternative solution I can think of. AJAX might be cleaner, but I thought I'd put this out there anyways.
document.write('<script id="myTemplate" type="text/x-jsrender"><div>{{:name}} ({{:year}})</div></script>');
If you put that into an external file and link it via
<script type="text/javascript"
src="template/myTemplate.js">
</script>
Then the script will append a valid script tag copy of the string to the DOM that jsrender can actually access. Of course, this does end up using roughly double the memory on the client but... if it's not too big of a file it shouldn't be a problem.
Browsers won't download scripts in languages they do not (internally) support (based on the type attribute).
Get the URI from the src attribute, then request the resource with XHR.

Can I find out the result of javascript in Python?

I'm now making a web crawler.
getting a link from HTML is easy part but acquiring a link from the result of javascript is not easy for me.
Can I get the result of javascript so as to know where a link is referred to?
for example.
How can I retrieve the link to google.com from javascript code in Python?
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
to google
</body>
<script>
document.getElementById('goog').onclick = function() {
window.location = "http://google.com";
};
</script>
</html>
You would need to install node.js and run a separate piece of code that executes the Javascript code in context to emit the html. This is possible using jsdom but the key to it is extracting the Javascript code from the HTML page, and setting up the context correctly.
Python doesn't offer a way to execute the Javascript, which would be a large task, and may not even be what you want, because you won't know how to execute all of the appropriate Javascript.
For the code you showed, you could simply regex the entire thing to get URL-like strings from it, but that could be very ad-hoc and error-prone.

insert external html

I am just learning to create websites and I have been programmming a lot of OOB languages before so I am kind of used to write small objects and just paste them where I want them.
I would like to know if there is a way to create for instace a login form och what ever piece of html that you use regulare on sites and save that to a file, html or xml and then with the help of javascript add this form onto your main site.
I will try to make an example to clearify what I want to do, it's the javascript that I do not know how to write...
form.html
< form id="form_login" >
Username: <input type="text" id="username"/><br>
Password: <input type="password" id="password"/><br>
<input type="button" id="button_login" value="log in" onclick="login(this)"/>
</form>
index.html
<html>
<head>
</head>
<body>
<div>
<script type="text/javascript" src="javascript.js"></script>
</div>
</body>
</html>
javascript.js
// this is where I am rendered clueless, I want my javascript to render out my form
$(#"div").html(form.html)
I am thinking that I should do a serverrequest to retrieve the form.html but I don't know how.
Personally, I believe that JavaScript is not very well suited for the job - people that turn off JavaScript (and Google too!) won't see important parts of your page (in Google's case, that can lead to less visitors!). Inclusions as you mention them should happen at the server. See Wikipedia's article on Server Side Includes for a possible solution.
Use load method instead:
$('#div').load('form.html', function() {
alert('Load was performed.');
});
It is better to include your files through server-side languages as well using server side includes.
You may want to consider using jQuery templates. They're in beta but have proven to be very useful to keep HTML code, well, HTML.
http://api.jquery.com/category/plugins/templates/

Categories