[Greasemonkey] link creation from form action?
Johan Sundström
oyasumi at gmail.com
Mon Feb 27 03:35:07 EST 2006
For that kind of thing, these helper methods make for both quite
readable and easily written code (once you are familiar with the power
tool XPath). The code would probably look a little like this
(untested):
var parent_form = 'ancestor:://form[1]';
var links_to_fix = '//a[starts-with(@href,"javascript:submitFiling")]['+
parent_form +']';
foreach( links_to_fix, fix_link );
function fix_link( node )
{
var form = get( parent_form, node );
if( form )
node.href = form.action;
}
function foreach( xpath, cb )
{
var nodes = all( xpath ), i;
for( i=0; i<nodes.length; i++ )
cb( nodes[i], i );
}
function all( xpath, root )
{
var doc = root ? root.evaluate ? root : root.ownerDocument : document;
var got = doc.evaluate( xpath, root||doc, null, 0, null ), next, result = [];
while( next = got.iterateNext() )
result.push( next );
return result;
}
function get( xpath, root )
{
if( typeof xpath == 'object' ) return xpath;
var doc = root ? root.evaluate ? root : root.ownerDocument : document;
var type = XPathResult.FIRST_ORDERED_NODE_TYPE, n = null;
return doc.evaluate( xpath, root||doc, n, type, n ).singleNodeValue;
}
--
/ Johan Sundström, http://ecmanaut.blogspot.com/
More information about the Greasemonkey
mailing list