General :  K-Meleon Web Browser Forum
General discussion about K-Meleon 
Pages: 12Next
Current Page: 1 of 2
Javascript to format and display history.txt...
Posted by: MonkeeSage
Date: January 28, 2003 12:42PM

function history() {
x = new File("C:\\Program Files\\K-meleon\\Profiles\\default\\fzjjgnqc.slt\\history.txt");
fileIsOpen = x.open("r");
if (fileIsOpen) {
buf = "History file name: " + x + "<br>";
while (!x.eof()) {
line = x.readln();
len = line.length();
if (!x.eof())
line = line.substr(14, len);
buf = buf + line + "<br>";
alert(buf);
}
if (x.error() != 0)
alert("Error reading history file");
x.close();
}
}

I tried to clip the first 14 characters off each line and write out the results to an alert (next step to write them to an html, or use write())... but for some reason it doesn't work... (e.g., the first line -- objectname = new File("path") doesn't seem to work -- alert(objectname) doesn't even return!)... mabye someone who knows more about js and file handling than I do, like Hugo (hint! winking smiley ) could figure out what went wrong and help out? smiling smiley

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: MonkeeSage
Date: January 28, 2003 12:57PM

oops...

len = line.length;

not...

len = line.length();

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: MonkeeSage
Date: January 28, 2003 01:30PM

line = line.substring(14, len);

not...

line = line.substr(14, len);

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: MonkeeSage
Date: January 28, 2003 02:29PM

Here's the basic idea if I can just figure out how to open files for reading, doh. smiling smiley

--------

function history() {
x = new File("C:\\test.txt");
contents="";
now = new Date();
LocalTime = now.toLocaleString();
fileIsOpen = x.open("r");

if (fileIsOpen) {
hpath = "History file: " + x + "<br>";
while (!x.eof()) {.
line = x.readln();
if (!x.eof())
len = line.length;
line = line.substring(14, len); // Cut off timestamp...
conetents += line + "<br>"; // Add line to contents..
}
location = 'file:///C:/Program Files/K-meleon/'+
'Profiles/default/fzjjgnqc.slt/history.html';
document.writeln(hpath); // Write path of history.txt...
document.writeln(LocalTime); // Write date...
document.write(contents); // Write contents...

if (x.error() != 0)
alert("Error reading history file!");
x.close();
}
}

--------

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: asmpgmr
Date: January 28, 2003 05:21PM

MonkeeSage,

Are you sure it's possible to read local files with Javascript ? Imagine the trouble it would cause if Javascript could read (and worse write) local files ? Visit a site and some evil .js code runs to wreak havoc on your system. Now maybe there's is a way to specify authorization for local file access - try checking http://www.w3.org or http://www.ecma.ch (the official name of Javascript is ECMAScript) for the spec.

It's too bad there isn't a scripting language that comes with win32 like shell scripts or Perl for Linux/Unix or Rexx for OS/2 and PC DOS 7.

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: MonkeeSage
Date: January 28, 2003 06:15PM

asmpgmr:

Well, I've only found one script that actually works and reads files without calling a .class file, and I've been searching for a few hours already if that says anything, hehe. Looks like it is possible though, e.g.,

function Fread(filename) {
var hread = '';
var char;
// netscape.security.PrivilegeManager.enablePrivilege('UniversalFileAccess');
var file = new java.io.File(filename);
var FileReader = new java.io.FileReader(file);
char = FileReader.read();
while (char != -1) {
hread = hread + String.fromCharCode(char);
char = FileReader.read();
}
FileReader.close();
alert(text);
}

I think that you don't need the security clearance unless you are trying to read from a file Netscape (Mozilla) thinks is a system file. Seems to work without it, but I may also have a previous clearance in my memory cache, as I've been playing at it for a little while now.

So now I just have to make a loop to escape on crlf so I can then chop the first 14 chars off the lines...I wish I was good at js, it takes forever to do anything if you aren't, heh. winking smiley


Shelumi`El
Jordan

S.D.G

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: asmpgmr
Date: January 28, 2003 08:46PM

MonkeeSage,

Isn't there a Javascript function to convert the time value to local date/time format ? The numeric value is milliseconds since Jan 1, 1970 (GMT). Note in bookmark.htm the last 3 digits are chopped off making the value seconds.

I agree with you about Javascript being a pain, I'm not an OO programmer nor do I want to be, it's just too different. I expect functions like open and read to take parameters since that's how the underlying OS APIs work.

Here's another thought, I suppose a C program could be written to take the input file and convert it to an appropriate output file with links and that could be called via a macro.

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: asmpgmr
Date: January 28, 2003 09:08PM

Another question, given the history file format %1:%2 where %1 is the time value and %2 is the URL don't you have to write lines out with the format in order to have links:

%1 <a href="%2">%2</a><br>

C statement:
printf("%s <a href=\"%s\">%s</a><br>", localtime(timeval), url, url);

To my knowledge, only mail clients automatically convert URLs to hyperlinks. If there's a way to get the browser to do that then would make the job far easier.

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: MonkeeSage
Date: January 28, 2003 10:01PM

asmpgmr:

LOL...

You read my mind. smiling smiley

I am not OO programmer either, and I'm barely literate in C even...but javascript was not working out for the task... I'm sure there are js gurus on some mountain in asia who could do it easy...but us mere mortals are just aren't up to the task winking smiley

So I booted up Dev-C++ (though I've never even had the urge to try C++...I have a hard enough time with K&R's brainchild, plain old ansi C)... and with a little help from my brother (Evan), this is what we come up with. (Ps. I'm going to give instructions for people to use this program, so don't think I'm trying to explain to you as if you need it or anything).



Source:
======

/* Main.c, builds khistformat.exe */
/* This source is freely distributable, and can be altered, ripped, ignored, or */
/* diskwiped to suite your fancy. Just don't pretend its yours or sell it. smiling smiley */

#include <stdio.h>

FILE *fp, *buf;
int count;
char chr;

char str[1024];

int main(int argc, char *argv[])
{
if (argc != 2) {
printf ("Enter only one file name.");
return -1;
}
fp = fopen (argv[1], "rb");
if (!fp) {
printf ("File access error.");
return -2;
}
buf = fopen("history.html", "wb");
fprintf (buf, "<html><head><title>Global History</title></head><body><center><LABEL><h3><font color=#666666>K-Meleon History</font></h3></LABEL></center><hr>\n");
while (!feof(fp)) {
for (count=0; count < 14; count++) {
chr = fgetc(fp);
}
fscanf (fp, "%s\n", str);
fprintf (buf, "<a href=\"%s\">%s</a><br>\n", str, str);
}
fprintf (buf, "<hr><font size=-1>Khistformat is free software, and if it breaks your computer, you can't say you didn't get you money's worth. End disclaimer type message.<br> This was brought to you by the letter \"b\" the number \"5\" and Evan & Jordan Callicoat.<br>K-Meleon rules.<b></body></html>");
fclose(fp);
fclose(buf);
return 0;
}


Binary:
=====

http://monkeesage.d2g.com/khistformat.exe (13.2kb).


Batch file (modified from Ismo's batch file):
=======

name: sorthist.bat
content:

@echo off
CD "c:\Program Files\K-Meleon\profiles\default\xxxxxxxx.slt\"
sort /+14 history.txt /O history.srt
khistformat history.srt
del history.srt
history.html


Macro:
=====

history {
exec("C:\\Program Files\\K-Meleon\\Profiles\\default\\xxxxxxxx.slt\\sorthist.bat");
}


Instructions:
=========

khistformat.exe takes one parameter, namely your history.srt file that is output from sort; then it chops off the first 14 characters (I'm too lazy to try and convert the time winking smiley, then it encapsulates the remainder of the line into an href tag. smiling smiley

Simple but effective. Also, I have like no clue at all how to use qsort, so I am just using the external windows sort program.

Hope this is helpful. smiling smiley

Ps. Be sure to set "xxxxxxxx.slt" to point you YOUR profile directory in the batch file as well as in the macro, or this will not work at all.


Shelumi`El
Jordan

S.D.G

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: asmpgmr
Date: January 29, 2003 12:36AM

I decided to write a C history file converter program and here's what I came up with:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

