My extension needs to read the history.dat file from the user's
profile. I am trying to read the data stream and will need to extract
the URLs from the file. Is there some easy/standard command/API to do
this? I guess it must be a commonly implemented feature.
I have tried using String.fromCharCode(a-decimal-number); and written
some functions based on code I saw on prodigynet.co.uk, they are
listed at the very end of this post. I have not come across an API
which could extract the URLs from the firefox history file. It would
be great if someone could please point me in the right direction. All
I can get using an alert on the read buffer is 61,104,116,116, ...
etc.. which stands for =http:... . I tried to using alerts on
String.fromCharCode for each character position via charAt, but it
didn't work :-( .
Thanks again,
-A
=====================================================================
aResult is obtained from within
var ffhistobserver = {
onStreamComplete : function(aLoader, aContext, aStatus, aLength,
aResult)
{
}};
and contains the actual data in the history file..next I tried to
convert that to text..alas!!
var fftexti = clean_numstr(aResult, 16);
var fftexto = "" ;
for(jloop1=0; jloop1 < fftexti.length; jloop1+=2)
{
fftexto+=byteToChar(parseInt(fftexti.substring(i, i+2), 16));
}
function byteToChar(histnum)
{
if(histnum < 32 || histnum > 255) return " ";
return charArray[histnum-32];
}
function clean_numstr(raw_str, histbase)
{
var ret_str = "";
var histc = "";
var histi;
for(histi=0; histi < raw_str.length; histi++) {
histc = raw_str.charAt(histi);
if(histc == "0" || parseInt(histc, histbase) > 0) {
ret_str += histc;
}
}
return ret_str;
}
var charArray = new Array(
' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-',
'.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';',
'<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd',
'e',
'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', ' ', 'Ç', 'ü',
'é', 'â', 'ä', 'à', 'å', 'ç', 'ê', 'ë', 'è', 'ï', 'î', 'ì', 'Ä', 'Å',
'É', 'æ', 'Æ', 'ô', 'ö', 'ò', 'û', 'ù', 'ÿ', 'Ö', 'Ü', 'ø', '£', 'Ø',
'×', 'ƒ', 'á', 'í', 'ó', 'ú', 'ñ', 'Ñ', 'ª', 'º', '¿', '®', '¬', '½',
'¼', '¡', '«', '»', '_', '_', '_', '¦', '¦', 'Á', 'Â', 'À', '©', '¦',
'¦', '+', '+', '¢', '¥', '+', '+', '-', '-', '+', '-', '+', 'ã', 'Ã',
'+', '+', '-', '-', '¦', '-', '+', '¤', 'ð', 'Ð', 'Ê', 'Ë', 'È', 'i',
'Í', 'Î', 'Ï', '+', '+', '_', '_', '¦', 'Ì', '_', 'Ó', 'ß', 'Ô', 'Ò',
'õ', 'Õ', 'µ', 'þ', 'Þ', 'Ú', 'Û', 'Ù', 'ý', 'Ý', '¯', '´', '', '±',
'_', '¾', '¶', '§', '÷', '¸', '°', '¨', '·', '¹', '³', '²', '_', ' ');
----- Original Message ----
> From: newbiegalore <banerjee...@gmail.com>
> To: dev-ext...@lists.mozilla.org
> Sent: Thursday, May 8, 2008 9:30:53 PM
> Subject: reading firefox history file: ascii conversion API
>
> Hello all :-),
> I am learning JS and extension programming and so am
> facing a few hurdles. I would greatly appreciate any pointers/code-
> samples/links to possible solutions.
>
> My extension needs to read the history.dat file from the user's
> profile. I am trying to read the data stream and will need to extract
> the URLs from the file. Is there some easy/standard command/API to do
> this? I guess it must be a commonly implemented feature.
>
> I have tried using String.fromCharCode(a-decimal-number); and written
> some functions based on code I saw on prodigynet.co.uk, they are
> listed at the very end of this post. I have not come across an API
> which could extract the URLs from the firefox history file. It would
> be great if someone could please point me in the right direction
For firefox 3:
http://developer.mozilla.org/en/docs/Using_the_Places_history_service
HTH,
Eric
____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
Thanks Eric for the pointer.
I tried
var jaalhistoryService = Components.classes["@mozilla.org/browser/nav-
history-service;1"]
.getService(Components.interfaces.nsINavHistoryService);
// no query parameters will get all history
// XXX default sorting is... ?
var ffhistoptions = jaalhistoryService.getNewQueryOptions();
// no query parameters will return everything
var ffhistquery = jaalhistoryService.getNewQuery();
// execute the query
var fftexto = jaalhistoryService.executeQuery(ffhistquery,
ffhistoptions);
alert(fftexto);
But nothing pops up on screen. The places description says that
without using any query parameters I can get everything in a flat file
format.. yet I don't seem to receive any data. Any ideas?
Update: I tried out the following code
alert('hello');
//using standard history query mechanism
var jaalhistoryService =
Components.classes["@mozilla.org/browser/nav-history-service;
1"].getService(Components.interfaces.nsINavHistoryService);
var ffhistoptions = jaalhistoryService.getNewQueryOptions(); //
line1
var ffhistquery =
jaalhistoryService.getNewQuery(); //line2
var fftexto = jaalhistoryService.executeQuery(ffhistquery,
ffhistoptions); //line3
var cont = fftexto.root;
var n = cont.childCount;
alert(n);
n =Math.min(n, 3);
cont.containerOpen = true;
for (var i = 0; i < n; i ++) {
var node = cont.getChild(i);
alert(node.title + "\n");
}
cont.containerOpen = false;
The first alert('hello') works fine when its at the beginning as
presented here, when I try to move it between lines 1/2/3 it does not
fire. Also later in the code, alert('n') denoting the childcount does
not get fired. In the original example on MDC, dump was used instead
of alert, but that shouldn't be the only reason, right?
I linked this code to run whenever I click on a page and it seems that
something is happening in the background because the response time
slows down a little. So it could be running the query but not dumping
on the screen.. Do I have to use some kind of Views as in Places:Views
for accessing the history data?
Again thanks for all the great help :-)
Update: Could someone *please* let me know how to convert an ascii
stream read from a file to a text blob.
I am writing to a file using
var jaalcachefile = Components.classes["@mozilla.org/file/
directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("ProfD", Components.interfaces.nsIFile);
//point to cache file
jaalcachefile.append("cache-classified.txt");
jaalcachefile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE,
0666);
//package data to push in file
var shorturl = snipurl(window.content.document.location.href);
//cache file should contain shorturl, fullurl, safety-
analysis and date
var date1 = new Date() ;
var pushdata = shorturl + "@" +
window.content.document.location.href + "@" + analysis + "@" +
date1.getTime();
// Write in cache file
var cacheStream = Components.classes["@mozilla.org/network/file-
output-stream;
1"].createInstance(Components.interfaces.nsIFileOutputStream);
cacheStream.init(jaalcachefile, 0x02 | 0x10 | 0x20, 0666, 0);
cacheStream.write(pushdata, pushdata.length);
cacheStream.close();
This writes the data fine to a file, but when I read it its
104,61,56,.. basically ascii values,
I tried using the following code from MDC and it also works fine
writing the characters in the file,
var charset = "UTF-8"; // Can be any character encoding name that
Mozilla supports
var os = Components.classes["@mozilla.org/intl/converter-output-stream;
1"]
.createInstance(Components.interfaces.nsIConverterOutputStream);
// This assumes that fos is the nsIOutputStream you want to write to
os.init(cacheStream, charset, 0, 0x0000);
os.writeString("Umlaute: \u00FC \u00E4\n");
os.writeString("Hebrew: \u05D0 \u05D1\n");
os.close();
but when I read it back using the following, again, (inspired by MDC
code) I got back 104,61,56,.. in the alert box.
var jaalcacheios = Components.classes["@mozilla.org/network/io-service;
1"].getService(Components.interfaces.nsIIOService);
var jaalcachefileURI = jaalcacheios.newFileURI(jaalcachefile);
var jaalcachechannel =
jaalcacheios.newChannelFromURI(jaalcachefileURI);
var jaalcacheobserver = {
onStreamComplete : function(aLoader, aContext, aStatus,
aLength, aResult)
{
alert(aResult);
};
var jaalcachesl = Components.classes["@mozilla.org/network/stream-
loader;1"]
.createInstance(Components.interfaces.nsIStreamLoader);
jaalcachesl.init(jaalcachechannel, jaalcacheobserver, null);
I have also tried to use code from the first 3 highlighted boxes on
http://developer.mozilla.org/en/docs/Reading_textual_data and
specified UTF-8 as the encoding but the alerts just don't fire.
Can someone please point me to an API to convert the output from the
file reading (aResult) into a text based blob that I can parse. This
would also help solve my previous problem of not being able to parse
the history.dat file. Somehow I am failing to use containers
properly.
Thanks again,
-A
Solved by using aRead[offset] instead of aRead.charAt(offset)..