The Sapience Society The advice of a self-proclaimed technological ninjician

9Jun/100

Generate Javascript Bookmarklets Using A Simple PHP Function

I wrote a pretty simple PHP function for converting Javascript into a bookmarklet. As you'll see, it's nothing too crazy. I did come up with a kind of interesting way of shortening the source using base 64. I don't know if I'm the first person to do this, but I came up with the idea on my own (no plagiarism!). Unfortunately, it's not IE compatible.. so there's a switch to turn that feature off (if you want).

Here's the PHP function:

function Bookmarkletize($source, $ieCompatible = false){
	$source = preg_replace('~[\t\r\n]~', '', $source);
	$source1 = 'javascript:'.rawurlencode($source);
	if($ieCompatible) return $source1;
	$source2 = 'javascript:eval(atob(\''.base64_encode($source).'\'))';
	if(strlen($source1) < strlen($source2))
		return $source1;
	else
		return $source2;
}

As an example, you can try "Bookmarkletizing" the source for the form hijacker I previously posted about. Here's what that would look like:

$source = <<<source
var index=prompt('Form index?', '0');
if(confirm('The action url is:\\n'+document.forms[index].action.substr(0,100)+'\\n\\nDoes that look right?')){
	document.forms[index].action='https://lococobra.com/showreq.php';
	document.forms[index].submit();
}
source;
echo Bookmarkletize($source);

(syntax might look funny, it's just because the PHP highlighting wordpress plugin I'm using doesn't support Heredoc string quoting)

Hope this is useful for someone!

Comments (0) Trackbacks (1)

    Leave a comment

    (required)