Joeviocoe / ALERT_MAIL

// ==UserScript==
// @name       ALERT_MAIL
// @namespace  
// @version    0.1.0
// @description  Send Alert messages when new Webmail detected
// @include      https://web-xxxx.xxx.xxx*
// @include      https://accounts.google.com/ServiceLogin*
// @include      https://mail.google.com/mail/u/0/x*
// @include      https://www.gmail.com/intl/en/mail/help/about.html
// @exclude       
// @copyright  2014+, Joeviocoe
// @require    http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js
// @grant      GM_setValue
// @grant      GM_getValue
// @grant      GM_openInTab
// ==/UserScript==
//
//
/*jslint browser: true*/
/*global $, jQuery, alert*/
//
//
///// Declare Variables /////
var username = "sendingemailaddr@gmail.com";
var password = "sendingemailpassword";
var recipient = "receivingemailaddr@gmail.com";
var unread;
var ele;
var body;
var attach;
var importance;
//
//
///// Webmail /////
//
if (window.location.href.indexOf('https://web-xxxx.xxx.xxx') >= 0) {
   if ( $('#spnFldrNm[fldrnm=Inbox]:eq(0)').next('#spnUC').children('#spnCV').length > 0 ) {
     unread = $('#spnFldrNm[fldrnm=Inbox]:eq(0)').next('#spnUC').children('#spnCV').text();
     } else {
     unread = 0;
   }
   newmsg = unread;
   //
   function timer() {
     newmsg = $('#spnFldrNm[fldrnm=Inbox]:eq(0)').next('#spnUC').children('#spnCV').text();
     if ( newmsg > unread ) {
       //unsafeWindow.console.log( "NEWMSG = " + newmsg );
       //unsafeWindow.console.log( $(".cData").parent(".ur:eq(0)").text() );
       body = "";
       for (var i = 0; i < newmsg; i++) {
         ele = $(".cData").parent(".ur:eq("+ i +")");
         attach = ""; importance = "";
         if ( ele.children(".c3").children(".atch").length > 0 ) {
           attach = "[Attachment]\n"; }
         if ( ele.children(".c3").children(".impMg").length > 0 ) {
           importance = "[Important]\n"; }
         body = body + ele.next("#sr.r2").children(".c3:eq(0)").text() + "\n-- " +
         ele.text() + "\n-- " +
         ele.next("#sr.r2").children(".c2:eq(0)").text() + "\n" + 
         attach + importance + "\n\n";
       }
       newMsgAlert();
     }
     if ( newmsg != unread ) { unread = newmsg; }
   }
   function newMsgAlert() {
     GM_setValue('alert_body', body);
     GM_openInTab('https://mail.google.com/mail/u/0/x/?f=1');
     //alert(body);
   }
   window.setInterval(timer, 10000);
}
//
//
///// GMAIL /////
//
function googleWelcome() {
    $('#gmail-sign-in') [0].click();
}
function gmailLogin() {
    $('#Email').val(username);
    $('#Passwd').val(password);
    $('#signIn') [0].click();
}
function sendAlertMsg() {
    if ( GM_getValue('alert_body').length > 0 && $('#bnm').text().indexOf('Inbox') >= 0 ) {
      $('#bnc') [0].click();  // COMPOSE
    }
    if ( $('#to').length > 0 && $('table.actionBar').text() == 'Compose' ) {
      $('#to').val(recipient);
      $('#sub').val('++DoD Webmail++ ' + GM_getValue('alert_body').split('\n')[0] );
      $('#body').val( GM_getValue('alert_body') );
      GM_setValue('alert_body', '');
      $('#send') [0].click();  // SEND MESSAGE
    }
    if ( GM_getValue('alert_body').length < 1 ) {
      if ( $('#bnm').text().indexOf('Inbox') >= 0 ) { $('#bnm') [0].click(); }
      if ( $('#t').length > 0 ) { $('#t') [0].click(); }
      if ( $('#bnm').text().indexOf('Trash') >= 0 ) { 
        if ( $('.noMatches').text() == 'No messages.' ) {
          window.setTimeout( window.close(), 5000);
        }
        $('input[name=t]').attr('checked', true);
        $('#dl') [0].click();  // DELETE
      }     
    }
}
if (window.location.href == 'https://www.gmail.com/intl/en/mail/help/about.html') {
  window.setTimeout(googleWelcome(), 2000);
}
if (window.location.href.indexOf('https://accounts.google.com/ServiceLogin') >= 0) {
  window.setTimeout(gmailLogin(), 2000);
}
if (window.location.href.indexOf('https://mail.google.com/mail/u/0/x') >= 0) {
  window.setTimeout(sendAlertMsg(), 1000);
}