# Make anchors visible, letting you link to or bookmark a section of a page. # # Note: this file must be named ending .user.rb to be recognised by mousehole. # # References: # XPATH: http://www.zvon.org/xxl/XPathTutorial/General/examples.html # REXML tutorial: http://www.germane-software.com/software/rexml/docs/tutorial.html # REXML::Element: http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/Element.html # # Author: neillzero (abstractplain.net) # Based on Mailto Envelope by Christopher T. Osborn (chrisoverzero@gmail.com) # http://users.wpi.edu/~tomato/mailto_envelope.user.rb # and Squarefree.com "named anchors" bookmarklet # http://www.squarefree.com/bookmarklets/webdevel.html#named_anchors # # Previous boilerplate: # Based on code from "Mailto Envelope Script" # by Jason Bailey (gamingfox.blogspot.com) # MouseHole conversion (c) Christopher T. Osborn (chrisoverzero@gmail.com) # The above notice must appear in some form within any distributed version # of this script, modified or otherwise. #the text we'll eventually use as the script's description. desc=<js bookmarklet. Just for learning.
Once installed, try it on the REXML tutorial. END MouseHole.script do # declaration name "Make Anchors Visible" namespace "abstractplain.net" description desc include_match %r{^http://} include_match %r{^https://} version "0.1" rewrite do |req, res| #for each 'a' tag anywhere in the document, with a 'name' attribute... document.each_element('//a[@name]') do |anchor| name = anchor.attributes['name'] #insert a link to the anchor, placing it just after the anchor. #make the a tag span=Element.new 'a' span.attributes['href']="#"+name span.attributes['class']="visible-anchor" span.attributes['style']= "border:1px solid" span.text="#"+name #insert it anchor.parent.insert_after anchor, span end end end