Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

Retrieve your Gmail messages as an XML feed

This Ruby example shows how to retrieve your most recent email as an atom XML feed from the Google Apps website.

require 'rubygems'
require 'httpclient'

url = "https://mail.google.com/a/yourwebsite.com/feed/atom"
client = HTTPClient.new
client.debug_dev = STDOUT if $DEBUG
client.set_auth(url, 'yourname@yourwebsite.com', 'yourpassword')
resp = client.get(url)


Note: You might require to gem install httpclient.

Ruby retrieve gmail emails through POP3

Found at
http://blog.seagul.co.uk/articles/2006/10/24/connecting-to-gmail-with-ruby-or-connecting-to-pop3-servers-over-ssl-with-ruby

pop.rb is the new Ruby 1.9 version

require 'pop_ssl' # I renamed the file from pop.rb to pop_ssl.rb to ensure I was requiring the correct version

username = 'YOUR_GMAIL_USERNAME@gmail.com'
password = 'YOUR_GMAIL_PASSWORD'

Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
Net::POP3.start('pop.gmail.com', 995, username, password) do |pop|
  if pop.mails.empty?
    puts 'No mail.'
  else
    pop.each_mail do |mail|
      p mail.header
    end
  end
end

Gmail - Select All Spam

// Standard HTML GMail does not have a select all option on the Spam page(s). Paste this into firebug console to select all...

for (i = 0; i < document.f.t.length; i++) {document.f.t[i].checked = true;}


or

for each(s in document.f.t){s.checked=true;}

Empty a Gmail label with FireWatir

When you have a huge label with thousands of messages, Gmail can't handle deleting all of them at a time (it says it can, but for me it's never worked). This solves the problem.

I wrote it more as a practice to get familiar with FireWatir, so it's not very pretty or anything and it's a bit slow. If you have doubts on how to use or suggestions on how to improve it please feel free to contact me.

By the way, it assumes Gmail is in German. Replace that string with the one that comes up for your language.

def empty_label(label)
  # you need a Firefox instance already running with JSSh installed and listening
  ff = Firefox.new
  # Goes to Gmail in HTML
  ff.goto('http://mail.google.com/mail/h/x4odcwnm5f5v/?s=l&l=#{label}')
  a = []
  # doing 'c.set' didn't work for me here so I had to do this hack of getting the value
  # and then acquiring the element thorugh ff.checkbox(:value,value)
  ff.checkboxes.each {|c| a << c.value}

  while a.length > 0 do
    c = []
    a.each {|v| c << ff.checkbox(:value,v)}
    # checks all checkboxes
    c.each {|e| e.set}
    # clicks the drop-down entry for deleting
    ff.select_list(:name,'tact').select('In den Papierkorb verschieben')
    ff.button(:name,'nvp_tbu_go').click
    a = []
    ff.checkboxes.each {|c| a << c.value}
  end
end

Gmail-Like Incremental Search //JavaScript Class


Implements a GMail-like auto-complete.

[UPDATED CODE AND HELP CAN BE FOUND HERE]


@REQUIRES Event-Listener

//Requires http://www.jsfromhell.com/geral/event-listener

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/dhtml/incremental-search [v1.0]

IncrementalSearch = function( input, callback, className ){
	var i, o = ( o = this, o.l = [], o.i = input, o.c = null, o.s = { e: null, i: -1 }, o.f = callback || function(){}, o.n = className || "", o );
	for( i in { keydown: 0, focus: 0, blur: 0, keyup: 0, keypress: 0 } )
		addEventListener( o.i, i, function( e ){ o.handler.call( o, e ); } );
};
with( { p: IncrementalSearch.prototype } ){
	(p.constructor.fadeAway = function( o ){
		o instanceof Object ? ( this.trash = this.trash || [] ).push( o ) && setTimeout( this.fadeAway, 200 ) : arguments.callee.c.trash.pop().hide();
	}).c = p.constructor;
	p.callEvent = function( e ){ this[e] && this[e].apply( this, [].slice.call( arguments, 1 ) ); };
	p.highlite = function( e ){ ( this.s.e && ( this.s.e.className = "normal" ), ( this.s = { e: e, i: e.listindex } ).e.className += " highlited", this.callEvent( "onhighlite", this.l[ this.s.i ], this.s.e.d ) ); };
	p.select = function(){ this.s.i + 1 && ( this.i.value = this.l[ this.s.i ], this.callEvent( "onselect", this.i.value, this.s.e.d ), this.hide() ); };
	p.hide = function(){ ( this.c && this.c.parentNode.removeChild( this.c ), this.c = null, this.l = [], this.s = { e: null, i: -1 }, this.callEvent( "onhide" ) ); };
	p.next = function(){ var e = ( e = this.s.e ) ? e.nextSibling || e.parentNode.firstChild : null; e && this.highlite( e ); };
	p.previous = function(){ var e = ( e = this.s.e ) ? e.previousSibling || e.parentNode.lastChild : null; e && this.highlite( e ); };
	p.handler = function( evt ){
		var o = this, t = evt.type, k = evt.key, e = /span/i.test( ( e = evt.target ).tagName ) ? e.parentNode : e;
		t == "keyup" ? k != 40 && k != 38 && k != 13 && o.show()
		: t == "keydown" ? ( k == 40 && o.next() ) || ( k == 38 && o.previous() )
		: t == "keypress" ? k == 13 && !evt.preventDefault() && o.select()
		: t == "blur" ? o.constructor.fadeAway( o )
		: t == "click" ? o.select()
		: t == "focus" ? o.show()
		: o.highlite( e );
	};
	p.show = function(){
		var cS, found = 0, o = this, i = o.i, iV = i.value, d = document, c = ( o.hide(), o.c = d.body.appendChild( d.createElement( "div" ) ) );
		( c.className = o.n, cS = c.style, cS.display = "none", cS.position = "absolute", o.callEvent( "onshow" ) );
		o.f.call( function( s, x, data ){
			if( !( x.length == undefined ? ( x = [x] ) : x ).length )
				return;
			var j, l = 0, i = o.l.length, e = c.appendChild( d.createElement( "div" ) );
			for( j in ( o.l[i] = s, e.className = "normal", e.d = data, e.listindex = i, !found && i == o.s.i && ++found && o.highlite( e ), x ) )
				e.appendChild( d.createTextNode( s.substring( l, x[j] ) ) ).parentNode.appendChild( d.createElement( "span" ) ).appendChild( d.createTextNode( s.substring( x[j], l = x[j] + iV.length ) ) ).parentNode.className = "selectedText";
			for( x in ( e.appendChild( d.createTextNode( s.substr( l ) ) ), { click: 0, mouseover: 0 } ) )
				addEventListener( e, x, function( e ){ o.handler.call( o, e ); } );
		}, iV );
		if( !c.childNodes.length )
			return o.hide();
		for( var x = i.offsetLeft, y = i.offsetTop + i.offsetHeight; i = i.offsetParent; x += i.offsetLeft, y += i.offsetTop );
		( cS.display = "block", cS.left = x + "px", cS.top = y + "px", !found && o.highlite( c.firstChild ) );
	};
}



