Get property value in JavaScript with script mediator - javascript

Is there a way, inside the javascript code from WSO2 ESB's Script mediator, to get a property's value when this property has a scope different from "default" ?
In case of a property with default scope :
get-property('MyProperty')
OR
<script language="js">
mc.getProperty("MyProperty");
</script>
In case of a property with 'transport' scope :
get-property('transport','FILE_NAME')
OR
<script language="js">
mc.????????
</script>

It seems that you can not get properties of other scopes than synapse using
mc.get-property("Property Name")
since mc is instance of Synapse.MessageContext in order to get other message context properties I do something like this in java, I don't know that if it is applicable in javascript or not. I do this for axis2 message context properties.
Here "context" is the instance of Synapse.MessageContext.
org.apache.axis2.context.MessageContext axis2MessageContext;
axis2MessageContext = ((Axis2MessageContext)context).getAxis2MessageContext();

You can get property using this code:
<property name="ComingRoles" expression="your property or value" scope="default" />
<script language="js">var rolelist = mc.getProperty('ComingRoles');</script>
I test it with WSO2ESB 4.9.0
Update:
If your property is not define in default scope, first you must define it in default scope.
for example:
<property name="authheader" expression="get-property('transport','X-JWT-Assertion')"></property>
<script language="js"> var temp_auth = mc.getProperty('authheader')
</script>
It should works with ESB 4.5.0 and above

I don't think mc have get-property method.
Script Mediator use the Apache Bean Scripting Framework for scripting language support. And the mc variable represents an implementation of the MessageContext, named ScriptMessageContext.java.
[Here is the Class of ScriptMessageContext][1]
[1]: https://synapse.apache.org/apidocs/org/apache/synapse/mediators/bsf/ScriptMessageContext.html
You can check any DEFAULT scope property(method) in there.
If not, you may need to put these scope property in custom property.
Like:
<property name="CustomAction" expression="get-property('Action')"/>
Then use the getProperty("CustomAction") in JS to get them.

I did something like below
<property expression="json-eval($.)" name="JSONPayload"
scope="default" type="STRING"/>
<script language="js"><![CDATA[var pl_string = mc.getProperty("JSONPayload");
var newPayload="{\"event\": " + pl_string + "}";
mc.setPayloadJSON(newPayload);]]></script>

Related

Retrieving Variable From A Javascript File

My question is how to share a variable between two different javascript files. I am creating a form that transfers the value to a different page, which alerts that variable. I have 2 html files (index.html and form.html) as well as 2 javascript files (form.js and index.js). So basicly I want to share the variable theNick from form.js to index.js to display it using alert(). If this is not possible, is there another way to do this?
form.html:
<input type="text" id="Nick" placeholder="Nickname">
<a id="btn" onclick="submit()" href="index.html.">Submit</a>
form.js:
function submit(){
var theNick = document.getElementById("Nick").value; //retrieves theNick from your input
???
}
index.html:
<button onclick="callNick()">Click Me to view your Nickname.</button>
index.js:
function callNick(){
???????
alert(theNick); //I want to get access to this variable from form.js
}
By using the var keyword you are doing exactly the opposite. If you want to share something the easiest thing would be to bind the variable to the window object like this: window.theNick = ... and use it like alert(window.theNick).
It is sort of possible.
First of all you need to make certain that your HTML loads both JavaScript files. There isn't really a way for them to import each other, so the HTML must load both scripts.
Secondly, you need to modify your function submit to use a global variable. Global variables are initially defined outside the scope of a function. The function callNick is already looking for a global variable.
However, the submit function is defining its own, local variable because of the keyword var being used inside the function scope. Change it like so:
// Set global variable
var theNick;
function submit(){
// Use global variable
theNick = document.getElementById("Nick").value; //retrieves theNick from your input
???
}
See this article for further information.
http://www.w3schools.com/js/js_scope.asp
You could just declare the variable outside of the function
var theNick; // you could omit this entirely since it will be declared in
// global scope implicitly when you try to assign it in the function
function submit(){
theNick = document.getElementById("Nick").value; //retrieves theNick from your input
}
javascript does not care about which files declarations are made but in which scope.
By placing the variable in global scope you'll be able to access it everywhere.
Global variables are not the best coding strategy but this should help you with the concept
It seems like you need to store the variable in the local storage object of the window object, this way you can set its value on the first page and retrieve it on the second.
page 1
window.localStorage.setItem("yourVariable", "yourValue");
page 2
var myVar = localStorage.getItem("yourVariable");
Only one 'caveat': this is a html5 feature, so it comes with limitations, check this link for more info.
You can pass your variable into the url, using the ?yourVar= GET mark :
form.js
function submit(e){
var theNick = document.getElementById("Nick").value; //retrieves theNick
e.target.href+='?n='+theNick; // set the href of your anchor with your variable
}
form.html
<input type="text" id="Nick" placeholder="Nickname">
<!-- We pass the event object into our function as a parameter -->
<a id="btn" onclick="submit(event)" href="index.html">Submit</a>
index.js
function callNick(){
// get the variable from the current location
var theNick = window.location.href.split('?n=')[1];
alert(theNick);
}
index.html
<button onclick="callNick()">Click Me to view your Nickname.</button>
▶︎ Plunker where "form" has been changed to "index" and "index" to "result".
Note :
To pass multiple variables, you can use the & delimiter, and then use the window.location.search property as done in this CSS-tricks article.
▶︎ Multiple vars plunker

