Use the hookFrontendBeforeOutput hook function to do what you want.
Code:
/*
This hook is called immediately after the output is generated but before
it is displayed. This does not include the preheader portion (which may
already have been sent to the browser at this point depending on
configuration).
*/
public static function hookFrontendBeforeOutput($params)
{
//the style used to render the page
//this parameter is read only
$params['styleid'];
//html from the end of the preheader to the end of the page
//this include the the entire <body> tag
//this parameter is editable
$params['pageHtml'];
}
There are sample products utilizing hooks in the do_not_upload/development directory.