0

How to tell if somebody is signed in on AIM

Posted by Darren on Jan 20, 2010 in Uncategorized

Whilst writing an AIM bot, I needed a way to check if a user was online – after reading AOL’s API I discovered this:

http://big.oscar.aol.com/$aim?on_url=true&off_url=false

replace $aim with the aim address you are querying – if they are online you will be taken here:

http://big.oscar.aol.com/true

if not:

http://big.oscar.aol.com/false

 
0

Add a stylesheet on the fly with javascript

Posted by Darren on Jan 20, 2010 in Coding, javascript

You may notice ive been dabbling in javascript more recently. As part of a project I need to insert a stylesheet through javascript, my first attempt lead to me being success; only in firefox. I decided to insert the stylesheet in the body of the HTML by updating the InnerHtml of a div i knew existed. This worked a treat in firefox but notta thing in other (probably right) browsers.

I set about doing it the right way and this is what I came with:
function add_stylesheet(){
if(document.createStyleSheet) {
document.createStyleSheet('http://counter.adcourier.com/share/css/share.css');
} else {
var styles = "@import url('http://counter.adcourier.com/share/css/share.css');";
var newSS=document.createElement('link');
newSS.rel='stylesheet';
newSS.href='data:text/css,'+escape(styles);
document.getElementsByTagName("head")[0].appendChild(newSS);
}
}

Cross browser and exactly what I wanted.

Tags: , , ,

 
0

Automatic bookmark script in javascript

Posted by Darren on Jan 18, 2010 in Coding, javascript

After googling, yahooing and scratching my head – ive now realised that automatic booking in browsers such as Chrome and Safari (including the iphone) simply isn’t possible. However I did find a useful script that takes care of IE, Firefox and Opera – whilst displaying a “helpful” message to others.

function bookmarksite(){
var title = top.document.title;
var url = document.location.href;
if (window.sidebar){ // firefox
window.sidebar.addPanel(title, url, "");
}else if(window.opera && window.print){ // opera
var elem = document.createElement('a');
elem.setAttribute('href',url);
elem.setAttribute('title',title);
elem.setAttribute('rel','sidebar');
elem.click();
}else if(document.all){ // ie
window.external.AddFavorite(url, title);
} else{
alert("Sorry, we do not support automatic bookmarking for your browser at this time.");
}
}

Tags: , , , , , ,

 
1

Create a unique comma separated list from an array.

Posted by Darren on Sep 22, 2009 in Coding, PERL

Hi There,

Some quick PERL today :) – Recently I was working with someone who wanted to produce a comma separated list from an array element – the catch was they only wanted unique values. There attempt was as follows:

my ($val1,$val2,$val3) = @_;
my $return;
$return .= $val1;
$return .= ','.$val2 if ($val2 && ($val2 ne $val1));
$return .= ','.$val3 if ($val3 && ($val3 ne $val1) && ($val3 ne $val2));
return $return;

Okay, so that does the job. Is it pretty? no. Is it efficient? Hell no.

So, heres how I propose it should be done:

my @list = @_;
my %seen = ();
@list = grep { $_ && !$seen{$_}++ }@list;
return join(',',@list);

Beautiful, and only 2 lines of code. :) . If any of you have a better of way of doing this, please feel free to comment below.

 
1

Run PHP from a .html extension

Posted by Darren on Sep 21, 2009 in Coding, PHP

I recently had an odd request, they wanted their site to appear as HTML but run PHP; my first thought was why? but, if thats what they want, thats what they get.

It turned out to be a very simple task. If you open up your .htaccess file and add the following line:

AddType application/x-httpd-php .html

.html will execute as PHP!

Job done. :)

Tags: , ,

 
2

Randomize array whilst keeping the keys in tact..

Posted by Darren on May 8, 2009 in Coding, PHP

I had a problem recently – I needed to randomize an array – of course I could have used the PHP function array_rand however, this overwrote the existing keys with numbers – not good! as I need to use those keys as part of my display – okay, not best practice but i’d set it up this way thus far, why should I change it? Therefore I discovered a function that allowed me to do exactly what I needed, it randomizes the array whilst keeping the existing keys :) !

function array_rand_keys($array, $limit = 1) {
$count = @count($array)-1;

if ($limit == 0 || !is_array($array) || $limit > $count) return array();
if ($count == 1) return $array;

for ($x = 0; $x < $limit; $x++) {
$rand = rand(0, $count);

while (isset($rands[$rand])) $rand = rand(0, $count);

$rands[$rand] = $rand;
}

$return = array();
$curr = current($rands);

while (count($return) != $limit) {
$cur = 0;

foreach ($array as $key => $val) {
if ($cur == $curr) {
$return[$key] = $val;

$curr = next($rands);
continue 2;
} else {
$cur++;
}
}
}

return $return;
}

Tags: , ,