Example

<style type="text/css">
/*container da lista*/
.autocomplete{
	cursor: pointer;
	border: 1px solid #999;
	border-top: none;
	background: #eee;
}
/*caracteres que combinaram*/
.autocomplete .selectedText{ font-weight: bold; color: #008; }
/*items não selecionados*/
.autocomplete .normal{ border-top: 1px solid #999; overflow: hidden; white-space: pre; }
/*item selecionado*/
.autocomplete .highlited{ background: #ddf; }
</style>

<form action="">
	<fieldset>
		<legend>Preenchimento dinâmico</legend>
		<label for="list" >Emails</label>
		<input autocomplete="0" type="text" name="list" id="list" />
		<br />
		<label for="ip" >Lista de IPs</label>
		<textarea name="ip" rows="3" cols="20" id="ip"></textarea>
		<br />
	</fieldset>
</form>

<script type="text/javascript">
//<![CDATA[

var list = [ "192.168.0.1", "192.168.0.2", "192.168.0.3", "192.168.1.1", "192.168.1.2", "192.168.1.3", "200.168.0.1", "200.168.0.2", "200.168.0.3", "200.168.1.1", "200.168.1.2", "200.168.1.3" ];
new IncrementalSearch( document.forms[0].ip, function( search ){
	for( var i in list )
		if( !list[i].indexOf( search ) )
			this( list[i], 0 );
}, "autocomplete" );


var names = [ "João Alves <joao@123.com>", "Jonas Raoni Soares Silva <jonas@abc.com>", "Roberto <rob@net.net>", "Maria Fernanda <mariaf@i.tu>" ];

function retrieveNames( search ){
	search = search.toLowerCase();
	for( var i in names ){
		if( search ){
			for( var j = 0, indices = []; j = names[i].toLowerCase().indexOf( search, j ) + 1; indices[indices.length] = j - 1 );
			this( names[i], indices, i );
		}
		else
			this( names[i], 0, i );

	}
}

x = new IncrementalSearch( document.forms[0].list, retrieveNames, "autocomplete" );


//]]>
</script>


Atom / RSS feed of your GMail account

https://USERNAME:PASSWORD@gmail.google.com/gmail/feed/atom

Change Gmail encoding to display thai.

Some emails send from yahoo and hotmail will display thai incorrectly. This bookmarklet re-encode it by shifting the unicode numbers. Can be applied to some language whose encoding is in the same "shifting" order.
javascript:(function(){  map=[];for(i=161;i<251;i++)map[i]=String.fromCharCode(i+3424);  function thai(s){s2='';for(var i=0;i<s.length;i++){n=s.charCodeAt(i);if(n>160&&n<251) s2+=map[n];else s2+=s.charAt(i)}return s2}   function rc_thai(el){if(el.nodeType== 3){el.data=thai(el.data);return} if(el.tagName == 'SCRIPT') return; for (var i=0; i<el.childNodes.length; i++) rc_thai(el.childNodes[i])}   for(i=0;i<4;i++){if(fi=window.frames[0].frames[i].document.getElementById('fi')) break}  rc_thai(fi); })();

The one-liner version above maybe difficult to read.
Here's a reorganized one
javascript:(function(){
map=[];
/* create conversion table */
for(i=161;i<251;i++) {
  map[i]=String.fromCharCode(i+3424);
}

function thai(s){
  s2='';
  for(var i=0;i<s.length;i++){
    n=s.charCodeAt(i);
    if(n>160&&n<251) 
      s2+=map[n];
    else 
      s2+=s.charAt(i)
  }
  return s2
}   

/* recursively convert encoding of sub-element */
function rc_thai(el){
  if(el.nodeType== 3){
    el.data=thai(el.data);
    return
  } 
  if(el.tagName == 'SCRIPT') 
    return; 
  for (var i=0; i<el.childNodes.length; i++)
    rc_thai(el.childNodes[i])
}   

/* finding the content element in sub-sub-frame */
for(i=0;i<4;i++){
  if(fi=window.frames[0].frames[i].document.getElementById('fi'))
    break
}  

/* change that element (and all its descendants */
rc_thai(fi); 
})();
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS