Terry Terry - 1 year ago 71
HTML Question

Greasemonkey script that converts all instances of a specific filtered selection of HREF values to another value

Since Firefox cannot open links in a new tab if they have a HREF tag divert to a function, perhaps Greasemonkey could be used to run a script to convert them; i.e. to the actual HREF.

I can see what foo does;

function foo(x) { self.location.href = 'page.asp?ID=' + x }

Is anyone able to provide a Greasemonkey user script that would change all instances of;

<a href="javascript:foo(12354)">


<a href="page.asp?ID=12354">

where 12354 is the variable and the only HREF tags that need to be modified are the ones that call out to the function foo ?

something like;

var anchors = document.querySelectorAll('a');

for(var i=0;i<anchors.length;i++)
if(anchors[i].href includes 'foo' then
val1 = get the value within the parenthesis; i.e. 12354
anchors[i].href = "page.asp?ID=" + val1


Answer Source

Enumerate the links that have foo text in href attribute, extract the id and assign a new value:

[].forEach.call(document.querySelectorAll('a[href^="javascript:foo"]'), function(a) {
    var id = a.href.match(/\d+/)[0];
    a.setAttribute('href', 'page.asp?ID=' + id);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download