Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Oct 2006
    Posts
    206
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Logic problem with loop.

    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 or here.

    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?

    Thanks,
    1212jtraceur

    PHP Code:
    // ==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 
    0max; ++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(mailtomailtoText);
                
    alertText alertText.replace(ccccText);
                
    alertText alertText.replace(bccbccText);
                
    alertText alertText.replace(subjectsubjectText);
                
    alertText alertText.replace(bodybodyText);
                
    // 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;
            }
        }
    }) (); 
    Last edited by 1212jtraceur; 12-26-2006 at 05:17 PM. Reason: corrected code

  • #2
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    1212jtraceur:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <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","");
    					 alert(nAddr[nIndex].replace(/\|/g,"\n"));
    					 return false;
    					}
    				}
    			}
    	}	
    
    	onload=init;
    	
    </script>
    <style type="text/css">
    
    	 body {background-color:#eae3c6;margin-top:60px}
    	
    </style>
    </head>
    	<body>
    		<a href="http://www.google.com"> Link </a>
    		<br>
    		<a href="mailto:someone@one.com?cc=someoneelse@one.com&bcc=andsomeoneelse2@one.com">Send mail</a>
    		<br>
    		<a href="mailto:someone@two.com?cc=someoneelse@two.com">Send mail</a>
    		<br>
    		<a href="mailto:someone@three.com">Send mail</a>
    		<br>
    		<a href="http://www.google.com"> Link </a>
    	</body>
    </html>
    Last edited by Ancora; 12-26-2006 at 06:12 PM.

  • #3
    Regular Coder
    Join Date
    Oct 2006
    Posts
    206
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •