13 May 2009 - 23:37How to configure Firefox to view feeds in Google Reader

When you select “Google” as your default RSS/Atom feed reader, it will ask you every time whether you want to use Reader or iGoogle. To skip this prompt and use Google Reader every time, there are a few simple things you need to do.

Go into your about:config (type it into the address bar) and filter for “fusion.google.com

Edit the line with the value http://fusion.google.com/add?feedurl=%s (the name should be something like browser.contentHandlers.types.2.uri) – change the value to http://www.google.com/reader/view/feed/%s

Restart Firefox and you should be good to go.  Now Firefox will open RSS and Atom feeds in Google Reader, without asking you if you want to use iGoogle. If you want to test to see if it worked, maybe you should try to subscribe to my blog, hmmm?

You can still add feeds to your iGoogle page by clicking Add Stuff -> Add Feed or Gadget.

No Comments | Tags: web

15 April 2008 - 22:42[How to] round corners on images with JavaScript

Something I’ve come to love about the JQuery JavaScript library is that, no matter what client-side scripting challenges arise these days, my first thought is “I bet there’s an easy way to do this with JQuery!”. I recently worked on a website where the client wanted rounded corners on a lot of the site’s elements, including images. A quick Google search found this jquery.corner plugin; which was, as most JQuery plugins are, extremely simple to implement and adapt for my own project.

The corner plugin has a bunch of ways to add corner effects to div elements, but unfortunately you can’t apply it directly to an image. Back to Google, I found a way to make it work with images: wrap it in a div and apply the corner code to the div element. This is good, but not best for our purposes. We still have to apply styles to make the divs match the size of their child image elements.

Objective:
To loop through all divs of a certain class, and automatically resize them to match their child image elements.

We’ll put this code inside our $(document).ready(function(){ … }) block so it runs as soon as the document is ready to run code.

We want to apply this code to all divs of a certain class, we’ll say class=”round-corners” in this example. To loop through every div of this class and run code on it, we use the wonderful JQuery.each function.

First, select images with a parent div element of this class (“round-corners”):
$(‘div.round-corners > img’)
and run the JQuery.each function to loop through:
$(‘div.round-corners > img’).each(function(id){ … })

Within this loop we want to select the image, and set the height and width of its parent div element. An easy way to do this is with the .height and .width functions. These functions are both accessors and modifiers, as is the case with many functions in JQuery.
Select the parent element of the selected image:
$(this).parent(‘div.round-corners’)
get the height of the image we’ve selected:
$(this).height()
so the whole line to set the height will look like this:
$(this).parent(‘div.round-corners’).height( $(this).height() );
Repeat for the width property with the .width function.

That should do it. Let’s take a look at the source for the whole page:

<script type="text/javascript" src="jquery.js"></script><script type="text/javascript" src="jquery.corner.js"></script><script type="text/javascript">$(document).ready(function(){ $('div.round-corners > img').each(function(id){     $(this).parent('div.round-corners').height( $(this).height() ).width( $(this).width() ); })$('.round-corners').corner();})</script><div class="round-corners"><img src="P3040004.JPG" alt="P3040004.JPG" /></div>

<div style="height: 130px; background-color: rgb(255, 0, 0);" class="round-corners">This is just a regular div with no image inside, but it will have round corners too!</div>

<div class="round-corners"><img src="bluehills.jpg" alt="bluehills.jpg" /></div>

An idea for improving this script would be to use the .wrap function to automatically wrap the images in divs

Have fun rounding corners, and please use them responsibly.

2 Comments | Tags: code, how to, web

24 February 2008 - 22:26[How to] use PHP to make form programming faster and easier

Programming forms is fairly easy, but it can certainly be one of the most tedious things to program. Anyone who’s programmed something like an application form with a decent amount of fields will tell you that it takes forever to make all of the form elements, and add the necessary code to pre-populate with values or pre-check radio buttons etc. (This is a good practice when programming forms, so a user doesn’t have to fill the form out again if it’s submitted with errors.) Whether you’re smart or just lazy, you’ll definitely want to make a simple PHP function to expedite this otherwise-boring process. Enter: the formElement() function.

/** * form elements */   function formElement($type, $name, $value = '',$extra = '')   {       global $_POST;       if ($type=='text') {           $val = (!empty($_POST[$name])) ? $_POST[$name] : $value ;           $o = '<input type="text" name="'.$name.'" value="'.$val.'"'.$extra.' />';       } elseif ($type=='radio') {           $checked = ($_POST[$name]==$value) ? 'checked="checked"' : '';           $o = '<input type="radio" name="'.$name.'" value="'.$value.'" '.$checked.' '.$extra.' />';       } elseif ($type=='checkbox') {           $checked = ($_POST[$name]==$value) ? 'checked="checked"' : '';           $o = '<input type="checkbox" name="'.$name.'" value="'.$value.'" '.$checked.' '.$extra.' />';       }

       return $o;   }

It’s pretty straight-forward, but I’ll break it down parameter-by-parameter.

$type – in my example, this is the same as the type attribute in the html input tag. You could expand the function to include textareas too, or whatever you need.

$name – same as the name attribute.

$value – For checkbox and radio types, it uses this value to check against the item in the $_POST array, and adds checked=”checked” where appropriate. For text types, it pre-populates the textbox with this value if the form hasn’t been submitted yet.

$extra – I use this for any other attributes you may need to add to your form elements. Things such as CSS inline styles, event handlers like onclick, and id or class attributes could go here.

So with our new formElement function, this;

   <input type="radio" name="fav_food" value="pizza" <?=($_POST['fav_food'] == 'pizza') ? 'checked="checked"' : '';?> /> Pizza   <input type="radio" name="fav_food" value="souvlaki" <?=($_POST['fav_food'] == 'souvlaki') ? 'checked="checked"' : '';?> /> Souvlaki

becomes this:

   <?=formElement('radio','fav_food','pizza');?> Pizza   <?=formElement('radio','fav_food','souvlaki');?> Souvlaki

1 Comment | Tags: code, how to, web

16 December 2007 - 0:24The Snowman on Youtube

Winter hasn’t even started yet, and we’ve already had a couple pretty good snow storms here in Nanaimo. Everyone’s got their fingers crossed for a white Christmas this year, so it’s a good time to watch Raymond Briggs’ 1982 animated classic, “The Snowman”. You can view the film’s entire 27-minute length on Youtube. The video must have been uploaded before Youtube established their 10-minute limit on videos. Whether you’re up for a bit of cheery nostalgia, or if you’re seeing it for the first time, or if you just like watching full movies in abhorrent Youtube quality, The Snowman is worth a watch at least once a year. Also available on Youtube is David Bowie’s introduction, which I guess can be found on certain editions of the film.

No Comments | Tags: web

2 December 2007 - 20:36Was GameSpot editor Jeff Gerstmann fired for objectivity?

This has been an interesting weekend for GameSpot readers, video game reviewers and geekier advocates of ethical behavior. Following rumors that GameSpot’s Jeff Gerstmann had been fired for his 6.0/10 review of Kane & Lynch, comments sections on video game blogs all over the web have exploded with support for Jeff and criticism for CNet and GameSpot. Representatives for CNet GameSpot of course haven’t shed any light on the situation, but the rumors as I understand them are as follows:

Gerstmann’s less-than-flattering review of Kane & Lynch: Dead Men came at a bad time, as the GameSpot site was running an Eidos-sponsored K&L promotion. Somebody with money pressured somebody who wanted money, and Jeff was let go. Actually, Friday’s Penny-Arcade sums it up pretty well.

Kotaku quoted an “anonymous tipster” as saying, “As writers of what is supposed to be objective content, this is our worst nightmare coming to life,”. If the rumors are true, this has horrible implications for the rest of the CNet empire, which includes download.com, mp3.com and tv.com, which I’ve never actually been to.. but I bet they get a lot of hits with a domain like that.

I’ve listened to The HotSpot, GameSpot’s weekly podcast, religiously since I first heard it a little over a year ago. Much more than just co-hosts or coworkers, the HotSpot guys legitimately seem like they’re all good friends with each other. It should be really interesting to hear what they have to say about Gerstmann’s departure on Tuesday’s show. Best case scenario; they all walk off the job in support for Jeff. More likely, they’ll downplay the whole thing, or at least set the record straight.

No Comments | Tags: web

17 October 2007 - 18:54[How to] force "www." on your website

I recently added some lines to my website’s .htaccess that force a “www.” on the front of my domain. If you try to go to my website by typing just dmack.ca, you’ll notice it automatically redirects you to www.dmack.ca.

It’s best practice to either always force the www. or always force no www., but not use both. There are a bunch of reasons to do this, and they vary depending on what kind of website you run:

  • Cookies. If visitors can access your site with and without the “www.” (two different URLS as far as the cookies are concerned), it could cause problems.
  • Likewise with $_SERVER['REQUEST_URI'] and such things if you’re running scripts.
  • The most cited reason I see for this is that it hurts your pagerank. Apparently you could get penalized for duplicate content.

In the interest of avoiding the above problems, and any sort of minor confusion, here’s how to set up a painless automatic redirect in your .htaccess file.
I’ll use my own domain in this example. Just add the following lines:

# redirect dmack.ca to www.dmack.caRewriteCond %{HTTP_HOST} ^dmack.ca [NC]RewriteRule ^(.*)$ http://www.dmack.ca/$1 [L,R=301]

