Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Cool code for your home node

by BigLug (Chaplain)
on Sep 26, 2002 at 04:19 UTC ( #200812=monkdiscuss: print w/ replies, xml ) Need Help??

In my wanderings about the monastery I've discovered some cool goings-on on people's home nodes. The following is a collection of a few such things as well as some enhancements I've made. I've tried to give credit where credit is due, but as some of these things appear on many home nodes I've probably got it wrong. If you want to claim 'I was first' leave a note for others to read below.

1. Other stuff in your Bio
Beatnik's home node shows several extra pieces of info in his bio. Normally your bio just shows a few things that are standard on everyone's home node (Experience, Level, Location etc.) However, as the location is free-text it is possible to insert further fields into your bio.

Add this to your location, you can put more than one, just repeat the code:

</td></tr><tr><td>Field Name:</td><td>Field Content
2. The Button
I first ran into the button when someone clicked the button on Beatnik's home node. I don't want to spoil it for you other than to say once you've clicked it read the chatterbox.
<form method="POST"> <input type="hidden" name="op" value="message"> <input type="hidden" name="node_id" value="5993"> <input type="hidden" name="message" value="Your Message"> <input type="submit" value="Button Title"> </form>
Note: It is possible to add the button to your bio in the manner described above and as done on my home node.

3. The Javascript Warning
Petruchio has this piece of javascript on his home node as a warning. This javascript reads your perlmonks cookie and warns that is could be sent to another site for hacking your password.

<img src="http://www.brinzer.net/png/border.png" onload="if (document.cookie.length < 1) { return } alert( 'This is why you should not run around with JavaScript' + 'enabled. Your cookie says: ' + document.cookie + ' This information could have been quietly sent to a remote server, +' + ' and your account stolen.');">
3a. The Javascript Greeting
When you visit smitz' home node you'll get a lovely little greeting and a warning about javascript. He takes the cookie (as shown above) and extracts your username from it. He then says hello to you.
<script language="javascript"> <!-- var cookie = document.cookie; var userstart = cookie.indexOf('userpass', 0) + 9; var userend = cookie.indexOf('%257C', userstart); if( userend > userstart ) { var username = cookie.substring(userstart, userend); // For some reason, the RegExps don't work, // so we resort to the good old style. :) while( username.indexOf('%2520') > -1 ) { var index = username.indexOf('%2520'); username = username.substring(0, index) + ' ' + username.subst +r(index + 5, username.length); } document.write( '' + '<hr /><p align="center">' + '<table border="1" cellpadding="6">' + '<tr><td>' + 'Hello <i>' + '<a href="/?node=' + username + '">' + username + '</a></i>!<br />' + 'Javascript bad, you know? ' + 'Maybe you should turn it off,' + ' lest I dunk your cookies, bub.' + '</td></tr></table>' + '</p>' ); } //--> </script>
3b. The Home Node Logbook
After seeing the above greeting it occurred to me that I could use it to keep a log of the people who visit my home node. This uses two pieces of code: firstly the javascript to snarf the username and then a (remotely hosted) perl script to log the username with the time.

The javascript:

<script language="javascript"> <!-- var cookie = document.cookie; var userstart = cookie.indexOf('userpass', 0) + 9; var userend = cookie.indexOf('%257C', userstart); if( userend > userstart ) { var username = cookie.substring(userstart, userend); while( username.indexOf('%2520') > -1 ) { var index = username.indexOf('%2520'); username = username.substring(0, index) + ' ' + username.subst +r(index + 5, username.length); } document.write("<script src='http://www.isite.net.au/perlmonks/per +lmonkvisitor.pl?visitor="+username+"'></script>"); document.write("Hello "+username+", welcome to my homenode. Your visit + has been logged. If you'd like to see my log you can see it <a href= +'http://www.isite.net.au/perlmonks/perlmonkvisitor.pl'>here</a>.") } //--> </script>
The perl code. This code will not log you and will print your log if called without a visitor parameter:
#!/usr/bin/perl -w $visitorfile = "/path/to/visitors.txt"; use CGI; $query = new CGI; # If the visitor parameter exists then we assume its a visit from the +home node: if (my $visitor = $query->param('visitor')) { # Don't log ourselves: unless ($visitor =~ /your_username/i ) { open (VISITORS,">>$visitorfile") || die("Perlmonk Visitors cou +ldn't open $visitorfile: $!"); print VISITORS scalar(localtime) . "\t" . $visitor . "\t" . $E +NV{HTTP_REFERER} . "\n"; close VISITORS; } print "Content-Type: text/javascript\n\n"; # Output our javascript: print qq|document.write("Welcome and ")|; } else { open (VISITORS,"$visitorfile") || die("Perlmonk Visitors couldn't +open $visitorfile: $!"); @data = <VISITORS>; close VISITORS; print "Content-Type: text/html\n\n"; print "<h1>Visitors to my Homenode</h1>\n"; print "<table border=1 cellspacing=0><tr><th>Time/Date</th><th>Use +rname</th></tr>\n"; foreach $line(@data) { my ($date, $user) = split(/\t/,$line); print "<tr><td>$date</td><td>$user</td></tr>\n"; } print "</table>"; }
4. The Messager
Once again from Petruchio's home node comes this non-javascript code to allow visitors to send you a message when they visit your homenode. This harmless piece of code just submits a '/msg' to the chatterbox.
<form method="post"> <input type="hidden" name="op" value="message"> <input type="hidden" name="message" value="/msg Petruchio Hi, I just visited your page."> <center> <input type="submit" name="message_send" value="Hello"> </center> </FORM>
4a. The Custom Messager
This is an adaptation of the above. It uses javascript to allow the user to create their own custom message.
<form method="post" name='f' onSubmit="f.message.value = f.message.val +ue + prompt('Enter your message:'); return true;"> <input type="hidden" name="op" value="message"> <input type="hidden" name="message" value="/msg Username "> <center> <input type="submit" name="message_send" value="Hello"> </center> </form>

Edit kudra, 2002-09-26 Added readmore tag

Comment on Cool code for your home node
Select or Download Code
Re: Cool code for your home node
by Chady (Priest) on Sep 26, 2002 at 07:09 UTC

    not to forget OeufMayo's cool javascript that changes HTML elements' contents... visit his homenode with js turned on, and check his experience and level.


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
Re: Cool code for your home node
by moxliukas (Curate) on Sep 26, 2002 at 11:33 UTC
    There are also CSS additions to homenodes. Have a look at mr2 or even my homenode ;)
Re: Cool code for your home node
by ignatz (Vicar) on Sep 26, 2002 at 14:12 UTC
    Be forwarned that using JavaScript on your homenodes can create bizarre results for some users.

    Neat post.

    ()-()
     \"/
      `                                                     
    
Re: Cool code for your home node
by Lexicon (Chaplain) on Sep 26, 2002 at 18:21 UTC
    Don't forget the secret upvoter:
    <DIV ALIGN=CENTER> <FORM METHOD="POST" ACTION="/index.pl" ENCTYPE="application/x-www-form-urlencoded"> <INPUT TYPE=SUBMIT VALUE='Give me some Love!'> <INPUT TYPE=HIDDEN NAME='op' VALUE='vote'> <INPUT TYPE=HIDDEN NAME='vote__51278' VALUE=1> <!-- <SCRIPT> var WorstNodes = new Array(51278, 37646, 38577, 38578, 38763, 48607, 43640, 50885, 55 +631, 58105); var idx = Math.floor(Math.random(WorstNodes.length)); document.write("<INPUT TYPE=HIDDEN NAME='vote__" + WorstNodes(idx) + " +' VALUE=1>"); </SCRIPT> --> </FORM></DIV>
    It produces a button on your page (mine says "Give me some love!"). If clicked, it upvotes your (well, currently my) worst rated node. The part in scripts will trade out between your 10 worst rated nodes, so if you get one fool silly enough to press it repeatedly, you can take more of their votes. I originally wrote it almost two years ago as a sneaky trick to get rid of a couple negative rep nodes. ;)

    And, not to show off, but I made a hit counter a couple years ago as well. That's the broken image in my home node. It's an image because I decided to learn ImageMagick at the same time, so I produced a binary counter. The calling code was:

    <DIV ALIGN=CENTER><SCRIPT> d=document; c=d.cookie; if (c=="userpass") {n="Anonymous Monk";} else{n=c.substring(9,c.indexOf('%'));} d.write("<img src='http://code.scouras.net:81/count/image.cgi?L=perlmo +nks&U="+n+"'>"); </SCRIPT> <NOSCRIPT> <img src='http://code.scouras.net:81/count/image.cgi?L=perlmonks&U=NOJ +S'> </NOSCRIPT></DIV><P>
    You can see a sample image here: http://code.scouras.net:81/pmc/output.gif I believe this is the final code here:
    use CGI; use CGI::Carp qw(fatalsToBrowser); #use warnings; use strict; my $CountFile = "PMCounter.txt"; my $query = new CGI; my $User = $query->param('U') || 'unknown'; my $Loc = $query->param('L') || 'unknown'; if ($User eq 'unknown') { my %cookie = $query->cookie('PerlMonks'); if ($cookie{'UserName'}) { $User = $cookie{'UserName'} } } my $cookie = $query->cookie( -name => 'PerlMonks', -value => { UserName => $User }, -expires => '+10y', -domain => '.anapraxis.net', ) or die "Could not build cookie. $!"; my $header = $query->header( -type=>'image/gif', -nph=>1, -expires=>'now', -cookie=>$cookie, ) or die "Could not send header. $!"; print $header; my ($C_Total, $C_Comb, $C_Loc, $C_User) = Get_Count ($User, $Loc, $Cou +ntFile); my $Image = Get_Counter_Image($C_Total, $C_Comb, $C_Loc, $C_User); print $Image; #################################################################### # GET COUNT #################################################################### sub Get_Count { my $User = shift; my $Loc = shift; my $File = shift; my $C_Total = 0; my $C_Comb = 0; my $C_Loc = 0; my $C_User = 0; my $Write = ""; open COUNT, "$File" or die "Could not open $File. $!"; while (my $line = <COUNT>) { if ( $line =~ /^\s+(\d+)\s+Total Hits\s+([\w\d\: ]+?) \#\s+[\w\d\ +: ]+$/ ) { $C_Total = $1 + 1; $Write .= sprintf ( "%8d %42s %20s # %s\n", $1 + 1, 'Total Hi +ts', $2, "" . localtime()); } elsif ( $line =~ /^\s+(\d+)\s+$User\s+$Loc\s+([\w\d\: ]+?) \#\s ++[\w\d\: ]+$/ ) { $C_Comb = $1 + 1; $Write .= sprintf ( "%8d %20s %20s %20s # %s\n", $1 + 1, $Us +er, $Loc, $2, "" . localtime()); } elsif ( $line =~ /^\s+(\d+)\s+Location\s+$Loc\s+([\w\d\: ]+?) \ +#\s+[\w\d\: ]+$/ ) { $C_Loc = $1 + 1; $Write .= sprintf ( "%8d Location %32s %20s # %s\n", $1 + 1, + $Loc, $2, "" . localtime()); } elsif ( $line =~ /^\s+(\d+)\s+User\s+$User\s+([\w\d\: ]+?) \#\s ++[\w\d\: ]+$/ ) { $C_User = $1 + 1; $Write .= sprintf ( "%8d User %36s %20s # %s\n", $1 + 1, $Us +er, $2, "" . localtime()); } else { $Write .= $line } } if ($C_Total == 0) { $Write .= sprintf ( "%8d %42s %20s # %s\n", $1 + 1, 'Total Hi +ts', localtime(), "" . localtime()); $C_Comb = 1; } if ($C_Comb == 0) { $Write .= sprintf ("%8d %20s %20s %20s # %s\n", 1, $User, $L +oc, "" . localtime(), "" . localtime()); $C_Comb = 1; } if ($C_Loc == 0) { $Write .= sprintf ("%8d Location %32s %20s # %s\n", 1, "$Loc +", "" . localtime(), "" . localtime()); $C_Loc = 1; } if ($C_User == 0) { $Write .= sprintf ("%8d User %36s %20s # %s\n", 1, "$User", +"" . localtime(), "" . localtime()); $C_User = 1; } close COUNT or die "Count not close $File. $!"; open COUNT, ">$File" or die "Could not open $File. $!"; print COUNT $Write or die "Could not print to $File. $!"; close COUNT or die "Could not close $File. $!"; return ($C_Total, $C_Comb, $C_Loc, $C_User); } #################################################################### # GET COUNTER IMAGE #################################################################### sub Get_Counter_Image { my $C_Total = shift; # Number of hits my $C_Comb = shift; my $C_Loc = shift; my $C_User = shift; my @BitArray = (); my $Image = ''; for (1..32) { if ($C_User & 1) { unshift @BitArray, '1.gif' } else { unshift @BitArray, '0.gif' } $C_User >>= 1; } for (1..32) { if ($C_Loc & 1) { unshift @BitArray, '1.gif' } else { unshift @BitArray, '0.gif' } $C_Loc >>= 1; } for (1..32) { if ($C_Comb & 1) { unshift @BitArray, '1.gif' } else { unshift @BitArray, '0.gif' } $C_Comb >>= 1; } for (1..32) { if ($C_Total & 1) { unshift @BitArray, '1.gif' } else { unshift @BitArray, '0.gif' } $C_Total >>= 1; } my @montage = ( 'montage', 'background', 'white', 'fill', 'white', '-mode', 'unframe', '+display', '-monocrome', '-tile', '128x1', '-geometry', '1x2+0+0!', @BitArray, 'output.gif'); system @montage; { open IMAGE, 'output.gif' or die "Couldn't open output.gif. $!"; binmode IMAGE; local $/; $Image = <IMAGE>; close IMAGE or die "Couldn't close output.gif. $!"; } return $Image; }

Re: Cool code for your home node
by b10m (Vicar) on Dec 02, 2003 at 11:06 UTC
    In order to prove that JavaScript is evil, I had the following code on my node (which raised some complaints, so I took it down)
    <!-- // This is the B10m "JavaScript is evil" trap ... // Yes, these are pieces snatched from multiple sources and // glued together by me ;) // // A cookie will be set, to ensure that we don't fill up te // Chatterbox with just these silly messages. And to ensure // people won't fear to enter my home node again ... // // Thanx to JSchmitz for the idea of posting to the CB ;) // --> <form name="b10m_trap" method="post" action="/index.pl" enctype="appli +cation/x-www-form-urlencoded"> <input type="hidden" name="node_id" value="295259" /> <input type="hidden" name="op" value="message" /> <input type="hidden" name="message" value="/me declares: 'JavaScript i +s the work of Satan!'" /> </form> <script language="JavaScript"> var b10m = document.cookie; function getCookie(name) { var index = b10m.indexOf(name + "="); if (index == -1) return null; index = b10m.indexOf("=", index) + 1; var endstr = b10m.indexOf(";", index); if (endstr == -1) endstr = b10m.length; return unescape(b10m.substring(index, endstr)) } if (!getCookie("trapped")) { var today = new Date(); var expire = new Date(); expire.setTime(today.getTime() + 3600000*24*365); document.cookie = "trapped=you_becha" + ";expires="+expire.toGMTStr +ing(); document.b10m_trap.submit(); } </script>
    Updated: Spelling can be hard ... changed a few words to correct spelling.
    --
    B10m

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: monkdiscuss [id://200812]
Approved by gmax
Front-paged by gmax
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (14)
As of 2014-09-17 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (74 votes), past polls