I’m trying to write a script in Greasemonkey that will generate link’s in a frame, but with my limited Javascript knowledge I don’t really know how to do this.
Example of subject:
<html>
<head>
<frameset border="0" frameborder="no" framespacing="0" cols="*,280" rows="*">
<frameset border="0" frameborder="NO" framespacing="0" cols="*" rows="*,200">
<frameset border="0" frameborder="NO" framespacing="0" cols="*" rows="75,*">
<frame scrolling="NO" name="bannerFrame" src="banner.php">
<frame scrolling="auto" name="mainFrame" src="main.php">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<body class="framemainbg" marginwidth="0" marginheight="0" topmargin="0" leftmargin="0">
<table class="areadescription" cellspacing="0" cellpadding="0" border="0">
<br>
<table>
<tbody>
<tr>
<td>
<p class="personlistcaption">Text:</p>
<p class="listusersrow">
<table>
<tbody>
<tr>
<td valign="top">
<td valign="top">
<b>Text </b>
Text -
<a href="fight.php?action=attacknpcmenu&checkid=1347789191&act_npc_id=764">Attack</a>
-
<a class="fastattack" onclick="this.href += '&yscroll=' + window.pageYOffset;" href="fight.php?action=attacknpc&checkid=8409099&act_npc_id=764">Quickattack</a>
<br>
Text
</td>
</tr>
</tbody>
</table>
</p>
</td>
</tr>
</tbody>
</table>
<br>
<table>
<form name="formular">
</body>
</html>
</frame>
</frameset>
<frameset border="0" frameborder="NO" framespacing="0" cols="*" rows="*,0">
</frameset>
<frameset border="0" frameborder="NO" framespacing="0" cols="*" rows="*,360">
</frameset>
<noframes><body> Text </body></noframes>
</html>
Example of desired Output :
<html>
<head>
<frameset border="0" frameborder="no" framespacing="0" cols="*,280" rows="*">
<frameset border="0" frameborder="NO" framespacing="0" cols="*" rows="*,200">
<frameset border="0" frameborder="NO" framespacing="0" cols="*" rows="75,*">
<frame scrolling="NO" name="bannerFrame" src="banner.php">
<frame scrolling="auto" name="mainFrame" src="main.php">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<body class="framemainbg" marginwidth="0" marginheight="0" topmargin="0" leftmargin="0">
<table class="areadescription" cellspacing="0" cellpadding="0" border="0">
<br>
<table>
<tbody>
<tr>
<td>
<p class="personlistcaption">Text:</p>
<p class="listusersrow">
<table>
<tbody>
<tr>
<td valign="top">
<td valign="top">
<b>Text </b>
Text -
<a href="fight.php?action=attacknpcmenu&checkid=1347789191&act_npc_id=764">Attack</a>
-
<a class="fastattack" onclick="this.href += '&yscroll=' + window.pageYOffset;" href="fight.php?action=attacknpc&checkid=8409099act_npc_id=764">Quickattack</a>
-
<a href="fight.php?action=slapnpc&checkid=8409099&act_npc_id=764&mark=0">Hit</a>
-
<a href="fight.php?action=chasenpc&checkid=8409099&act_npc_id=764&">Chase</a>
<br>
Text
</td>
</tr>
</tbody>
</table>
</p>
</td>
</tr>
</tbody>
</table>
<br>
<table>
<form name="formular">
</body>
</html>
</frame>
</frameset>
<frameset border="0" frameborder="NO" framespacing="0" cols="*" rows="*,0">
</frameset>
<frameset border="0" frameborder="NO" framespacing="0" cols="*" rows="*,360">
</frameset>
<noframes><body> Text </body></noframes>
</html>
This Links should be generated:
-
<a href="fight.php?action=slapnpc&checkid=8409099&act_npc_id=764&mark=0">Hit</a>
-
<a href="fight.php?action=chasenpc&checkid=8409099&act_npc_id=764&">Chase</a>
The ‘checkid=…’ and the ‘npc_id=…’ must be the same value as in this link:
<a class="fastattack" onclick="this.href += '&yscroll=' + window.pageYOffset;" href="fight.php?action=attacknpc&checkid=8409099act_npc_id=764">Quickattack</a>
OK, this is really just basic HTML DOM manipulation, there’s nothing GreaseMonkey-specific here.
First, I’m going to assume that the link you want to copy the URL parameters from is the only one with
class="fastattack", since that makes finding it easy:Next, we need to generate the first new link:
…make it point to the URL we want:
…and give it the link text we want:
Next, we insert the new link into the DOM just after the original link, like this:
Oops, we forgot to insert the delimiter first! No worries, we can still do that:
Now we can just do the same for the other link:
This time, I remembered to insert the delimiter first. I used the same
delimnode as above, but I made a copy of it because I wanted to insert another identical delimiter, not move the original delimiter to a new position in the DOM.Finally, we need to close the
ifblock, and that’s it:(Disclaimer: I have not actually tested the code above. I think it should work, but there might be bugs or typos that I’ve missed.)
Edit: Changed the code to insert the new links just after the original, rather than at the end of the parent paragraph.
Addendum:
If you have multiple links with
class="fastattack"in the document, and wish to apply the code above to each of them, you can do that by replacing the first two lines above with a loop over all the links, instead of just the first one: