Flickr MultiMailer

By steeev Last update Jun 22, 2007 — Installed 3,971 times.
// Flickr MultiMailer Greasemonkey Script
// by Stephen Fernandez aka Steeev    http://steeev.freehostia.com    http://flickr.com/photos/steeev
// version 2.25 last updated 08 Aug 2006

// --------------------------------------------------------------------
//
// This is a Greasemonkey  (0.6.4) user script.
//
// To install, you need FireFox  http://www.mozilla.org/firefox and 
// the firefox extension called Greasemonkey: http://greasemonkey.mozdev.org/
// Install the Greasemonkey extension then restart Firefox and revisit this script.
// Now click the button "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Flickr MultiMailer", and click Uninstall.
//
// --------------------------------------------------------------------


// Usage Instructions: On the compose mail page, click the "Multiple Recipients" link, then you can select multiple 
//                     recipients by holding down the control button and clicking the usernames you want to send to.
//                     You can now save a selection of users as a mailing list by clicking the checkbox before you send a mail.

// ==UserScript==
// @name            Flickr MultiMailer
// @description     Lets you send Flickr Mail to multiple recipients.
// @author          Stephen Fernandez aka steeev http://flickr.com/photos/steeev http://steeev.freehostia.com
// @version         2.25 (08/08/06)
// @namespace       http://steeev.f2o.org/flickr/
// @exclude         http://www.flickr.com/messages_write.gne?to=*
// @exclude         http://flickr.com/messages_write.gne?to=*
// @include         http://www.flickr.com/messages_write.gne
// @include         http://flickr.com/messages_write.gne
// @include         http://www.flickr.com/messages_write.gne#*
// @include         http://flickr.com/messages_write.gne#*
// ==/UserScript==

// Changelog
// ---------
// 17/06/06 - added ability to create mailing lists
// 21/06/06 - some minor code refactoring + fixed font display bug
// 03/08/06 - fixed bug with apostrophes in usernames, message and subject, thanks go to flickr user t_feiler for the tip
// 08/08/06 - added confirmation step for deleting mailing lists, and changed delete button colour

//GM_setValue('tipbeenseen',"no");

unsafeWindow.multimailtoggle=0;
unsafeWindow.addEventListener("load", function() { unsafeWindow.init_script() }, false);//.user.js

unsafeWindow.init_script =function() {

  //alert(document.getElementsByClassName('ThinCase')[0].innerHTML);

  if (document.getElementsByTagName('select')[0]) {
    inpoint=document.getElementsByTagName('select')[0];
    //alert(inpoint.parentNode.parentNode);

  enmulspan=document.createElement('span');
  enmulspan.setAttribute('id','enablemulti');
  enmulspan.innerHTML='<a href="" onclick="toggle_multi();return false;">Enable Multiple Recipients</a><br>';

  inpoint.parentNode.insertBefore(enmulspan, inpoint);

  dforms= document.getElementsByTagName('form');
  for(i=0;i<dforms.length;i++)
    if (dforms[i].getAttribute('action')=='messages_write.gne') {
      daform=dforms[i];
      break;
    }

  mstatus=document.createElement('div');
  mstatus.style.display='none';
  mstatus.setAttribute('id','mailstatus');
  mstatus.setAttribute('class','StartAlert');

  daform.parentNode.insertBefore(mstatus,daform);
  daform.setAttribute('onSubmit','return multimailer(this);')

  imps=document.getElementsByTagName('input');
  for (i=0;i<imps.length;i++)
    if (imps[i].getAttribute('value')=='PREVIEW') {
    prebutt=imps[i];
    break;
    }
  prebuttparent=prebutt.parentNode;

  prebuttspan=document.createElement('span');
  prebuttspan.setAttribute('id','previewbutt');
  prebuttspan.appendChild(prebutt);

  prebuttparent.appendChild(prebuttspan);
  prebuttparent.innerHTML=prebuttparent.innerHTML.replace('							&nbsp;or&nbsp;\n							<input class="Butt" value="SEND THIS" type="submit">','');

  prebuttparent.innerHTML+='&nbsp;&nbsp;&nbsp;<input class="Butt" value="SEND THIS" type="submit">'
  inpoint.parentNode.parentNode.innerHTML=inpoint.parentNode.parentNode.innerHTML.replace('<h3>','').replace('</h3>','');
  }

}

unsafeWindow.repopulategrouplist=function(command) {
  if (command=='old')
    oldgroupidlist=unsafeWindow.readCookie('oldgroupidlist'); // this list always gets saved whether or not the user chooses it
  else {
   bo=unsafeWindow.document.forms[1].selbooger;
   oldgroupidlist=bo.options[bo.selectedIndex].value;
  }
  arGroupList=oldgroupidlist.split(',');
  ob=unsafeWindow.document.forms[1].to_nsid;

  //blank currently selected options
  for (x=0;x<ob.options.length;x++) 
    ob.options[x].selected=false;

  // reselect previously selected options
  for (i=0;i<arGroupList.length;i++) {
    for (x=0;x<ob.options.length;x++){ 
      if(ob.options[x].value==arGroupList[i]) {
         ob.options[x].selected=true;
      }
    }
  }

  return false;
}

unsafeWindow.deletesavedselection =function() {
  thesel=document.getElementById('selboogerid');
  if(thesel.selectedIndex==0) 
    return;
  val2del=thesel.options[thesel.selectedIndex].value;
  txt2del=thesel.options[thesel.selectedIndex].textContent;
  item2del=txt2del + '^'+ val2del;
  //alert(item2del);
  //alert(thesel.selectedIndex);

  if (GM_getValue("savedgroups")) {
    var arrgroups=GM_getValue("savedgroups").split('|');
    //alert(arrgroups);
    //tmp=arrgroups.removeItems(item2del);
    arrgroups.splice(thesel.selectedIndex-1,1);
    //alert(arrgroups);
    GM_setValue("savedgroups",arrgroups.join('|'));
  }
  thesel.options[thesel.selectedIndex]=null;
  alert('Your saved selection "' + txt2del + '" has been deleted');
  return false;
}

unsafeWindow.makesavedgrpsform = function() {

  if (GM_getValue("savedgroups")) {

    var arrgroups=GM_getValue("savedgroups").split('|');
    savgrpsel=document.createElement('select');
    savgrpsel.setAttribute('name','selbooger');
    savgrpsel.setAttribute('id','selboogerid');
    savgrpsel.setAttribute('onchange','repopulategrouplist()');

    theOption=document.createElement("option");
    theText=document.createTextNode('Select a Mailing List');
    theOption.appendChild(theText);
    theOption.setAttribute("value",0);
    savgrpsel.appendChild(theOption); 

    for(i=0;i<arrgroups.length;i++) {
      theOption=document.createElement("option");
      theText=document.createTextNode(arrgroups[i].split('^')[0]);
      theOption.appendChild(theText);
      theOption.setAttribute("value",arrgroups[i].split('^')[1]);
      savgrpsel.appendChild(theOption); 
    }

    dsgbutt=document.createElement('input');
    dsgbutt.setAttribute('type','button');
    dsgbutt.setAttribute('class','CancelButt');
    dsgbutt.setAttribute('value','Delete Mailing List');
    dsgbutt.setAttribute('title','Clicking this will delete the currently selected Saved User Selection');
    dsgbutt.setAttribute('onclick','if (confirm("Are you sure you want to delete this mailing list"))deletesavedselection()');

    if(!document.getElementById('sgspanid')) {
      sgspan=document.createElement('span');
      sgspan.setAttribute('id','sgspanid');
    }

    sgspan.innerHTML='<br/>You may select a previously saved selection of users if you like<br/>';

    sgspan.appendChild(dsgbutt);       
    sgspan.appendChild(savgrpsel);
    sgspan.appendChild(dsgbutt);  

    inpoint=document.getElementById('enablemulti');
    inpoint.parentNode.insertBefore(sgspan, inpoint.nextSibling);

    return false; 
  }
  else
    return false;
}