The first line is a rewrite conditional directive that basically ensures the URL starts with “dmack.ca”. The [NC] means it’s not case sensitive. The second line is an ordinary rewrite directive that matches everything and does a 301 redirect to the properly formatted URL (complete with “http://www.”). This matches the url if it begins with dmack.ca, but it could just as easily match any url that isn’t www.dmack.ca (think: exclamation mark negates… !^www.dmack.ca)

1 Comment | Tags: how to, web

2 October 2007 - 19:27rsizr: Content-aware photo resizing hits the web

You might recall having your socks knocked off by a tech demo of a program that could do what it called “content aware resizing” a few months ago.

rsizr.com has a flash application that lets you try it for yourself. The possibilities for resizing landscape photographs are endless, as are the possibilities for hilarity on celebrity portraits.

1 Comment | Tags: web

22 July 2007 - 3:54[How to] create a simple web template using PHP’s output buffer

The PHP output buffer is exactly what it sounds like: it allows us to capture output and, instead of printing it to the screen immediately, set it as a variable and deal with it later on. The “output” could include echoed lines and ordinary markup outside of php tags. For example: if you include a file, you can set all the variables and run all the functions inside it, but save the output and display it in different parts of your template, like the title, meta tags, content etc. In this tutorial, I’ll explain how to use the PHP output buffer to implement very simple template system for your website.

This guide assumes you’ve already got a nice-looking template with something like an empty div for your page content. I’m using /index.php as my template file, and i’ll pass it the same of the page to display as a GET variable.

The template index.php file should look a bit like this:

[/index.php]

<?php//get the page to display, or use home.php if nothing is passed.$page = (!empty($_REQUEST['page'])) ? $_REQUEST['page'] . '.php' : 'home.php' ; //turn on output bufferingob_start(); //get the contents of the file (/pages/home.php etc.).//do this outside of php tags because the file most likely has its own php tags, as well as ordinary html in it.eval('?> ' . file_get_contents($_SERVER['DOCUMENT_ROOT'] .'/pages/'.$page) . '<?php '); //return the contents of the output buffer, and set it as a variable.$page_content = ob_get_contents(); //turn off output buffering, and clear the output buffer so we can do it again just as easily.ob_end_clean(); ?><!DOCTYPE [...]<title><?=$page_title;?></title><meta name="description" content="<?=$meta_description;?>" /><meta name="keywords" content="<?=$meta_keywords;?>" />[...]<div id="page-content"> <?=$page_content;?></div>[...]

The variables $page_title , $meta_description , and $meta_keywords are defined in the page content file ‘/pages/whatever.php’.

A page content file would look like this:

[/pages/home.php]

<?php//define meta and title tags$page_title = 'This is the page title!';$meta_description = 'This is the meta description';$meta_keywords = 'meta keywords, hot dogs, rap music';?><h1>Welcome</h1><p>This is a test of my simple template system.</p><p>I sure hope it works!</p><p><?phpecho 'I can echo things this way too...';?></p>

Then we’d access this page with the uri /index.php?page=home and the script would start the output buffer, then get the contents of home.php and set the variables, but not echo anything until you tell it to. You can do this as many times as you want, if you wanted to include a menu from an external file as well, for example.

2 Comments | Tags: code, how to, web

20 May 2007 - 20:29Forum Youtube Linker makes youtube links more useful

I wrote a little script that generates bbcode for linking to Youtube videos. It was inspired by the way GTalk and Orkut take Youtube URLs and show a screenshot and description etc.

It spits out forum bbcode that’ll look like this:

Yoji Biomehanika clip @ Sensation Black 2004

Youtube thumbnailYoutube thumbnailYoutube thumbnail
Yoji drops a sick track @ sensation black 2004Dark By Design – Blackout (yoji biomehanika mix)
http://www.youtube.com/watch?v=YfIPt5Ai5P0

It was basically a way for me to teach myself regular expressions in php. A few of my friends have already found it useful, so give it a shot if you post on any bbcode-enabled forums.

You can also set up a bookmarklet and point it to javascript:(window.location = ‘http://portfolio.itas.ca/~mackenzied/uchoob.php?url=’+location.href);
or drag this into your bookmarks/toolbar: Link to this Youtube video

2 Comments | Tags: code, video, web

23 April 2007 - 20:08I finally found a ‘linkify’ bookmarklet!

Updated Dec. 2008, Looks like moving from Blogger to Wordpress broke this post quite a bit :)

I’ve been looking for a bookmarklet that would turn plain-text URLs into clickable links for a while, because it got tedious opening big blocks of plain-text links in Google Reader or in torrent trackers. I had it on my to-do list to write one, but I happened upon one that had already been written at squarefree.com’s bookmarklets site. ..I just wouldn’t have guessed the name.

To make this worthwhile, here are my 3 favorite bookmarklets:

  1. Linkify – Turns plain text URLs into clickable links. Try it out here if you want: http://www.dmack.ca
  2. Hide visited links – makes visited links invisible (in real time, even). Try them on the above url once you make it a link.
  3. Remove redirects – a good way to get around a lot of interstitial ads. Removes redirect scripts from urls in this format: http://www.example.com/out.php?url=http://www.example.org/

Post your favorite bookmarklets in the comments section!

3 Comments | Tags: tech, web