Home
Andy's Blog - Hooked on bookmarklets [entries|archive|friends|userinfo]
Andy

[ website | Portal: Andy ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Hooked on bookmarklets [Jun. 28th, 2009|04:44 pm]
Previous Entry Add to Memories Tell a Friend Next Entry
The idea that you can pass the location from your address bar to a script on the web, and then have that script interact with that page fascinates me in all kinds of ways. I just cobbled together a complementary script to my Pikachoors system so I can grab all the images from a page, list them on a new page, and pick the one I wanted. (Right now it's only keyed to images, but it'd be easy enough to sniff out links and backgrounds.) Bookmarklets are awesome!


<?php

// Error messages
function PikachoorError($msg = null) {
    if ($msg != null) {
        echo('ERROR: ' . $msg);
        exit;
    }
    else {
        PikachoorError('No error message passed.');
    }
}

function FetchPage($url = null) {
    if ($url == null) {
        PikachoorError("No url passed to FetchImage...");
    }
    
    $ch = curl_init($url);
    
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    $html = curl_exec($ch);
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    curl_close($ch);
    
    if ($status == 200) {
        return $html;
    }
    else {
        PikachoorError("Page not found...");
    }
}

function CheckImage($url = null) {
    $ch = curl_init($url);
    
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
    curl_setopt($ch, CURLOPT_HEADER, 1); // get the header
    curl_setopt($ch, CURLOPT_NOBODY, 1); // and *only* get the header 
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    curl_exec($ch);
    
    $type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    if ($status != 200) {
        return false;
    }
    
    if (!preg_match("/image/", $type)) {
        return false;
    }
    
    return true;
}

function AbsImage($img = null, $url = null) {
    $startPos = strpos($url, "://") + 3;
    $uParts = split("/", substr($url, $startPos));
    $iParts = split("/", $img);
    $tParts = array();
    $tcnt = 1;
    $turl = "";
    
    foreach ($iParts as $i) {
        if ($i == "..") {
            $tcnt++;
        }
        else if ($i != ".") {
            $tParts[] = $i;
        }
    }
    
    for($i=0; $i < (count($uParts) - $tcnt); $i++) {
        $turl .= $uParts[$i] . '/';
    }
    
    return $turl . implode('/', $tParts);    
}

// Main
$url = isset($_GET['url']) ? htmlspecialchars(stripslashes($_GET['url'])) : null;
$x = array();
$y = array();

if ($url == null)
{
    PikachoorError("No url...");
}

preg_match_all('/src="(.*)"/', FetchPage($url), $x);

foreach ($x[1] as $z)
{
    if (strpos($z, '"') != false) {
        $z = substr($z, 0, strpos($z, '"'));
    }
    
    if (substr($z, 0, 4) != "http") {
        $z = AbsImage($z, $url);
    }
    
    if (CheckImage($z)) {
        $y[] = $z;
    }
}


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
    <title>Pikachoors in a page</title>
</head>
<body>
    <?php foreach($y as $image) { ?>
    <p>
        <img src="<?php echo($image); ?>" alt="<?php echo($image); ?>">
        <br>
        <a href="http://pics.mytrapster.com/wp-pikachoor-post.php?url=<?php echo($image); ?>">Add This Image</a>
    </p>
    <?php } ?>
</body>
</html>

LinkReply

Comments:
[User Picture]From: [info]outlawpoet
2009-06-28 10:15 pm (UTC)

(Link)

bookmarklets are totally awesome. It's the only reason why I post anything to tumblr at all.
[User Picture]From: [info]jarodrussell
2009-06-28 10:33 pm (UTC)

(Link)

It's really game changing when the browser stops being a document viewer, and starts being an editable interface.

And that reminds me. Now that I've got Flickr, Photobucket, and Pikachoor feeds, I should setup a Tumblr account to glom it all together.