Wednesday, 16 July 2014

Fire javascript events manually!!!

Hi All,

In this post, I am going to explain how I fired javascript events of my webpage manually. I came across number of posts, when I googled about it.

But the solution, I am going to explain below is the most simplest one and I am unable to find further reference related to how this feature works.

Anyway, I am documenting my findings here. Once, I come across more info - I will update this post.

Lets get into the scenario:

I want to fire 'onchange' event in one of my field. But I need to change the value of the corresponding field using javascript - hence 'onchange' event will not get triggered.

So, I was looking for javascript that would trigger the 'onchange' event and I found the simple solution as below:

var sel1 = document.getElementById('testID');

As you can observe, I am just getting the reference of the my element and updating the value of it.

Then with a single line code, I am triggering the 'onchange' event. It is as simple as that and we are done. Event will get triggered!!!

Below post elaborates this concept in bit detail. You can refer this for your reference:

I used above mentioned code in Firefox and Chrome. It worked. I am not sure about older version of IE. But, I hope it should work in IE10+ version.

Thursday, 3 July 2014

wkhtmltopdf - Generate pdf of password protected pages

Hi All,

In this post, I am going to explain about how to generate pdf images of password protected pages using wkhtmltopdf solution.

Wkhtmltopdf is a simple but powerful opensource solution for generating pdfs out of web page images. You can find more information about this solution here.

wkhtmltopdf uses headless browsers to launch the webpages and generate the pdf. As they are headless browsers, we cannot enter data in to their UI fields directly. Hence using wkhtmltopdf on password protected pages is a challenge.

Wkhtmltopdf asks us to handle this feature by using the command --cookie.jar (as stated in help command):

--cookie-jar <path>             Read and write cookies from and to the
                                      supplied cookie jar file

It asks the user to use above mentioned command for login operation, store the cookie information of login process in one of the jar file and use that jar file while accessing subsequent password protected pages.

Above task is not straight forward and it requires basi c understanding of how http request and response works. Whenever we perform login operation, browser sends the login information that we entered as a 'http post' parameter to the server. Server, in turn, validates the credentials and authenticates the user (if the credentials are valid) by sending the confirmation in cookie.

So, lets see how this process works with a demo site ''. We will login to this website using wkhtmltopdf and we will generate the pdf of password protected pages with it.

At first, we need to understand what are all the post parameters concerned with the login operation of our website. We can use 'Tamper Data' plugin of Firefox to identify the post parameters.

Start the 'Tamper Data' and perform the login operation. You can find the parameters that are posted when performing login operation. Below screenshot shows the 'post' parameters of '' captured using Tamper Data:

As you can observe, username and password are not the only fields, and we have additional post parameters as well (In another website, I had 4 parameters corresponding to login operation). Please note the values 'Post Parameter Name' and 'Post Parameter Value' of all the parameters.

Now we need to make a request to login page of our application with all the post parameters and should save the generated 'cookie' information in one jar. Then we need to make request for password protected pages with the cookie jar to generate pdf.

Lets see the commands of wkhtml to perform above operations:

wkhtmltopdf --cookie-jar my.jar --post uid jsmith --post passw demo1234 --post btnSubmit Login demo.pdf

Above command, makes a request to login page and pass the post parameters with the '--post' command as mentioned. Now wkhtmltopdf will make a post request from login page and would save the cookie details in 'my.jar'.

Now we are done!!!

Hereafter, make every other request to password protected pages (as below) with the cookie jar and wkhtmltopdf would awesomely generate the pdf of the requested pages.

wkhtmltopdf --cookie-jar my.jar demo1.pdf

Thank you!!!

Tuesday, 24 June 2014

Java Reflections on inner classes

Java Reflections is another powerful feature of Java that enables you to inspect the contents of java classes in run time without even knowing the name of the class.

Consider this scenario that you have an access to java object and you want to read the following properties of the object:

1. Object's class name
2. Details of the fields in the class
3. Details of the methods present in classes and etc.

Java reflections allows you to perform the above mentioned activities with its powerful feature and you can find more details about it here.

Consider another scenario, that you have an access to the object that is an instance of inner class of another class. Now you want to perform the same activities that are listed above on the inner class.

In this blog, I am going to explain how to inspect the fields and properties of inner classes using Java reflections.

Say, you are having access to the object (innerObj), which is an instance of the inner class 'innerClass' (say, its enclosing class is com.test.outerClass').

Whenever you fetch the name of the object innerObj with below statement


it will return the string value as 'com.test.outerClass$innerClass'

With this value as a reference - we can obtain the name of the outer class, as below:

                String parent = innerObj.getClass().getName().split("\\$")[0];

From then on, we can get the access to inner class details as below:

Class<?> enclosingClass = Class.forName(parent);
Object enclosingInstance = enclosingClass.newInstance();

Class<?> innerClass = Class.forName(myObj.getClass().getName());
Constructor<?> ctor = innerClass.getDeclaredConstructor(enclosingClass);
Object innerInstance = ctor.newInstance(enclosingInstance);

Field[] fields = innerClass.getDeclaredFields();

Now, we can perform various operations as detailed in the Java reflections on 'innerClass' and inspect the various attributes of inner classes.

Thursday, 19 June 2014

Add javascript event handlers to frames and iframes of web page

Hi All,

Those who are familiar with javascript would know, how powerful are javascript event handlers and how it makes their life easier in web applications.

Javascript event handlers are inserted at document or at individual element level in web sites and they listen for the specific events and perform the required actions as defined by the developer.

You can find the definition about events and its various types here:

Events can be inserted into web pages with help of below mentioned methods:

i) addEventListener (for firefox, chrome and IE 9+ browsers)
ii) attachEvent (for IE 5-8)

