View Full Version : Logic problem with loop.

12-26-2006, 06:16 PM
I'm attempting to create a userscript that finds 'mailto:' anchors, disables the 'mailto:' activity, and instead (when clicked) alerts the user with a description of the contents of the anchor's href. I use Gmail, and find it very annoying to have outlook or hotmail pop up when all I want to see is the address that I should email.

I've got my function mostly completed, but my loop doesn't work properly. It makes each 'mailto:' anchor show the information of the last anchor processed. You can see this if you test the function here (http://www.ianr.unl.edu/internet/mailto.html) or here (http://www.outfront.net/tutorials_02/adv_tech/mailto.htm).

I've had a problem like this before, and I'm thinking that I am just not seeing something that is fairly obvious. Could someone tell me why this is not working as expected?


// ==UserScript==
// @name mailto2alert
// @namespace http://1212jtraceur.memebot.com/
// @description Changes 'mailto:EMAIL_ADDRESS' anchors to 'alert(EMAIL_ADDRESS)' anchors. This is useful for those who would like email addresses displayed to them, so that they may use a non-default mail application, such as Gmail.
// @include *
// ==/UserScript==

(function ()
// A series of regular expressions describing strings found in 'mailto:' anchors, followed by the strings they should be replaced with.
var mailto = /mailto:/i;
var mailtoText = 'To: ';
var cc = /(\?|&)cc=/i;
var ccText = '\n' + 'CC: ';
var bcc = /(\?|&)bcc=/i;
var bccText = '\n' + 'BCC: ';
var subject = /(\?|&)subject=/i;
var subjectText = '\n' + 'Subject: ';
var body = /(\?|&)body/i;
var bodyText = '\n' + 'Body: ';

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

var max = anchors.length;
for (var i = 0; i < max; ++i)
var anchor = anchors[i];
var href = anchor.href;

// If anchor is a 'mailto:' anchor.
if (href.indexOf('mailto:') === 0)
// Constructs alertText, complete with newlines and identifiers.
var alertText = href.replace(mailto, mailtoText);
alertText = alertText.replace(cc, ccText);
alertText = alertText.replace(bcc, bccText);
alertText = alertText.replace(subject, subjectText);
alertText = alertText.replace(body, bodyText);
// When the anchor is clicked, shows alertText.
anchor.addEventListener('click', function(){alert(alertText);}, true);

// Scrolls the reloaded page to the clicked link
if (anchor.id.length > 0)
anchor.id = 'mailto2alert' + i;
anchor.href = '#' + anchor.id;
}) ();

12-26-2006, 07:08 PM

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<title>Any Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">

var nAddr = [];

function init(){

var n = 0;
var nLink = document.getElementsByTagName('a');
for (i=0; i<nLink.length; i++)
if (/^mailto/.test(nLink[i].href))
nAddr[nAddr.length] = nLink[i].href.replace(/^mailto:/,"").replace(/\?/,"|").replace(/\&/,"|");
nLink[i].href = nLink[i].href.replace(nLink[i].href,"#");
nLink[i].id = "m"+n++;
nLink[i].onclick = function()
var nIndex = this.id.replace("m","");
return false;


<style type="text/css">

body {background-color:#eae3c6;margin-top:60px}

<a href="http://www.google.com"> Link </a>
<a href="mailto:someone@one.com?cc=someoneelse@one.com&bcc=andsomeoneelse2@one.com">Send mail</a>
<a href="mailto:someone@two.com?cc=someoneelse@two.com">Send mail</a>
<a href="mailto:someone@three.com">Send mail</a>
<a href="http://www.google.com"> Link </a>

12-27-2006, 04:05 AM
Thanks, that looks like it will work much better! I say 'looks like' because Greasemonkey is having a problem with it, but that's for another thread...