unsafeWindow.toggle_multi = function () {
  body = document.documentElement;
  if (unsafeWindow.multimailtoggle==0) {
    //enable multiple recipients

    tempmess=document.forms[1].elements.namedItem("message").value;
    tempsubj=document.forms[1].elements.namedItem("subject").value;

    var s = document.getElementsByTagName('select')[0];
    s.size = 10;
    s.multiple = 'multiple';
    var f = document.getElementsByTagName('form');
    f[1].setAttribute('onsubmit', 'return multimailer(this);');
    var m = document.createElement('div');
    m.className = 'StartAlert';
    m.id = 'mailstatus';
    //m.style.display = 'none';
    //f[1].parentNode.insertBefore(m,f[1]);


    if (GM_getValue('tipbeenseen','no')=='no')
      {
      document.getElementById('mailstatus').innerHTML='<B>Tip:</b> You can select multiple recipients by holding down the control key (CTRL) or Shift key and then clicking the usernames you want to send to.<br/><br/><B>N.B</b> The preview button is only available in single recipient mode, if you wish to preview your mail before sending to multiple recipients, write it in single recipient mode then preview it, then change to multiple recipient mode.<br/><br/><A href="" onclick="disablemmtip();return false;">Close Tip</a>';
      document.getElementById('mailstatus').style.display='block';
      }
    document.getElementById('enablemulti').innerHTML='<a href="" onclick="toggle_multi();return false;">Disable Multiple Recipients</a>';
    document.getElementById('previewbutt').style.display='none';
    
    document.forms[1].elements.namedItem("subject").value=tempsubj;
    document.forms[1].elements.namedItem("message").value=tempmess;


    //do init the save selection thang
    chkbxspan=document.createElement('span');
    chkbxspan.setAttribute('id','chkbxspan');

    chksavesel=document.createElement('input');
    chksavesel.setAttribute('name','savegroups');
    chksavesel.setAttribute('type','checkbox');
    chksavesel.setAttribute('id','chksave');
    imps=document.getElementsByTagName('input');
    for(i=0;i<imps.length;i++)
      if(imps[i].getAttribute('value')=="SEND THIS") {
        chkinsertpoint=imps[i];
        break;
      }
    txtlabel=document.createTextNode('Save this user selection as a mailing list ');
    chkbxspan.appendChild(chksavesel);
    chkbxspan.appendChild(txtlabel);

    chkinsertpoint.parentNode.insertBefore(chkbxspan, chkinsertpoint);

    //document.getElementsByTagName('blockquote')[0].getElementsByTagName('p')[1].appendChild(chksavesel);
   
    //document.getElementsByTagName('blockquote')[0].getElementsByTagName('p')[1].appendChild(txtlabel);
    //chkinsertpoint.parentNode.insertBefore(txtlabel, chkinsertpoint);

    unsafeWindow.makesavedgrpsform();
    // end init the save selection thang

    unsafeWindow.multimailtoggle=1;
    }
  else { 

    //disable multiple recipients
    tempmess=document.forms[1].elements.namedItem("message").value;
    tempsubj=document.forms[1].elements.namedItem("subject").value;

    if (document.getElementById('sgspanid'))
      document.getElementById('sgspanid').parentNode.removeChild(document.getElementById('sgspanid'));
    if (document.getElementById('chkbxspan'))
      document.getElementById('chkbxspan').parentNode.removeChild(document.getElementById('chkbxspan'));

    document.getElementById('mailstatus').style.display='none';

    var s = document.getElementsByTagName('select')[0];
    s.size = 1;
    s.multiple = '';

    //blank currently selected options
    ob=unsafeWindow.document.forms[1].to_nsid;
    for (x=0;x<ob.options.length;x++) 
      ob.options[x].selected=false;

    document.getElementById('enablemulti').innerHTML='<a href="" onclick="toggle_multi();return false;">Enable Multiple Recipients</a>';
    document.getElementById('previewbutt').style.display='inline';
    
    document.forms[1].elements.namedItem("subject").value=tempsubj;
    document.forms[1].elements.namedItem("message").value=tempmess;
    unsafeWindow.multimailtoggle=0;
  }

}