But, when we need to attach event handlers to elements present inside frames and iframes of web pages - we need to do some additional work. In this blog, I am going to explain how to achieve it.

Event handlers can be added into iframes as below:

var iframes = document.getElementsByTagName('iframe');

function callback(e){
For adding into Frames, you can use the below statement:

var frames = document.getElementsByTagName('frame');

function callback(e){

Wednesday, 18 June 2014

Eclipse Kepler - Network Connection - Socks Proxy Bypass Issue

In this post, I am going to describe about how to overcome one of the Eclipse Kepler issue that bogged me for few days before identifying it.

Before getting into issue, I would like to cover some basic information related to 'Network Connections' preferences of Eclipse.

Most of the Eclipse users would have set the proxy in the 'Network Connections' preference settings during their course of work. For long time, I had a question of what is its significance and what does the options 'native', 'direct' and 'manual' means.

As far as its significance is concerned, during plugin installation and updates - Eclipse would directly contact the internet and download the updates. If you are sitting behind proxy, then Eclipse would not be able to successfully connect to internet and would be hanging whenever we attempt to make the connection.

To address this, we are required to mention the proxy details in the 'Network Connections' preferences of Eclipse. Eclipse provides us with 3 options here:

a. Direct - Makes the connection directly without the aid of any proxies. If you are connecting to internet only through proxy, then this option would not help you and you need to change it to either native or manual.
b. Native - Makes the connection using the proxy set at the OS level. With this option, eclipse fetches the proxy details that you would have set from your browsers (Tools --> Intenet Option --> Connections --> LAN Settings) and uses it.
c. Manual - Makes the connection with user provided proxy details. As far as 'Manual' option is concerned, we can set the proxy value for the protocols 'http', 'https' and 'socks' in it.

Coming back to the issue that bogged me, whenever we set the proxy values for the above mentioned 3 protocols - looks like, Eclipse Kepler would ignore http and https protocol and tries to establish the internet connection only using 'socks' protocol.

If your update site does not support communication using 'socks' protocol, then Eclipse could not make connection and it would hang indefinitely.

Fortunately, people have found this bug and identified the work around for it. Work around is simple just clear the proxy details under 'socks' and proceed. It worked for me and helped me resolve the issue!!

You can refer this blog for more details: 

Tuesday, 17 June 2014

Firefox addon - Pass data between addon script and content script

In this blog, I am going to describe how to pass data between addon script and content script of Firefox addon.

Those familiar with Firefox addon would know about addon script and content script files.

Firefox sdk provides various apis to interact with local system attributes such as registry keys, clipboard content, context menu etc. And these apis could be accessed from a script called addon script (main.js under lib folder of addon).

But from this addon script, we cannot access any of the document object that contains the html details of web page that we load in browsers. To access document object, we need to use separate script called content script that could be accessed from data folder. These restrictions are introduced to ensure the security of user information.

This article from developer website provides detailed information about the features of addon script and content script.

As far as communication between addon script and content script, they have a feature called 'port', which enables the user to send and receive data between the scripts.

In this blog, I am explaining about how to send and receive the data using PageMod module. We can also send and receive information from other modules such as tabs and panel.

Have the below code in main.js file:

  include: "*",
  contentScriptFile: data.url("textscript.js"),
  contentScriptWhen: "ready",
  onAttach: startListening

function startListening(worker) {
    worker.port.emit('check','test emit');

Here, we are attaching 'startListening' function with PageMod object and it emits a message 'test emit' with the user defined event 'check'.

And in contentscript, below code will capture the data emitted by addon script:

self.port.on('check', function(message) {

Similarly, you can emit the message from content script and can receive it in addon script. You can find those details here:

Monday, 16 June 2014

Install Mozilla Firefox addon programmatically

This blog summarizes the procedure to install Mozilla Firefox addon programmatically.

We do have a command '-install-global-extension' to install addons for the firefox versions that are earlier than ver 3.6. (Refer:

For the latest firefox versions, Mozilla does not support the above mentioned command. Hence we need to install addons through some work arounds.

This blog details about how to install addons programmatically for latest versions of firefox, which do not support '-install-global-extension' command.

Usually, firefox addons are installed at the user level and not at the system level. In other words, if you install a firefox addon - it will be available only for the user who installed it. If another user logins to the same machine with his credentials, addon will not be available to him/her.

User can install the addon either by dragging and dropping the .xpi files into firefox browser or by placing the xpi file in the below location:


If user installs the addon by placing the .xpi file in the above location, firefox will prompt the user to install the addon at the next launch of firefox.

Some points to be noted here:

1. Usually 'AppData' folder will be a hidden folder. Hence we need to change the windows folder setting to display it.
2. '.default' folder will precede with some random text. For every user, this random text will change. Hence we need to handle this programmatically.
3. We need to ensure that .xpi file is named with the same value of '<em:id>' parameter as defined in the install.rtf file.

So, to install addon programmatically, we just need to copy and paste our .xpi file into the location mentioned above. Below code mentions how to find the .default file using regular expression. After finding the location, you are just required to copy and paste the xpi file into the identified location:



public class New {
                static String dataFolder = System.getProperty("user.home") + "\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles";
  public static void main(String[] args) {
                 File dir = new File(dataFolder);
                 FileFilter fileFilter = new RegexFileFilter(".*default");
                 File[] files = dir.listFiles(fileFilter);
                 File extDir = new File(files[i]+"\\extensions")

Please let me know if you have any queries with respect to above explanation.