hoodwink.d enhanced


A MouseHole Example: Showing Alt Text #

by why in inspect

I figured some of you might want to tinker with a simple MouseHole user script, so I translated one of the most basic GreaseMonkey strips: comicalt.user.js by Adam Vandenberg, which reveals the alt text for a comic strip right below the strip.

The MouseHole equivalent (userScripts/comicalt.user.rb) looks like so:

 MouseHole.script do
   # declaration
   name "Comics Alt Text" 
   namespace "http://adamv.com/greases/" 
   description 'Shows the "hover text" for some comics on the page'
   include_match %r!^http://.*achewood\.com/!
   include_match %r!^http://.*qwantz\.com/!
   version "0.2" 

   # instance variables
   @comics = {
     'achewood' => '//img[starts-with(@src, "/comic.php?date=")]',
     'qwantz'   => '//img[starts-with(@src, "/comics/")]'

   # the pages flow through here
   rewrite do |req, res|
     whichSite = case req.request_uri.host
                 when /achewood/: "achewood" 
                 when /qwantz/: "qwantz" 
     return unless whichSite

     comic = document.elements[@comics[whichSite]]
     return unless comic

     if comic.attributes['title']
       div = Element.new 'div'
       div.attributes['className'] = 'msg'
       div.text = "(#{ comic.attributes['title'] })" 
       comic.parent.insert_after comic, div

In all, very similiar to Greasemonkey. The declaration has been moved into the code. The include and exclude directives have been replaced with include_match and exclude_match, which both take regexps.

The start method receives WEBrick’s HTTPRequest and HTTPResponse objects. You can use these to read headers or to add headers.

The HTML itself is parsed by MouseHole and stuck in the user script’s @document instance variable. Modify the document with REXML and the altered document will be delivered back to the browser when you’re done. REXML is mixed into MouseHole, so you can refer to Element and Document without the module name.

Updated! For MouseHole version 1.1. More user script examples available on the wiki at UserScripts.
said on 30 Aug 2005 at 08:11

Excellent. Examples-we-like.

said on 30 Aug 2005 at 10:20

Any way to get MouseHole to reload userscripts? I’m trying to tweak and reload, but hitting the cache, like I should.

said on 30 Aug 2005 at 10:28

Reloading will be in the next release. You have to restart the whole thing presently.

said on 30 Aug 2005 at 11:24

Ugly hack to check mtimes of user scripts and reload (re-eval) them if they’ve changed. I’m using it for a quick code/test cycle. Looking forward to a true Ruby hacker’s solution.

Diff follows inline…

Compare: (<) mouseHole (3586 bytes) with: (>) mouseHole.orig (3361 bytes)

<     @script_mtime = {}
<         Dir['userScripts/*.user.rb'].each do |userb|
<         @script_mtime[userb] = File.stat( userb ).mtime
>         Dir['userScripts/*.user.rb'].each do |userb|
<         if File.stat(path).mtime > @script_mtime[path]
<           @script_mtime[path] = File.stat(path).mtime
<           script = eval(File.read(path))
<           @user_scripts[path] = script
<         end

said on 30 Aug 2005 at 12:07

brian: More or less that is what Wonderland does, though my scripts are different to those in MouseHole. I don’t think there is any way that is better.

You know _why, I’m not sure I can compete with you on this. Clearly a quick release with basic functionality is better than some slower but more full-featured release. I should have learned my lesson with the original RubyGems (which I was also reluctant to release too early.)

I will still show Wonderland, but I don’t think it is any more innovative than MouseHole. In fact I’m starting to dislike the design.

said on 30 Aug 2005 at 13:56

MrCode: I think it’s safe to say that your effort inspired _why’s. That’s how a community works. There’s always room for more implementations of an idea!

said on 30 Aug 2005 at 14:08

nedric: Well to be honest I got the idea from here, and _why’s original Hoodwink’d implementation really impressed me in how cool it is to “hack the web.” I was just the first out of the gate in trying to implement a Ruby Greasemonkey, but clearly I wasn’t the first to the finish line.

Hopefully there will be something that other people can learn from Wonderland, and maybe like RubyGems people will just use the name but not my code. At least I’m good at coming up with project names. Maybe I’ll stop coding and go into marketing.

said on 30 Aug 2005 at 14:35

MrCode: Perhaps if nothing else the name of yours (WonderLand) could be used instead of MouseHole.

said on 30 Aug 2005 at 14:51

Don’t worry, Wonderland will be released and MouseHole will seem rodent-infested in comparison (which is what _why wants anyhow, I think.)

Most of my frustration is with my own code and slow-progress, not _why’s (very effective) attempt at motivation.

said on 30 Aug 2005 at 15:07

MrCode: that’s the spirit! I will be quite thrilled should Ryan trump this. I’m going to continue to introduce features, but I’m sure they could be easily patched into Wonderland or vice versa. The world is flexible and we can do what we want.

said on 31 Aug 2005 at 05:01

brian: thank you!

why: don’t look at this page in opera on a mac, it will make you cry.

said on 31 Aug 2005 at 05:01

brian: thank you!

why: don’t look at this page in opera on a mac, it will make you cry.

said on 31 Aug 2005 at 05:02

but only once.

Comments are closed for this entry.