unsafeWindow.disablemmtip = function() {
  document.getElementById('mailstatus').style.display="none";
  GM_setValue('tipbeenseen',"yes");
  return false;
}

unsafeWindow.multimailer = function (dform) {
  unsafeWindow.mailstosend=0;
  if (unsafeWindow.multimailtoggle==0) // normal single recipient mail, so return.
    return true; 

  // multi recipient mail
  scroll(0,0);
  //alert(dform.to_nsid.value);
  ob=dform.to_nsid;

  groupidlist='';
  tempnamelist='';
  tempcounter=0;
  for (x=0;x<ob.options.length;x++){ 

    if(ob.options[x].selected==true && ob.options[x].value.match(/@/))
      {
      tempnamelist+=ob.options[x].text + ', ';
      groupidlist+=ob.options[x].value + ',';
      tempcounter++;
      }
  }
  
if ((dform.message.value=='') || (dform.subject.value==''))  {
  alert('You need to enter a subject and a message.');
  return false;
  }

  if (tempcounter==0) {
    alert('You didnt select anyone to send the mail to.');
    return false;
  }
  if ( tempcounter >= 1 )
  if (!confirm('You are about to send a Flickr Mail to the following ' + tempcounter + ' people:\n\n' + tempnamelist + '\n\nAre you sure ?'))
        return false;


  if(dform.savegroups.checked==true) {
    //alert('he wants to save');
    var gselname=prompt('Please enter a name for your mailing list:');
    dform.savegroups.checked=false;
    savegrouplist= gselname + "^" + groupidlist.substring(0,groupidlist.length-1);
    if (GM_getValue("savedgroups")) {
      var arroldsavedgroups=GM_getValue("savedgroups").split('|');
      //alert(arroldsavedgroups.join('|'));
    }
    else
      var arroldsavedgroups = new Array();
    arroldsavedgroups.push(savegrouplist);
    GM_setValue("savedgroups", arroldsavedgroups.join('|'));
    //alert(arroldsavedgroups.join('|'));
    //alert(arroldsavedgroups.join('|').substring(0,arroldsavedgroups.join('|').length-1));
      
    unsafeWindow.makesavedgrpsform();
    //chksavesel.setAttribute('type','checkbox');
    //var foo = [1,2,3];
    //GM_setValue("foo", foo.join(","));
    //var boo = GM_getValue("foo").split(",");
  }


  var arSelected = new Array();
  var nmSelected = new Array();
  while (ob.selectedIndex != -1) { 

    if(ob.options[ob.selectedIndex].value.match(/@/))
      {
      arSelected.push(ob.options[ob.selectedIndex].value); 
      nmSelected.push(ob.options[ob.selectedIndex].text);
      }

    ob.options[ob.selectedIndex].selected = false; 
  }
  //alert(arSelected.toString()); alert(nmSelected.toString()); alert(dform.magic_cookie.value); alert(dform.subject.value); alert(dform.message.value); alert(arSelected.length);
  
  for (i=0;i<arSelected.length;i++) 
    {
    if(arSelected[i].match(/@/))
      setTimeout("sendmail(\'" + arSelected[i] + "\',\'" + dform.magic_cookie.value + "\',\'" + encodeURIComponent(dform.subject.value).replace('\'','\\\'','g') + "\',\'" + encodeURIComponent(dform.message.value).replace('\'','\\\'','g') + "\','" + nmSelected[i].replace('\'','\\\'','g') + "\')",2500*(i+1));
      //alert(arSelected[i] + ' ' +nmSelected[i]);
      //alert("sendmail('" + arSelected[i] + "','" + dform.magic_cookie.value + "','" + dform.subject.value + "','" + dform.message.value + "')",2000*(i+1));
      //sendmail(arSelected[i], dform.magic_cookie.value, dform.subject.value, dform.message.value );
    }
  unsafeWindow.mailstosend=arSelected.length;
  unsafeWindow.mailscounter=0;

  document.getElementById('mailstatus').style.display='block';
  document.getElementById('mailstatus').innerHTML='<h3><font color=green>Sending ' + unsafeWindow.mailstosend + ' Mail/s</font></h3>'; //id="pulser" style="display:inline;!important border:0px"

  return false;
}

 unsafeWindow.sendmail=function(userid, mcookie, subject, message, username) {

   document.getElementById('mailstatus').innerHTML += "<div id='" + userid + "'><font color=blue>" + username + "</font></div>";


// BEGIN new xmlhttp code (was buggier than the gm_xmlhttprequest code anyway)
//alert(location.href.split('/')[2]);
/*
  pdata='magic_cookie=' + mcookie + '&reply=&done=1&to_nsid=' + userid + '&subject=' + subject + '&message=' + message;
  //alert(pdata);
  hostname=location.href.split('/')[2];
  req = false;
  try {
    req = new XMLHttpRequest();
  } 
  catch(e) {
    req = false;
  }
  if(req) {
    req.onreadystatechange = function() {
      if (req.readyState == 4 && req.status == 200) {
	//alert(req.responseText);
        //alert('Request for Atom feed returned ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' + 'Feed data:\n' + lat + ' ' + lon);
        if (req.responseText.match(/Your message has been sent./))
	  document.getElementById(userid).innerHTML = ' <span class="Confirm"><b>Sent</b></span>' + document.getElementById(userid).innerHTML;
	else
	  document.getElementById(userid).innerHTML = ' <span class="Problem"><b>Failed</b></span>' + document.getElementById(userid).innerHTML;
	unsafeWindow.mailscounter++;
        if(unsafeWindow.mailscounter==unsafeWindow.mailstosend)
          document.getElementById('mailstatus').innerHTML += '<br/><font color=red>Message processing complete.</font> <a href="" onclick="location.href=\'http://www.flickr.com/messages_write.gne\'">Compose a new message?</a>';
       }
      }
    req.open("POST",  'http://' + hostname + '/messages_write.gne', true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    req.send(pdata);
  }
*/
///// END new xmlhttp code


// this part was commented out because it didnt work in gm 0.3.5, but back now as gm_* functions are back in gm 0.6.4
   GM_xmlhttpRequest({
      method: 'POST',
      url: 'http://www.flickr.com/messages_write.gne',
      headers: {
        'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
        'Content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/atom+xml,application/xml,text/xml',
        'Referer': 'http://flickr.com/messages_write.gne',
      },
      data: 'magic_cookie=' + mcookie + '&reply=&done=1&to_nsid=' + userid + '&subject=' + subject + '&message=' + message ,
      onload: function(responseDetails) {
	  //alert(responseDetails.responseStatus + ' ' + responseDetails.responseText )
	  if (responseDetails.responseText.match('Your message has been sent.'))
	    document.getElementById(userid).innerHTML = '<span class="Confirm"><b>Sent</b></span>&nbsp;&nbsp;' + document.getElementById(userid).innerHTML;
	  else
	    document.getElementById(userid).innerHTML = '<span class="Problem"><b>Failed</b></span>&nbsp;&nbsp;' + document.getElementById(userid).innerHTML;
	  unsafeWindow.mailscounter++;
          if(unsafeWindow.mailscounter==unsafeWindow.mailstosend)
            document.getElementById('mailstatus').innerHTML += '<br/><font color=red>Message processing complete.</font> <a href="" onclick="location.href=\'http://www.flickr.com/messages_write.gne\'">Compose a new message?</a>';
		
      }
    });
// END replacement httprequest code

return false;
}

document.getElementsByClassName=function() {
  var findings=[], chosen =(document.getElementsByTagName)?document.getElementsByTagName("*"):document.all;
  for(var i=0; i<chosen.length; i++) {
  if(chosen[i].className==arguments[0])
  findings[findings.length]=chosen[i]
  }
  return findings;
}