Re: making requests from firefox without creating windows

Micha <[email protected]> Tue, 14 Mar 2006 04:25:02 +0100
Newsgroups gmane.network.wwwoffle.user
Message-ID <20060314042502.48c40bd2@woody>

What i would like to have is a one click button which passes the URL (or any other argument) to an external application, which in my case would be preferably a bash script, so i can handle it freely.

ok, there mozilla 'extensions'...i guess i could find out how to modify one for this puropose, but it would be working with the mozilla API (chrome) only, so that's no option.

More easy seems to be a browser bookmark, that is, a link....

And the question is: How could a link invoke external apps ? Obviously, if any link could invoke any executable wildy, that would be a security issue. So anything seems to be properly locked into the browser/HTML/Java cage. With one remarkable exception: The mailto link ! Well there telnet and irc, theoretically....but that doesn't work with any browser. The mailto link, howver, should work with nearly any browser.

Next we need to get the URL of the page shipped out altogether with the mailto link... 
Can a mailto link include the actual URL dynamically ?
I couldn't find anything without javascript, that's a pity (but if you have an idea...?).
Thst means you have to enable javascript at least for the specific domain.

My idea is to point the users default-mailer-path to a custom script, which acts like a 'proxy: If the mail address contains a specific marker, then it's a pseudo-address and the script will handle the content itself. Else, pass it to the mail program. I'll attach the POC script below.

This is quite a hack, but at least, should work with many browsers, and it can be easily extended.
I added the infrastructure to configure different actions. So, a pseudo-url looks like

mailto:mail2agent@action1@http://blafoo.com

and a javascript bookmarklet looks like
javascript:location.href='mailto:mail2agent@action1@'+location.href

The script is rather bloated, maybe it can all be done in a oneliner too. It's just the way i'm working, i'm always hoping different people will pick it up, (including myself some months later ;) and so i add lots of comments and provide some infrastructure for extension.
So: I included some optional (by now disabled) circumstance using escape(location.href) to replace control chars by %hex, but until someone finds an example where it really is necessary i prefer to make it unsafe, but easy. Also includes some debugging output, can be disabled by setting debug="" in the CONFIG section.

I greatly appreciate your ideas and improvements !


What i don't like (and you won't too) is the dependency on javascript....if i only knew a way
to say to browser 'this is a safe bookmarklet please ignore the site filter if i click on it !'. 
But this is all alpha of course, pehaps there is a way...
For Firefox i recommend NoScript which makes temporary or fix domain enabling very easy.
(It still needs a reload)
 
Other ideas ? What would be in store with submit forms or css 2.x ? 
css would be great, of course, can we handle mailto+dynamic URI with OnClick event handlers or such ?

--
micha


__________________________________________________

#!/bin/sh

# Version: 2006-03-14 alpha

# mail2agent "mailto:<address-string>"
#
# A workaround to pass arguments to a script via one browser click.
# Acts like a proxy for 'mailto' links, which handles pseudo-mail-addresses 
# starting with "mail2agent@" itself, and passes anything else to a real mailer. 
# To be invoked by browsers via HTML or javascript 'mailto' link
# 
# DEPENDS:
# sed (debian: package 'sed')
# cut (debian: package 'coreutils')
# Recommends: xmessage (debian: package 'xbase-clients')
# You can make it #!/bin/dash for POSIX compliance testing. 
#
# SETUP:
# Copy the script into your executable path (like /usr/local/bin) and configure 
# your browser or desktop-environment to use it as default mailto: handler
# Then you configure the real mailer in the CONFIG section below.
#
# INVOCATION EXAMPLES: 
# (1) Invoke action1 via ordinary HTML link: 
# <a href="mailto:mail2agent@action1@http://bla.foo.com">mail2agent</a>
# (2) A javascript bookmarklet which invokes action1:
# javascript:location.href='mailto:mail2agent@action1@'+location.href
# Safer may be to replace control chars by %hex ASCII values with
# javascript:location.href='mailto:mail2agent@action1@'+escape(location.href)
# then you may need to switch the url= lines in the script, too.
# (See action1 example)
#
# UGLYNESS:
# * You have to enable javascript 
# * How important is escape(location.href) ?
# * Don't know if it behaves well with other desktop apps
# * Only linux by now
# * NO WARRANTY WHATSOEVER. May destroy your harddisk :)
#
# COPYRIGHT:
# Released under GNU GPL version 2 or higher.
# Please share this script and join the list of authors!
# [email protected] (initial idea)
# you@improvements (improved the blafoo anything)
#

# CONFIG
mailer="sylpheed-claws --compose " # normal mails go here
debug="yes"	    # empty "" means quiet, "yes" for noisy
feedback="xmessage" # aaplication for debug output ($feedback "message")

# MAIN
raw=`echo "$@" | cut -d":" -f2-` # delete the "mailto:" piece
agent=`echo $raw | cut -d"@" -f1` # first part of the @ address
[ $debug ] && $feedback "raw:$raw"

case $agent in

    mail2agent) # main thing

	action=`echo $raw | cut -d"@" -f2`
	arg=`echo $raw | cut -d"@" -f3-`
	[ $debug ] && $feedback "$agent $action with arg: $arg"

	case $action in
	
	    action1) # Order URL via wwwoffle
		     url=${arg}
	             # If you use escape(location.href) in the browser  
		     # bookmarklet, then revert the %hex escape with: 
		     # url=`echo $arg | sed 's/\%3[aA]\/\//:\/\//1'`
	             [ $debug ] && $feedback "action1: wwwoffle -O $url"
	    	     wwwoffle -O $url
	    ;;
	
	    action2) [ $debug ] && $feedback "action2 with arg:$arg";;
	
	esac
	
	;;

	*) $mailer "$raw" # pass to configured mail programm 

	;;

esac

exit 0

__________________________________________________