macrodef versus script versus javascript

I want to create a task defined by a macrodef within a script element. I was hoping to find that there would be 'set' functions corresponding to each attribute. No such luck. Is there some other API for specifying the attributes?
var statustask = project.createTask("service-status");
statustask.setPath(project.getProperty("path"));
statustask.setStatusproperty("status");
statustask.setTimeout=("1"); // this isn't suppose to take a long time.
statustask.perform();
You can probably achieve what you want using methods of the MacroInstance (a subclass of Task) you'll get from the createTask method for a macro. This:
<macrodef name="my.macro">
<attribute name="attr1" default="NOT SET"/>
<sequential>
<echo message="attr1=#{attr1}" />
</sequential>
</macrodef>
<script language="javascript"><![CDATA[
var macro = project.createTask( "my.macro" );
macro.setDynamicAttribute( "attr1", "value_1" );
macro.execute();
]]></script>
Produces this when run:
[echo] attr1=value_1

How to pass a variable in javascript

I am building a bookmarlet based on this site: http://www.latentmotion.com/how-to-create-a-jquery-bookmarklet/
This is the code of bookmarlet:
javascript:(function(){
var head=document.getElementsByTagName('head')0],
script=document.createElement('script');
script.type='text/javascript';
script.src='http://myserver.com/bookmarlet-remote.js?' + Math.floor(Math.random()*99999);
head.appendChild(script);
})();
void 0
How I can pass a variable from the bookmarlet (above code), to bookmarlet-remote.js ?
I've tried after var myNewvar='myValue', without success, Any Idea?
All JS code on a page (including bookmarklet code and scripts included have) have access to the global scope. If you define a variable without the var prefix it will be available to all other scripts.
It might be a good idea to be explicit about this. do window.myVar = "foo"; to clearly signal that you are working with global variables.
Using var in the function makes it local to that function. To make it global you have to add it to the scope of the window, so:
window.newVariable = window.newVariable || 'Your new value here';
OR
window['newVariable'] = 'Your new value here';
You'd create a public variable.
window.rnd = Math.floor(Math.random()*99999);
In bookmarlet-remote.js you just access the variable.

How do I pass argument to anonymous Javascript function?

