Source for "Gmail Full Width 2"

By Raffles
Has 8 other scripts.


// ==UserScript==
// @name          Gmail Full Width 2
// @author        Raffles
// @namespace     http://ratherodd.com/
// @description   For new version of Gmail: when viewing a message/conversation, the advertisement box on the right is removed and messages and reply box occupy the full width available. Long titles are optionally truncated.
// @include       http*://mail.google.tld/mail/*
// ==/UserScript==

var truncate = true;

window.addEventListener('load', function() {  
  if (unsafeWindow.gmonkey) {
    unsafeWindow.gmonkey.load('1.0', function(gmail) {
      gmail.registerViewChangeCallback(function() {
        var rhs = gmail.getConvRhsElement();
        if (!rhs) return;
        var h1 = rhs.parentNode.previousSibling.previousSibling.getElementsByTagName('h1')[0];
        var h1text = h1.firstChild;
        var mtools = h1.parentNode.appendChild(rhs.firstChild.firstChild);
        var msgs = h1.parentNode.nextSibling;
        var int = window.setInterval(function() {
          if (rhs.innerHTML.length > 0 && rhs.innerHTML.indexOf('Would you like') > -1) {
            var xres = document.evaluate('//div[contains(text(), "Would you like")]', rhs.firstChild, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
            var mapthis = xres.snapshotItem(0).parentNode.cloneNode(true);
            mapthis.removeChild(mapthis.firstChild);
            mapthis.style.cssFloat = 'right';
            mapthis.style.margin = '0 14px;'
            mapthis.style.maxWidth = '120px';
            mapthis.style.border = 'none';
            var msgbody = msgs.firstChild.firstChild.nextSibling.firstChild.firstChild.firstChild.nextSibling.firstChild.firstChild.firstChild.firstChild.firstChild.firstChild.firstChild.nextSibling.firstChild.nextSibling.nextSibling.nextSibling;
            msgbody.parentNode.insertBefore(mapthis, msgbody);
          }
          if (rhs.innerHTML.length > 0) {
            window.clearInterval(int);
            rhs.parentNode.parentNode.removeChild(rhs.parentNode);
          }
        }, 150);
        rhs.parentNode.style.display = 'none';
        h1.style.cssFloat = 'left';
        msgs.style.clear = 'both';
        msgs.parentNode.parentNode.parentNode.parentNode.style.width = '100%'; // table
        mtools.style.cssFloat = 'right';
        mtools.style.marginRight = '8px';
        Array.forEach(mtools.getElementsByTagName('div'), function(sidelink) {
          sidelink.style.cssFloat = 'left';
          if (sidelink.firstChild && sidelink.firstChild.nodeName === 'DIV') return;
          sidelink.style.marginLeft = '4px';
          sidelink.style.marginRight = '4px';
        });
        window.setTimeout(function() {
          var h1w = gmail.getActiveViewElement().offsetWidth - mtools.offsetWidth - h1text.nextSibling.offsetWidth - 50;
          if (truncate && h1text.offsetWidth > h1w) {
            var loopcount = 0;
            var subj = truncsubj = h1text.innerHTML.replace(/<wbr>/g,'');
            while (h1text.offsetWidth > h1w) {
              h1text.innerHTML = truncsubj = truncsubj.substr(0, truncsubj.length - 4);
              loopcount++;
              if (loopcount > 100) {
                alert('loopcount has exceeded 100. Please tell author of Gmail Full Width 2 at http://userscripts.org/scripts/show/15998 that this has happened, and that the subject line was ' + subj.length + ' characters long. Also please mention whether the script is working properly other than for this annoying message.');
                break;
              }
            }
            h1text.innerHTML = truncsubj + '...';
            h1.style.position = 'relative';
            h1text.style.width = h1w + 'px';
            h1text.style.position = 'absolute';
            h1text.style.left = '10px';
            h1text.style.paddingLeft = '5px';
            h1text.style.overflow = 'hidden';
            h1text.style.backgroundColor = 'white';
            h1text.style.zIndex = '4';
            h1text.style.whiteSpace = 'nowrap';
            h1text.addEventListener('mouseover', function() {
              h1text.firstChild.nodeValue = subj;
              h1text.style.width = (msgs.offsetWidth - 28) + 'px';
              h1text.style.whiteSpace = 'normal';
              h1text.style.borderBottom = '2px solid #CCC';
              h1text.style.paddingBottom = '4px';
            }, false);
            h1text.addEventListener('mouseout', function() {
              h1text.firstChild.nodeValue = truncsubj + '...';
              h1text.style.width = h1w + 'px';
              h1text.style.whiteSpace = 'nowrap';
              h1text.style.borderBottom = '';
              h1text.style.paddingBottom = '';
            }, false);
            h1text.nextSibling.style.marginLeft = (h1w + 20) + 'px';
          }
          else {
            h1text.style.cssFloat = 'left';
          }
          h1text.nextSibling.style.cssFloat = 'left';
          Array.forEach(h1text.nextSibling.getElementsByTagName('table'), function(table) {
            table.style.display = 'inline-block'; // little tables containing "label | x" links
          });
        }, 500);
        Array.forEach(msgs.getElementsByTagName('textarea'), function(ta) {
          ta.style.width = '98.5%';
        });
      });
    });
  }
}, false);