Stephen on Software aka SOS

August 20, 2009

Getting image name from urls, using the Content-Disposition header

Filed under: Programming — Tags: , — sljm @ 1:37 pm

You know when you visit some forums and when you click on a link like this http://thecelebrityspotlight.com/attachment.php?attachmentid=64134&stc=1&thumb=1, they open up an image file and when you right click and save some how the image name appears ? There are no clues in the url that tells you what the image name is suppose to be and somehow magically the browser knows.

Well it appears that the browser is looking for the Content-Disposition header in the Http Response. Jim Ley @ jibbering.com has a page Using the XML HTTP Request Object, that describe how to get the headers using the XmlHttpRequest object.

For Firefox this code snippet should work. Read his blog post for more information.

 var xmlhttp=new XMLHttpRequest();
 xmlhttp.open("HEAD", "/faq/index.html",true);
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   alert("Content Disposition: "+
    xmlhttp.getResponseHeader("Content-Disposition"));
  }
 }
 xmlhttp.send(null)

August 19, 2009

Implementing File/Folder select for Firefox add-ons

Filed under: Uncategorized — Tags: — sljm @ 5:39 pm

To implement File/Folder select for firefox extensions you can look at the nsIFilePicker component.

To get the object:

var fp = Components.classes["@mozilla.org/filepicker;1"]
                   .createInstance(Components.interfaces.nsIFilePicker);

The object at this point is not initialized yet, you will need to use the init method to initialized it.

The method looks like this void init(parent,title,mode).

For parent you can use the window object that should be present. title is just the title of the file dialog, mode consist of any of the following valid values.

Constant Value Description
modeOpen 0 Load a file or directory.
modeSave 1 Save a file or directory.
modeGetFolder 2 Select a folder/directory.
modeOpenMultiple 3 Load multiple files.

Next you will need to call the show() method and check its return value to see if what the user pressed.

A sample of how to use;

const nsiFilePicker=Components.interfaces.nsIFilePicker;
fp.init(window,"My File dialog",nsIFilePicker.modeGetFolder);
var result=fp.show();
if(result == nsIFilePicker.returnOK || result == nsIFilePicker.returnReplace){
//do something
}

August 18, 2009

Getting the gBrowser object in Firefox

Filed under: Programming — Tags: — sljm @ 9:33 pm

Okies finally I have succumb and decided to do a Firefox add-on or extension to save open tabs that have documents/images inside, cause I am lazy to go one by one and save them.

So first step, try and get the tab browsers elements in the browser. In Firefox, there is a gBrowser object that if you can get it will help you to get all the browser tabs and their uris.

For the documentation, the only way that you can use gBrowser directly is that if you are under the scope of browser.xul when you are doing overlays. So for other windows like dialogs and sidebars, you will need to use window.opener.getBrowser method to get to the gBrowser object. If you look at the documentation for the window object, there is no mention of the getBrowser method, seems like the docs need some updating.

Look at the Getting access to the browser in the Tabbed Browser section of the MDC, you will find the various different methods for getting a gBrowser object.

Blog at WordPress.com.