A MouseHole Example: Showing Alt Text #
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" end 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 end end end
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.
robert
Excellent. Examples-we-like.
robert
Any way to get MouseHole to reload userscripts? I’m trying to tweak and reload, but hitting the cache, like I should.
why
Reloading will be in the next release. You have to restart the whole thing presently.
brian
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)MrCode
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.
nedric
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!
MrCode
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.
<(:{
MrCode: Perhaps if nothing else the name of yours (WonderLand) could be used instead of MouseHole.
MrCode
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.
why
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.
robert
brian: thank you!
why: don’t look at this page in opera on a mac, it will make you cry.
robert
brian: thank you!
why: don’t look at this page in opera on a mac, it will make you cry.
robert
but only once.
Comments are closed for this entry.