Copying text using Javascript URLs and TinyURL.com: cloud clipboarding!

The company where I’m currently doing a project has two separate networks: one small network that is used by the developers for a new application and
another network, used by the rest of the company, where the application is deployed to.

Today I wanted to copy a small piece of SQL from my PC to the server in the network. Because using a USB memory stick takes too much time (insert the stick, copy the text into a text file, store the file on the stick, etc.) I thought: apart from using webmail, can’t I store the text somewhere on a site and download it from there, preferrably using a small URL?

The first site that comes to mind is tinyurl.com. But I would then have to create a specific page first and shorten the URL afterwards, wouldn’t I?

Good news: tinyurl.com supports javascript URLs! So I simply created a new tinyurl.com link to:

javascript:f();function f(){document.write('<pre>the text that needs to be copied goes here</pre>');}

and opened up http://preview.tinyurl.com/ in a browser on the server and there you go! Cloud clipboarding!

 

UPDATE: some more Google-ing on the matter revealed two sites that do just this (but easier 😉 which are: http://cl1p.net and http://www.pastebin.com/ Once again this proves that anything you think up already exists somewhere else.

Advertisements

Handy Perl DateTime modules

I was working on a Perl script today and encountered the following two great DateTime format modules.
http://search.cpan.org/dist/DateTime-Format-Baby/
http://search.cpan.org/dist/DateTime-Format-Bork/
Good to see in the Changelog that someone even found a (fixed) bug in the Bork module. 🙂

Yearly WTF – the most terrible code I’ve seen in months

Today I came across the most terrible piece of code I’ve seen in months. Years. One would suspect the original programmer deliberately intended to cause chaos and despair.

It starts off with some Hungarian notation to throw you off-balance; check out the “strBetaald” variable which seems to be a string but is in fact a boolean:

string strValue = "";
string strCheck = "";
string strPaid = "";
string strProcess = "";
bool strBetaald = false;

Then the code proceeds to loop through XML nodes, setting the variable “strValue” to a certain value if the XML node name is X, and setting it to another value if the node name is Y. At the end of the run, strValue contains… what value? Well, doh, that depends on whether we came across X or Y first! The variable probably isn’t that important, since it just gets logged in a financial application database in approximately 50 lines down or so. Hey, how about a small coffee break, anyone?

In the middle of the loop we find the following gold nugget:

if (strCheck != "true".ToString())
{
    strCheck = strCheck;
}
else
{
    strCheck = strCheck;
    // some other stuff..
}
bool bet = Convert.ToBoolean(strBetaald);
bool chk = Convert.ToBoolean(strCheck);

So here we’re converting the string “true” to yet another string (just to be on the safe side) and to be really sure about getting the correct value, we assign the variable to itself.

Finally, we’re converting the boolean strBetaald to boolean, to be absolutely completely 100% positively certain that we’re right about this whole true/false thing because somewhere we read that there’s this weird thing called fuzzy logic and it may be lurking inside the compiler somewhere.

It all does make sense, so why am I so worked up about this?

McAfee heeft nu ook h4ck0rz op de marketing afdeling?

In een reclame e-mailtje (nee, geen spam want ik heb blijkbaar ooit iets aangevinkt) las ik de volgende zinsnede:

Nieuw! De ingebouwde hulpmiddelen voor het op peil houden van de computerprestatiez   (…)”

Ik snap dat een bedrijf als McAfee hackers nodig heeft om zinnige dingen te doen. Maar pas op dat dat soort gasten niet je marketing afdeling gaan pwn’en!

jQuery 1.3.1 released

jQuery - write less, do more

About a week ago, jQuery 1.3 was released, quickly followed by bugfix release 1.3.1.

In my opinion this is the best Javascript library out there. It is feature-rich but does not overindulge itself with unnecessary stuff, intelligently leaving that to plug-ins. To top it all off, they’ve managed to squeeze unsurpassed performance out of it. Truly amazing! (I wonder what IE8’s native Selectors API will do to the performance.)

Flowing text around an image in HTML

Flowing text around an imageYesterday I wrote a little script (that uses the excellent jQuery library) to flow text around an image in HTML in an unobtrusive way. 

The script dynamically inserts outline DIV elements across an image in the page and then pushes the image into the background. The text then flows around the (invisible) outline DIV elements. The dimensions of the outlines are stored in an XML file in the same directory as its corresponding image to keep things simple. 

I started a Google Code project called flowtext-lib. You can download the archive there. It contains a README file that explains the workings in a more detailed way and also an example HTML file with an image that has text flowing around it.

It was a technique I thought of a couple of years ago but never needed it… well, until yesterday, that is.

Calculating Dutch weeknumbers

Calculating Dutch weeknumbers (or in Google-friendly Dutch: Nederlands weeknummer berekenen) is a bit more complex than you’d expect. Luckily there’s a Wikipedia article describing how to do that. I transformed that algorithm into a bit of Javascript. You’re free to use it, if something’s wrong with it please drop me a note.

/***

Calculate Dutch week numbers
Adapted from Wikipedia algorithm (see http://nl.wikipedia.org/wiki/Weeknummer)
Free for use.

Author: mark AT vantzet DOT com

Syntax: getWeekNumber(Date object)

***/
function getWeekNumber(d) {
// Calculates Dutch weeknumber
// See http://nl.wikipedia.org/wiki/Weeknummer

function getDayDiff(d1, d2) {
// Transform d1 and d2 to milliseconds since epoch, subtract,
// and calculate back to whole days (rounding down)
return Math.floor((d1.valueOf() – d2.valueOf()) / 1000 / 60 / 60 / 24);
}
function createDate(year, month, date) {
// Create a new date w/ time set at 00:00:00.000.
// Use UTC functions to prevent timezone problems.
var d = new Date();
d.setUTCMonth(month – 1);
d.setUTCDate(date);
d.setUTCFullYear(year);
d.setUTCHours(0);
d.setUTCMinutes(0);
d.setUTCSeconds(0);
d.setUTCMilliseconds(0);
return d;
}
function getNEN2772WeekDay(d) {
// In calculations, NEN2772 weekdays are used (mon=1,tue=2,..,sun=7)
var n = d.getUTCDay();
return (n > 0 ? n : 7);
}

var nCurrentYear = d.getUTCFullYear();
var dNormalizedDate = createDate(nCurrentYear, d.getUTCMonth() + 1, d.getUTCDate());
var dJanuary4thCurrentYear = createDate(nCurrentYear, 1, 4);
var dJanuary4thPreviousYear = createDate(nCurrentYear – 1, 1, 4);
var dDecember31stPreviousYear = createDate(nCurrentYear – 1, 12, 31);
var dJanuary4thNextYear = createDate(nCurrentYear + 1, 1, 4);

var UK01 = getDayDiff(dNormalizedDate, dJanuary4thCurrentYear);
var UK02 = Math.floor(UK01 / 7);
var UK03 = getNEN2772WeekDay(dNormalizedDate);
var UK04 = getNEN2772WeekDay(dJanuary4thPreviousYear);
var UK05 = getDayDiff(dDecember31stPreviousYear, dJanuary4thPreviousYear);
var UK06 = Math.floor(UK05 / 7);
var UK07 = getNEN2772WeekDay(dDecember31stPreviousYear);
var UK08 = getNEN2772WeekDay(dJanuary4thCurrentYear);
var UK09 = getNEN2772WeekDay(dJanuary4thNextYear);
if (UK07 < UK08 ) { UK10 = 1; } else if (UK07 < UK04) { UK10 = UK06 + 2; } else { UK10 = UK06 + 1; } if (d.getUTCMonth() == 0 && d.getUTCDate() < 4) { if (UK03 < UK08 ) { UK11 = 1; } else { UK11 = UK10; } } else if (d.getUTCMonth() == 11 && d.getUTCDate() >= 29 && UK03 < UK09) { UK11 = 1; } else if (UK03 < UK08 ) { UK11 = UK02 + 2; } else { UK11 = UK02 + 1; } /* Debug. For 1999/8/11 the following checksum should yield true: s = UK01+','+UK02+','+UK03+','+UK04+','+UK05+','+UK06+','+UK07+','+UK08+','+UK09+','+UK10+','+UK11; alert(s == '219,31,3,7,361,51,4,1,2,53,32' ? 'Checksum passed' : 'Checksum failed'); */ return UK11; } [/sourcecode]