main()
{
FILE *infile, *outfile;
char *p, inbuf[1024], numbuf[11], timebuf[24];
time_t t;

numbuf[10]='\0';
infile=fopen("history.txt", "r");
outfile=fopen("history.htm", "w");
if (infile && outfile) {
fprintf(outfile, "<html>\n<title>Global History</title>\n<dl>\n");
do {
fgets(inbuf, 1024, infile);
strncpy(numbuf, inbuf, 10);
t=atol(numbuf);
strftime(timebuf, 24, "%b %d %Y %H:%M:%S", localtime(&t));
p=strchr(inbuf, ':')+1;
fprintf(outfile, "<dt>%s <a href=\"%s\">%s</a>\n", timebuf, p, p);
} while(feof(infile)==0);
fprintf(outfile, "</dl>\n</html>\n");
fclose(infile);
fclose(outfile);
}
}

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: Hugo
Date: January 29, 2003 12:44AM

Dammit guys... Cool, well done! (Though I haven't tested it yet, but I'm sure it's super! :-))))

Question:
--- asmpgmr mentioned somewhere that the history doesn't get flushed to file all the time (which is good I guess..) -- but only after 10 new history items have been added.. Is that still an issue or is it taken care of somewhere, somehow?

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: asmpgmr
Date: January 29, 2003 01:03AM

Hugo,

It's not really an issue since that was likely done to reduce disk I/O. I would guess a true global history plugin would call the Mozilla function to flush the history to disk before processing the file.

Here's the embedded global history source code:
http://lxr.mozilla.org/seamonkey/source/embedding/lite/nsEmbedGlobalHistory.cpp


Does anyone know if there's a way to make the forum (and the bug tracking system) keep leading spaces ?

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: Hugo
Date: January 29, 2003 01:15AM

That source code is a lot to dig in to..
Andrew just filed the
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/kmeleon/k-meleon/history/history.cpp
-link somewhere - the history plugin source. I gotta tell you I
don't understand the architecture here.. What history does
what? (Well, never mind really..I wouldn't be able to do much
about it I guess.)

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: Hugo
Date: January 29, 2003 01:17AM

Does anyone know if there's a way to make the forum (and the bug tracking system) keep leading spaces ?

Should this be filed as a web site RFE? Theres some code publishing going on here, so leading spaces would be much needed.

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: asmpgmr
Date: January 29, 2003 01:27AM

Isn't history.dll the existing URL session plugin ??

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: Andrew
Date: January 29, 2003 01:36AM

Currently that is used for session history. If anyone wants to see if that can be extended to handle global history, feel free to take a whack at it.

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: MonkeeSage
Date: January 29, 2003 01:43AM

asmpgmr:

Nice. I changed your input file to point to history.srt, and then used this batch setup:

========
@echo off
sort /R history.txt /O history.srt
[your binary].exe history.srt
del history.srt
history.htm
========

That way I got a nice date-sorted view from newest to oldest:

Jan 28 2003 16:34:24 http://kmeleonbrowser.org/forum/list.php?f=4
Jan 28 2003 16:34:19 http://kmeleonbrowser.org/forum/list.php?f=2
Jan 28 2003 16:33:24 http://kmeleonbrowser.org/forum/read.php?f=2&i=1573&t=1573
Jan 28 2003 16:27:54 http://kmeleonbrowser.org/forum/read.php?f=3&i=3363&t=3363
Jan 28 2003 16:26:32 http://kmeleonbrowser.org/forum/read.php?f=3&i=3380&t=3380
Jan 28 2003 16:25:49 http://kmeleonbrowser.org/forum/read.php?f=3&i=3222&t=3222
Jan 28 2003 16:25:45 http://kmeleonbrowser.org/forum/list.php?f=3
Jan 28 2003 16:22:09 http://www.bulwer-lytton.com/lyttony.htm
Jan 28 2003 16:21:32 http://kmeleonbrowser.org/forum/read.php?f=1&i=7081&t=7081
&c...

If you want I'll stick a copy of the binary I built from your source on my site and link it here for the K-M users who don't have a compiler? If you do let me know what to call the binary file. smiling smiley

Ps. There was a slight cast hiccup or something on line 21:

Compiler: Default compiler
Building Makefile: "C:\Dev-C++\khistformat\Makefile.win"
Executing make...
make.exe -f "C:\Dev-C++\khistformat\Makefile.win" all
gcc.exe -c main.c -o main.o -I"C:/Dev-C++/include" -fsave-memoized -fexpensive-optimizations -O3

main.c: In function `main':
main.c:21: warning: assignment makes pointer from integer without a cast

But it compiles and runs fine. Strict ANSII compliance is really, really stupid...they went and broke everything to fix things...yeah, that was smart hehe winking smiley That's why I keep a spare gcc 2.96! smiling smiley


Shelumi`El
Jordan

S.D.G

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: asmpgmr
Date: January 29, 2003 01:57AM

MonkeeSage,

Call it convhist.exe. As for the cast warning, yeah C is annoying like that. I'll look into it and post an update tomorrow.

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: MonkeeSage
Date: January 29, 2003 02:29AM

Done.

http://monkeesage.d2g.com/convhist.exe

Also, I get that warning on and off every other make or so, so I think its the ansii compliance in gcc 3 that is the problem, not your code.

Ps. You may want to change (line 20)

strftime(timebuf, 24, "%b %d %Y %H:%M:%S", localtime(&t));

to

strftime(timebuf, 24, "%b %d %H:%M:%S", localtime(&t));

...then you have a bit more room for the urls on the right.


Shelumi`El
Jordan

S.D.G

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: MonkeeSage
Date: January 29, 2003 02:58AM

line 21:

p=(char*)strchr(str, ':')+1;

^ Strict ansii wanted that lol... smiling smiley


Shelumi`El
Jordan

S.D.G

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: MonkeeSage
Date: January 29, 2003 03:01AM

Ps I unabashedly ripped your timestamp function:

========

/* Main.c, builds khistformat.exe */
/* This source is freely distributable, and can be altered, ripped, ignored, or */
/* diskwiped to suite your fancy. Just don't pretend its yours or sell it. smiling smiley */

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

FILE *fp, *buf;
int count;
char tbuf[10], *p, showtime[29];
time_t t;
char str[1024];

int main(int argc, char *argv[])
{
if (argc != 2) {
printf ("Enter only one file name.");
return -1;
}
fp = fopen (argv[1], "rb");
if (!fp) {
printf ("File access error.");
return -2;
}
buf = fopen("history.html", "wb");
fprintf (buf, "<html><head><title>Global History</title></head><body><center><LABEL><h3><font color=#666666>K-Meleon History</font></h3></LABEL></center><hr>\n");
while (!feof(fp)) {
fscanf (fp, "%s\n", str);

/* Code by asmpgmr */
strncpy(tbuf, str, 10);
t=atol(tbuf);
strftime(showtime, 24, "%b %d", localtime(&t));
p=(char*)strchr(str, ':')+1;
/* Code by asmpgmr */

fprintf (buf, "%s <a href=\"%s\">%s</a><br>\n", showtime, p, p);
}
fprintf (buf, "<hr><font size=-1>Khistformat is free software, and if it breaks your computer, you can't say you didn't get you money's worth. End disclaimer type message.<br> This was brought to you by the letter \"b\" the number \"5\" and Evan & Jordan Callicoat.<br></font><font color=#666666 size=-1><b>K-Meleon rules.</b></font></body></html>");
fclose(fp);
fclose(buf);
return 0;
}

========

Shelumi`El
Jordan

S.D.G

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: MonkeeSage
Date: January 29, 2003 03:08AM

This looks nice:

fprintf (buf, "[ %s ] <a href=\"%s\">%s</a><br>\n", showtime, p, p);

smiling smiley


Shelumi`El
Jordan

S.D.G

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: MonkeeSage
Date: January 29, 2003 03:48AM

Place a file histbg.bmp in the same dir with khistformat.exe; this file will be used as the "Background" property of the <body> tag, in the written output, history.html.

e.g.,

http://monkeesage.d2g.com/khistformat.jpg


Shelumi`El
Jordan

S.D.G

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: asmpgmr
Date: January 29, 2003 03:50AM

An update which simplifies things, fixes an eof glitch and the warning (needed string.h for strchr prototype)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

main()
{
FILE *infile, *outfile;
char *p, inbuf[1024], timebuf[22];
time_t t;

infile=fopen("history.txt", "r");
outfile=fopen("history.htm", "w");
if (infile && outfile) {
fprintf(outfile, "<html>\n<title>Global History</title>\n<dl>\n");
while(1) {
fgets(inbuf, 1024, infile);
if (feof(infile)) break;
p=strchr(inbuf, ':')+1;
*(p-4)='\0';
t=atol(inbuf);
strftime(timebuf, 22, "%b %d %Y %H:%M:%S", localtime(&t));
fprintf(outfile, "<dt>%s <a href=\"%s\">%s</a>\n", timebuf, p, p);
}
fprintf(outfile, "</dl>\n</html>\n");
fclose(infile);
fclose(outfile);
}
}

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: MonkeeSage
Date: January 29, 2003 04:18AM

Compiled: No warnings, no errors, upped to:

http://monkeesage.d2g.com/convhist.exe


Shelumi`El
Jordan

S.D.G

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: Nick
Date: January 29, 2003 07:47PM

Excellent. That will do me, I am one of them that if it does the job, it does the job smiling smiley

I have also got my Perl version working also on very similar lines now with a batch file, so I have two options... and I found a perl2exe app that creates a standalone binary from perl script (bloody huge size, though). That works OK if you launch 'alone', but when called up from a macro it cannot find history.txt - so there is something built into it which looks at where it was launched and base's the root location from that (I guess?).

But anyway, you solution is cleaner and better (at the mo winking smiley ).

Thanks!

Nick

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: asmpgmr
Date: January 30, 2003 07:23PM

Another update, it now takes an optional parameter which specifies the directory to look for history.txt in and it will write history.htm there. If not specified then the current directory is used as before. I also added error messages if the files can't be opened.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

main(int argc, char *argv[])
{
FILE *infile, *outfile;
char *p, fname[256], inbuf[1024], timebuf[22];
time_t t;
register int i;

fname[0]='\0';
if (argc>1) {
strcpy(fname, argv[1]);
i=strlen(fname)-1;
if (fname=='\"') fname='\\';
if (fname!='\\') strcat(fname, "\\");
}
strcat(fname, "history.txt");

infile=fopen(fname, "r");
if (infile) {
strcpy(&fname[strlen(fname)-3], "htm");
outfile=fopen(fname, "w");
if (outfile) {
fprintf(outfile, "<html>\n<title>Global History</title>\n<dl>\n");
while(1) {
fgets(inbuf, 1024, infile);
if (feof(infile)) break;
p=strchr(inbuf, ':')+1;
*(p-4)='\0';
t=atol(inbuf);
strftime(timebuf, 22, "%b %d %Y %H:%M:%S", localtime(&t));
fprintf(outfile, "<dt>%s <a href=\"%s\">%s</a>\n", timebuf, p, p);
}
fprintf(outfile, "</dl>\n</html>\n");
fclose(outfile);
}
else printf("Unable to write %s\n", fname);
fclose(infile);
}
else printf("Unable to read %s\n", fname);
}


Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: asmpgmr
Date: January 30, 2003 08:09PM

I'm not sure what the deal with the italics is. Maybe the forum should support some form of the <pre> tag so that preformatted text can be copied in unchanged, leading spaces and all.

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: Nick
Date: January 30, 2003 10:02PM

All the quotes seem to have foobarred it. ''''test'''' single ones break a lot of sites.

As an aside, I once spent hours upon hours debugging a source code perl script (so important, I can't remember what it was smiling smiley ), only to find the author used two single quotes '' as opposed to a 'double' quote " 8-{

Nick

Options: ReplyQuote
Re: Javascript to format and display history.txt...
Posted by: MonkeeSage
Date: January 31, 2003 12:10AM

asmpgmr:

main.c: In function `main':
main.c:17: warning: comparison between pointer and integer
main.c:17: incompatible types in assignment
main.c:18: warning: comparison between pointer and integer


It doesn't like these two lines:

17: if (fname=='\"') fname='\\';
18: if (fname!='\\') strcat(fname, "\\");



Shelumi`El
Jordan

S.D.G

Options: ReplyQuote
Pages: 12Next
Current Page: 1 of 2


K-Meleon forum is powered by Phorum.