I'm trying to create a new page in Alfresco, but the tutorials gives to me the information that i have to create three files: new-page.get.js, new-page.html.ftl and new-page.get.xml , like Aikau - http://docs.alfresco.com/5.0/concepts/dev-extensions-share-page-creation.html
But the javascript is different, for example, I try to get the current URL with: window.location.search or make console.logor alert. But, in this three cases, I got "undefined" like "window is undefined"
Why is this javascript different? What type of javascript is? Where I can get tutorials, for example, to program this javascripts?
I want to make a window.location.search to get the current URL , but if I don't have this command, what can I use for this effect?
Normally, the Alfresco way wouldn't be to get the raw URL. Instead, you should be using the built-in argument processing
Since Alfresco itself is open source, we can look at Alfresco for some examples! So, starting with the groups get webscript, we see a URL pattern defined as:
<url>/api/groups?shortNameFilter={shortNameFilter?}&zone={zone?}&maxItems={maxItems?}&skipCount={skipCount?}&sortBy={sortBy?}</url>
With that, we see a whole bunch of pre-defined parameters on the URL.
Next, we look at the javascript controller behind that webscript, and we see things like:
var shortNameFilter = args["shortNameFilter"];
var zone = args["zone"];
Those URL parameters are then parsed into your webscript in the args variable, available for you to fetch as a hash.
No need to do any raw URL munging yourself, if you define your webscript correctly the framework does it all for you!
The JavaScript isn't different, the language itself is still the same.
window, console and alert are just APIs supplied by browsers. They aren't a native part of JavaScript.
The documentation you linked to should be your starting point for figuring how what APIs are available.
You can get the server URL in the Javascript web script (on the backend) by
var path = url.getServer()
http://localhost:8080 will be returned for example
Here is the list of available the methods - you can concatenate them to get a direct URL:
Related
I currently have a Java Spring project where the bulk of the front is in AngularJS, HTML, etc. I currently have an application.properties file that holds:
name:myName
idNum:8888888888
password:squirrels
contextPath:/ilovesquirrels-ui
server:0000
ui.firstLink: www.google.com
ui.secondLink: www.yahoo.com
ui.thirdLink: www.w3schools.com
myBool: False
The first five seem to read in automatically to a place I cannot seem to find. The last four, I'd like to access in Javascript to store the urls and the boolean. I'd like to write something in JS like:
var myLink1 = "something that accesses ui.firstLink in application.properties";
var myLink2 = "something that accesses ui.secondLink in application.properties";
var myLink3 = "something that accesses ui.thirdLink in application.properties";
Right now, I am reading information from a Javascript file that holds a JSON object that I'd eventually like to get rid of. It was all the same information as application.properties, except it is more visible to the end user. How do I get the links from my application.properties file into Javascript variables?
I don't like to mix JavaScript with the server-side language as it:
makes life harder for editors
is harder to read
couples JS with the server-side technology
Therefore, I would put the desired variable expressions as meta tags or data-attributes and then access them using JS.
<body data-uifirstlink='<%=properties.getProperty("uifirstLink")%>';
accessed with jQuery by: $('body').data('uifirstlink');
note: ideally, these data attributes should be as contextual as possible (instead of body choose a more specific place).
<meta name='uifirstlink' content='<%=properties.getProperty("uifirstLink")%>' />
accessed with jQuery by: $('meta[name="uifirstlink"]').prop('content')
A service for retrieving properties is not good because:
You need to wait for the page to load to run the call and use the variables
You have unnecessary Ajax calls
You create more coupling between client and server
Finally, you could consider cookies or headers, but I don't think is so straightforward.
You could create a jsp file with this:
<script>
var myLink1 = '<%=properties.getProperty("ui.firstLink")%>';
</script>
If you want to use spring, create a PopertiesPlaceHolderConfigurer and use spring tags in jsp. See here
When I inspect a webpage I can see javascript files with name and a query string attached to their name. Like: jquery-1.9.1.min.js?ctag=0$$16.0.4230.1217.
I googles ctag but didn't find anything useful which I can understand.
I want to know what is the difference between files with ctag and without it?
(jquery-1.9.1.min.js vs. jquery-1.9.1.min.js?ctag=0$$16.0.4230.1217)
When you see http parameters added to the end of a script url it's usually for one of two reasons.
To cache the script in the browser.
Usually a version number is added, the cache can then be forced to update by changing the version. i.e.
http://example.com/js/myscrpt.js?ver=0.4
To send data to the server.
It might be that the script being returned is actually generated server side and the parameter is sending a value that is used in the generation of that script. i.e.
http://example.com/js/myscript.js?userid=935284025805
UPDATE: searching the web, it seems that links deployed on Sharepoint using JSLINK adds a ctag parameter to the urls of javascript files. It's possible the link is from a Sharepoint site, see here and here for Sharepoint questions about the added ctag
If I have a function written in Google Spreadsheets script editor that retrieves the data in the spreadsheet in JSON format, how can I access that function outside of the script editor in my own code? I want to access that JSON and manipulate it in my own code. Is there a way to do that using the Spreadsheets API? I format it in a specific way inside script editor so I can't just use the json-in-script provided. In the call (http://spreadsheets.google.com/feeds/feed/key/worksheet/public/basic?alt=json-in-script&callback=myFunc) there's a callback function for myFunc. Can I use the function I defined in the script editor to replace myFunc?
Following your comment that brings some details on your use case, there is a Google-Apps-Script feature specially designed to give access to some functions you wrote from within another script : is is called libraries and is fully described in the documentation.
EDIT, following 2cond comment:
Calling a GS function from a javascript (or any other language) script that is not a Google Script (GS) is not possible if you consider using it as a function...
but
what you can eventually do - depending on the data this function must handle - is to deploy a script as a webApp running as a service and call this service from your external app using the equivalent of an urlFetch (that's the service doing that in GS).
The service will have an url to which you can add parameters and it will return a result that you can use in your local app.
Of course this workflow has a few limitations and might quickly become complex but in many cases it is fully workable.
Note that the url you will have to use in the "versioned" one ending with .exec (Not sure this word is correct but I mean the published url that corresponds to a version of your script and not the ".dev" one that one can use to test a script in GS).
You'll find details about that in the documentation and on many other ressources, including SO. The url is typically something like this :
https://script.google.com/macros/s/AKfycbyw-2WtmF7wsd__________azjImbMWm5YrxB8/exec?someParameter=someValue&otherParam=otherVal // etc...
I am trying to pass a single piece of information (using a query string) to my Facebook page tab application.
For example, if the user clicks on this URL:
-http://apps.facebook.com/myappname/?app_data=mydata
I would want to be able to access 'mydata' in the app.
From the reading I've done, Facebook does not allow GET requests, but it's possible to do this using app_data and signed_request.
However, I have not been able to find any information on how to set this up using the javascript SDK (is that even possible?) and .NET (ideally, I would be able to implement this with just JavaScript). I have no idea how to set up and read data using a signed_request, and the documentation around signed_requests is confusing me more than helping. I would really like simple instructions on how to implement this feature.
EDIT:
I think I've almost figured it out. In case anyone else is looking for an answer to this, I put what I did so far below. Also, if you see any room for improvement please let me know. I don't claim this is perfect by any means, but it works.
First, the url needs to be the page tab url, not the direct url to the app (like I posted above):
-http://www.facebook.com/pages/PageName/########?sk=app_#########&app_data=mydata
Here is the javascript code that is working for me:
//get value of signed request and split it
var signedRequest = $('#mainContent_hfSigned').val().split(".");
//decode json (this does not work on ie - needs to be replaced)
var decodedJson = window.atob(signedRequest[1]);
//parse json to gain access to parameters
var jsonParams = jQuery.parseJSON(decodedJson);
//append the app_data varible to ensure it's being read properly
$('.message').append('Your app_data param is "' + jsonParams.app_data + '"')
On jsfiddle:
http://jsfiddle.net/C3xsm/1/
One thing I know I still need to do is replace atob with a base64url decoder for javascript. I'm thinking about using this one:
http://code.google.com/p/stringencoders/source/browse/trunk/javascript/base64.js
If it works well, I'll update it here. Or if anyone knows of something that works better, please let me know.
ideally, I would be able to implement this with just JavaScript
That’s not possible, because as you already found out the signed_request parameter is POSTed to your page, and JavaScript has no access to POST parameters itself.
The documentation on the signed_request parameter has instructions on how to parse/decode it server-side; the example is in PHP, but it should be easy to transfer the basic algorithm (if you can call it that) to your .NET environment.
I use curl, in php and httplib2 in python to fetch URL.
However, there are some pages that use JavaScript (AJAX) to retrieve the data after you have loaded the page and they just overwrite a specific section of the page afterward.
So, is there any command line utility that can handle JavaScript?
To know what I mean go to: monster.com and try searching for a job.
You'll see that the Ajax is getting the list of jobs afterward. So, if I wanted to pull in the jobs based on my keyword search, I would get the page with no jobs.
But via browser it works.
you can use PhantomJS
http://phantomjs.org
You can use it as below :
var page=require("webpage");
page.open("http://monster.com",function(status){
page.evaluate(function(){
/* your javascript code here
$.ajax("....",function(result){
phantom.exit(0);
}); */
});
});
Get FireBug and see the URL for that Ajax request. You may then use curl with that URL.
There are 2 ways to handle this. Write your screen scraper using a full browser based client like Webkit, or go to the actual page and find out what the AJAX requesting is doing and do request that directly. You then need to parse the results of course. Use firebug to help you out.
Check out this post for more info on the subject. The upvoted answer suggests using a test tool to drive a real browser.
What's a good tool to screen-scrape with Javascript support?
I think env.js can handle <script> elements. It runs in the Rhino JavaScript interpreter and has it's own XMLHttpRequest object, so you should be able to at least run the scripts manually (select all the <script> tags, get the .js file, and call eval) if it doesn't automatically run them. Be careful about running scripts you don't trust though, since they can use any Java classes.
I haven't played with it since John Resig's first version, so I don't know much about how to use it, but there's a discussion group on Google Groups.
Maybe you could try and use features of HtmlUnit in your own utility?
HtmlUnit is a "GUI-Less browser for
Java programs". It models HTML
documents and provides an API that
allows you to invoke pages, fill out
forms, click links, etc... just like
you do in your "normal" browser.
It has fairly good JavaScript support
(which is constantly improving) and is
able to work even with quite complex
AJAX libraries, simulating either
Firefox or Internet Explorer depending
on the configuration you want to use.
It is typically used for testing
purposes or to retrieve information
from web sites.
Use LiveHttpHeaders a plug in for Firefox to see all URL details and then use the cURL with that url.
LiveHttpHeaders shows all information like type of method(post or get) and headers body etc.
it also show post or get parameters in headers
i think this may help you.