I am writing a simple counter, and I would like to make installation of this counter very simple for users. One of the simplest counter code (for users who install it) I ever see was Google Analytics Code
So I would like to store main code in a file and user who will install my counter will need just to set websiteID like this:
<html><head><title></title></head><body>
<script type="text/javascript" src="http://counterhost.lan/tm.js">
var websiteId = 'XXXXX';
</script>
</body></html>
Here is my code:
<script type="text/javascript" src="http://counterhost.lan/tm.js">
var page = _gat.init('new');
</script>
and this is my JS file:
(function() {
var z = '_gat';
var aa = function init(data) { alert(data); alert(z);};
function na() {
return new z.aa();
}
na();
})();
I tried to understand Google Analytics javascript code but I failed to do this. Can anyone suggest how can I specify variable between tags and then read it in anonymous function which is located in a javascript file ?
Thanks.
In your example, websiteId is a global variable. So it is accessible everywhere including anonymous functions unless there is a local variable with the same name
<script> var websiteId = "something"; </script>
Later in the page or included js file...
(function() {
alert(websiteId); //this should work
})();
Can anyone suggest how can I specify variable between tags and then read it [...]
Not if your tag has both a SRC attribute and JS content.
<script type="text/javascript" src="http:/x.com/x.js"></script>
.. is different from,
<script type="text/javascript">
var x = 1;
</script>
One framework that optionally adds JS variables to SCRIPT tags is Dojo. So if you're using Dojo you can add variables to the global djConfig hash by writing,
<script type="text/javascript" src="mxclientsystem/dojo/dojo.js"
djConfig="
usePlainJson: true,
parseOnLoad: true
">
</script>
Dojo does this by running through the SCRIPT tags and evaluating the custom djConfig attribute.
This does not, however solve your problem.
You do really want two SCRIPT tags. One saying,
<script type="text/javascript">
var websiteId = '123456';
</script>
which will set a global variable websiteId and a second one,
<script type="text/javascript" src="http:/x.com/myreporter.js"></script>
which can load from anywhere and read out the websiteId variable and, I assume, report it back.
You can pass variables to an anonymous function like so:
(function(arg1, arg2, arg3) {
alert(arg1);
alert(arg2);
alert(arg3);
})("let's", "go", "redsox");
// will alert "let's", then "go", then "redsox" :)
I'm not entirely clear about what you're asking, but...
You can tag any HTML element with an id attribute, then use
document.getEntityById() to retrieve that specific element.
You can also give any HTML element user-defined attributes having names of your own choosing, then get and set them for that element within Javascript.
I think you've got a bit confused with how JS objects are called.
z is a String, '_gat'. You can't call aa() on it because a String has no member called aa. aa is a standalone function stored in a local variable. Even if you did call aa(), it doesn't return anything, so using the new operator on its results is meaningless. new can only be called on constructor-functions.
I guess you mean something like:
var _gat= function() {
// Private variable
//
var data= null;
// Object to put in window._gat
//
return {
// Set the private variable
//
init: function(d) {
data= d;
}
};
}();
Then calling _gat.init('foo') as in your second example would set the variable to website ID 'foo'. This works because the _gat object is the return {init: function() {...}} object defined inside the anonymous function, keeping a reference (a ‘closure’) on the hidden data variable.
If you specify a src attribute as part of a script element, any code within the script element tags themselves will not be executed. However, you can add this functionality with the following code. I got this technique from Crockford (I believe it was him), where he uses it in of his talks on the unrelated topic of rendering performance and asynchronously loading scripts into a page to that end.
JavaScript:
(function() {
// Using inner class example from bobince's answer
var _gat = (function() {
var data= null;
return {
init: function(d) {
console.info("Configuration data: ", d);
data = d;
}
}
})();
// Method 1: Extract configuration by ID (SEE FOOT NOTE)
var config = document.getElementById("my-counter-apps-unique-and-long-to-avoid-collision-id").innerHTML;
// Method 2: search all script tags for the script with the expected name
var scripts = document.getElementsByTagName("script");
for ( var i=0, l=scripts.length; i<l; ++i ) {
if ( scripts[i].src = "some-script.js" ) {
config = scripts[i].innerHTML;
break;
}
}
_gat.init( eval("(" +config+ ")") );
})();
HTML:
<script type="text/javascript" src="some-script.js" id="my-counter-apps-unique-and-long-to-avoid-collision-id">
{some: "foo", config: "bar", settings: 123}
</script>
Both methods have their draw backs:
Using a unique and non-colliding ID will make determining the proper script element more precise and faster; however, this is not valid HTML4/XHTML markup. In HTML5, you can define arbitrary attributes, so it wont be an issue at that time
This method is valid HTML markup; however, the simple comparison that I have shown can be easily broken if your url is subject to change (e.g.: http vs https) and a more robust comparison method may be in order
A note on eval
Both methods make use of eval. The typical mantra concerning this feature is that "eval is evil." However, that goes with say that using eval without knowing the dangers of eval is evil.
In this case, AFAIK, the data contained within the script tags is not subject to inject attack since the eval'ing script (the code shown) is executed as soon as that element is reached when parsing the HTML into the DOM. Scripts that may have been defined previously are unable to access the data contained within the counter's script tags as that node does not exist in the DOM tree at the point when they are executed.
It may be the case that a well timed setTimeout executed from a previously included script may be able to run at the time between the counter's script's inclusion and the time of the eval; however, this may or may not be the case, and if possible, may not be so consistently depending on CPU load, etc.
Moral of the story, if you're worried about it, include a non-eval'ing JSON parser and use that instead.

Can't access property generated by a script in an imported ANT target

What I'm trying to do is compile to a file which takes it's version from a constant inside my source files.
I have a setup like this (or at least the significant bits):
tasks/compile.xml
<project name="Compile">
<target name="check.version">
<script language="javascript">
regex = /VERSION.+?"([\d\.]+)";/;
r = new java.io.BufferedReader(new java.io.FileReader(new java.io.File(file)));
line = r.readLine();
while ( (line = r.readLine()) != null) {
m = regex.exec(line);
if (m) {
project.setProperty( "project.version" , m[1] );
break;
}
}
r.close();
</script>
<echo>${ant.project.name}-${project.version}</echo> <!-- = Fail-0.2 -->
</target>
</project>
And a build.xml:
<project name="Fail">
<import file="${basedir}/build/tasks/compile.xml"/>
<target name="test">
<antcall target="check.version">
<param name="file" value="${basedir}/Source.as"/>
</antcall>
<echo>${project.version}</echo> <!-- = ${project.version} -->
<echoproperties></echoproperties>
</target>
</project>
So, it seems that the property set by the script is only locally defined in that target, if i specify another target in the same project ("Compile") it won't know of that property either.
I've also tried to set a in the "Compile"-project xml but it won't be overwritten by the target anyway.
So how can I access that property generated by the script? Or is there another way for doing something like this?
I'd really like to keep that part in a separate xml as it makes the project build script so much cleaner.
Thanks!
If you call the other target via antcall, properties set within it won't be in the scope of the caller.
If you need to access properties set by another target, you could declare that target as a dependency to ensure that it gets executed before your target. Like this:
<target name="test" depends="check.version">
<echo>${project.version}</echo> <!-- = value set in check.version -->
<echoproperties></echoproperties>
</target>
Edit: There's also the AntCallBack task which is available from Ant-Contrib and Antelope:
AntCallBack is identical to the
standard 'antcall' task, except that
it allows properties set in the called
target to be available in the calling
target.

Categories