Skip to content

Instantly share code, notes, and snippets.

@jeffdeville
Created April 22, 2012 20:24
Show Gist options
  • Select an option

  • Save jeffdeville/2466656 to your computer and use it in GitHub Desktop.

Select an option

Save jeffdeville/2466656 to your computer and use it in GitHub Desktop.
/** If user is not loggeding **/
move2picasa.loggedout=function(xhr, textstatus){
move2picasa.$('#details').hide()
move2picasa.$('#message').show()
if(xhr.status==0){
move2picasa.showMessage('Looks like you have aborted an ongoing request. Please refresh to continue.', true)
}else{
if(xhr.responseText == 'Authorization required'){
move2picasa.$('#message').html('Please login into <a target="_blank" href="http://picasaweb.google.com">Picasa</a> in another tab and then refresh this tab to use the extension. <br/> Do not logout/login into another account until transfer is complete. Hit "Cancel" and "Upload" again if there are any hiccups.').css('color', 'red')
}
else{
move2picasa.$('#message').html('Some error occured, Google says: <br/>'+(xhr.responseText)).css('color', 'red')
}
}
}
/** Show Message **/
move2picasa.showMessage=function(message, warning){
move2picasa.$('#details').hide()
move2picasa.$('#message').show()
move2picasa.$('#message').html(message)
color='green'
if(warning){
color='red'
}
move2picasa.$('#message').css('color', color)
}
/** Show Message **/
move2picasa.hideMessage=function(){
move2picasa.$('#details').show()
move2picasa.$('#message').hide()
}
/** Storing Data To Local DB **/
move2picasa.setItem=function(key, value){
try{
window.localStorage.removeItem(key);
window.localStorage.setItem(key, value);
return true
}catch(e){
}
return false;
}
/** Accessing Data From Local DB **/
move2picasa.getItem=function(key){
try{
d=window.localStorage.getItem(key);
return d
}catch(e){
}
return null;
}
/** Removing Data From Local DB **/
move2picasa.removeItem=function(key){
try{
window.localStorage.removeItem(key);
return true;
}catch(e){
}
return false;
}
/** Closing The Notification **/
move2picasa.$('#close-notification').click(function(){
move2picasa.$('#notification').slideUp()
move2picasa.$('#title').animate({'marginTop':10})
move2picasa.setItem('close-notification', true)
})
/** Simple logging function **/
move2picasa.log=function(data){
console.log(data)
}
/** Fetching OAuth Data **/
move2picasa.getOAuth=function(callback){
oauth=window.location.href
oauth=oauth.split('=')
move2picasa.oauth=oauth[1]
move2picasa.setItem('fboauth', move2picasa.oauth)
callback()
}
/** Attaching Click Events To The Images **/
move2picasa.attachImageHandler=function(){
move2picasa.$('.img').die('click').live('click', function(){
move2picasa.$(this).removeClass('skip')
if(move2picasa.$(this).hasClass('selected')){
move2picasa.setItem('psel_'+move2picasa.$(this).attr('pid'), false)
move2picasa.$(this).removeClass('selected')
move2picasa.$(this).addClass('notselected')
move2picasa.$(this).parent().parent().parent().parent().find('.albumselector').attr('checked', false)
if(move2picasa.$(this).parent().parent().parent().parent().find('.selected').length==0){
move2picasa.$(this).parent().parent().parent().parent().find('.uploadnone').attr('checked', true)
}
}else{
move2picasa.setItem('psel_'+move2picasa.$(this).attr('pid'), true)
move2picasa.$(this).removeClass('notselected')
move2picasa.$(this).addClass('selected')
move2picasa.$(this).parent().parent().parent().parent().find('.uploadnone').attr('checked', false)
if(move2picasa.$(this).parent().parent().parent().parent().find('.img').length == move2picasa.$(this).parent().parent().parent().parent().find('.selected').length){
move2picasa.$(this).parent().parent().parent().parent().find('.uploadall').attr('checked', true)
}
}
})
}
/** Album Selector, Full OR No Image From An Album **/
move2picasa.attachAlbumSelector=function(){
move2picasa.$('.albumselector').bind('click', function(){
if(move2picasa.$(this).is(':checked')){
if(move2picasa.$(this).hasClass('uploadall')){
move2picasa.$(this).parent().parent().parent().find('.img').each(function(){
move2picasa.$(this).removeClass('notselected').addClass('selected')
pid=move2picasa.$(this).attr('pid')
move2picasa.setItem('psel_'+pid, true)
})
move2picasa.$(this).parent().parent().find('.uploadnone').attr('checked', false)
}else{
move2picasa.$(this).parent().parent().find('.uploadall').attr('checked', false)
move2picasa.$(this).parent().parent().parent().find('.img').each(function(){
move2picasa.$(this).removeClass('selected').addClass('notselected')
pid=move2picasa.$(this).attr('pid')
move2picasa.setItem('psel_'+pid, false)
})
}
}
})
}
/** Disabling Image Drag **/
move2picasa.disableImageDrag=function(){
move2picasa.$('.img').bind('dragstart', function(e){
e.preventDefault();
});
}
/** Attaching Event To Upload/Cancel Button **/
move2picasa.AttachUploadHandler=function(){
move2picasa.$('#upload-button').css('display', 'block')
move2picasa.$('#upload-button button').click(function(){
if(typeof move2picasa.deletingEmptyAlbums!='undefined' && move2picasa.deletingEmptyAlbums){
alert('Please wait untill all your empty albums are deleted.')
return;
}
if(move2picasa.$(this).text()=='Upload')
{
move2picasa.$(this).text('Cancel')
move2picasa.$('.img').die('click').live('click', function(){ alert('You cannot select/deselect while upload is in progress.') })
move2picasa.$('.albumselector').unbind('click')
move2picasa.$('.albumselector').attr("disabled", true);
move2picasa.$('#total').text(move2picasa.$('.selected:not(.skip)').length)
move2picasa.totaluploadcount=move2picasa.$('.selected:not(.skip)').length
move2picasa.$('#done-uploading').text(0)
move2picasa.$('#upload-status').show()
move2picasa.startUploadFlag=true;
move2picasa.setItem('startUploadFlag', true)
move2picasa.startUpload()
}else{
move2picasa.startUploadFlag=false;
move2picasa.setItem('startUploadFlag', false)
move2picasa.$(this).text('Upload')
move2picasa.$('.albumselector').attr("disabled", false);
move2picasa.attachImageHandler()
move2picasa.attachAlbumSelector()
move2picasa.$('#upload-status').hide()
}
})
}
/** Lookup Picasa Album Id **/
move2picasa.lookupPicasaId=function(imagetoupload, callback){
var lookupalbumid=move2picasa.$(imagetoupload).attr('aid')
_fb_albums=move2picasa.getItem('_fb_albums')
_fb_albums=move2picasa.$.parseJSON(_fb_albums)
var fbalbumname=_fb_albums[lookupalbumid]
var tempid=false
move2picasa.$.ajax({
dataType:'xml',
url:'https://picasaweb.google.com/data/feed/api/user/default',
success:function(data){
picasaalbums=data.getElementsByTagName('entry')
picasaalbums_count=picasaalbums.length
tempid=false;
for(alcount=0; alcount<picasaalbums_count; alcount++){
title=picasaalbums[alcount].getElementsByTagName('title')[0].firstChild.nodeValue
//console.log(title+' => '+fbalbumname)
if(title==fbalbumname){
tempid=picasaalbums[alcount].getElementsByTagName('id')[0].firstChild.nodeValue
tempid=tempid.split('/')
tempid=tempid[tempid.length-1]
break
}
}
if(tempid){
move2picasa.setItem('pc4_'+lookupalbumid, tempid)
callback(imagetoupload, tempid, move2picasa.uploadBinary)
}else{
move2picasa.createPicasaAlbum(imagetoupload, callback)
}
},
error:function(data, textstatus){
move2picasa.loggedout(data, textstatus)
}
})
}
/** Create Album **/
move2picasa.createPicasaAlbum=function(imagetoupload, callback){
var fbalbumidtocreate=move2picasa.$(imagetoupload).attr('aid')
_fb_albums=move2picasa.getItem('_fb_albums')
_fb_albums=move2picasa.$.parseJSON(_fb_albums)
var createfbalbumname=_fb_albums[fbalbumidtocreate]
var createtempid=false
createalbum = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:gphoto='http://schemas.google.com/photos/2007'>";
createalbum+=" <title type='text'><![CDATA["+createfbalbumname+"]]></title>"
createalbum+=" <gphoto:access>private</gphoto:access>"
createalbum+=" <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/photos/2007#album'></category>"
createalbum+="</entry>"
move2picasa.$.ajax({
url:'https://picasaweb.google.com/data/feed/api/user/default',
type:'POST',
dataType:'xml',
headers:{'Content-Type':'application/atom+xml'},
data:createalbum,
success:function(newalbumdata){
entry=newalbumdata.getElementsByTagName('entry')[0]
createdalbumid=entry.getElementsByTagName('id')[0].firstChild.nodeValue
createdalbumid=createdalbumid.split('/')
createdalbumid=createdalbumid[createdalbumid.length-1]
move2picasa.setItem('pc4_'+fbalbumidtocreate, createdalbumid)
callback(imagetoupload, createdalbumid, move2picasa.uploadBinary)
},
error:function(data, textstatus){
move2picasa.loggedout(data, textstatus)
}
})
}
/** Getting Picasa Destination Album Id For An Image **/
move2picasa.getPicasaId=function(imagetoupload, callback){
albumid=move2picasa.$(imagetoupload).attr('aid')
id=move2picasa.getItem('pc4_'+albumid)
if(id!=null){
callback(imagetoupload, id, move2picasa.uploadBinary)
}else{
move2picasa.lookupPicasaId(imagetoupload, callback)
}
}
/** Fetching Binary Data **/
move2picasa.getBinary=function(imagetoupload, picasaalbumid, callback){
file=move2picasa.$(imagetoupload).attr('src_big')
var xhr = new XMLHttpRequest();
xhr.open("GET", file, true);
xhr.overrideMimeType("text/plain; charset=x-user-defined");
xhr.send(null);
xhr.onreadystatechange=function(){
if(xhr.readyState!=4){return;}
callback(imagetoupload, picasaalbumid, xhr, {'Content-Type':'image/jpeg'}, move2picasa.uploadCallBack);
}
}
/** Uploading Binary Data **/
move2picasa.uploadBinary=function(imagetoupload, picasaalbumid, imagebinaryxhr, header, callback){
url='https://picasaweb.google.com/data/feed/api/user/default/albumid/'+picasaalbumid
imagedata=imagebinaryxhr.responseText
if(move2picasa.$.trim(imagedata)=='Invalid Request' || move2picasa.$.trim(imagedata)==''){
move2picasa.$(imagetoupload).addClass('skip')
move2picasa.startUpload()
}else{
XMLHttpRequest.prototype.sendAsBinary = function (datastr){
function byteValue(x) {
return x.charCodeAt(0) & 0xff;
}
var ords = Array.prototype.map.call(datastr, byteValue);
var ui8a = new Uint8Array(ords);
this.send(ui8a.buffer);
};
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
for(h in header){
xhr.setRequestHeader(h, header[h]);
}
xhr.onreadystatechange=function(){
if(xhr.readyState!=4){return;}
callback(xhr, imagetoupload);
}
xhr.sendAsBinary(imagedata);
}
}
/** Callback After Each Photo Is Uploaded **/
move2picasa.uploadCallBack=function(xhrvar, imagetoupload){
if(xhrvar.status==403 || xhrvar.responseText=='Authorization required'){
alert('Looks like you have logged out of your Google account. Please relogin and click "Upload" to continue upload')
move2picasa.$('#upload-button button').click()
}else if(xhrvar.status==404 || xhrvar.responseText=='No album found.'){
alert('Looks like you have loggedin with another account. Please relogin and click "Upload" to continue upload.')
move2picasa.$('#upload-button button').click()
}
else if(move2picasa.$.trim(xhrvar.responseText)=='Error: 10 STORAGE_UNAVAIL'){
alert('Looks like, you have crossed Picasa\'s available limit. I would not be able to transfer any more pictures.')
move2picasa.$('#upload-button button').click()
}else if(xhrvar.status!=201){//uploaded
alert("Some error occured. Googls says:\n"+xhrvar.responseText)
move2picasa.$('#upload-button button').click()
}
else{
move2picasa.setItem('upd_'+move2picasa.$(imagetoupload).attr('pid'), true)
move2picasa.$(imagetoupload).removeClass('img').removeClass('selected').addClass('uploaded')
move2picasa.startUpload()
}
}
/** No Click Event On Already Uploaded Photos **/
move2picasa.uploadedPhotos=function(){
move2picasa.$('.uploaded').live('click', function(){
alert('This photo is already uploaded, you cannot de-select it.')
})
}
/** Upload Function **/
move2picasa.startUpload=function(){
if(!move2picasa.startUploadFlag){
return false;
}
count=(parseInt(move2picasa.$.trim(move2picasa.$('#done-uploading').text()))+1)
if(move2picasa.$('.selected:not(.skip)').length == 0){
move2picasa.startUploadFlag=false
alert('Finished uploading all selected photos.')
move2picasa.$('#upload-button button').click()
}else{
move2picasa.$('#upload-status-wrapper').html('<div id="upload-status" style="display:block"><span id="done-uploading">'+count+'</span> of <span id="total">'+(move2picasa.totaluploadcount)+'</span> Uploading <!--<a href="http://flattr.com/thing/340617/Move2Picasa" target="_blank"><img src="http://api.flattr.com/button/flattr-badge-large.png" style="vertical-align:text-top;" alt="Flattr this" title="Flattr this" border="0" /></a>--></div>')
imagetoupload=move2picasa.$('.selected:not(.skip)').eq(0)
count=(parseInt(move2picasa.$.trim(move2picasa.$('#done-uploading').text()))+1)
move2picasa.imagetoupload=imagetoupload
move2picasa.getPicasaId(imagetoupload, move2picasa.getBinary)
}
}
close=move2picasa.getItem('close-notification')
if(close=='true'){
move2picasa.$('#notificationwrapper').hide()
move2picasa.$('#title').css({'marginTop':10})
}
move2picasa.fetchFBPhotos=function(_fb_albums_temp){
var firstalbum=false;
var firstalbumdetails=false;
for(var albumid in _fb_albums_temp) {
firstalbum=albumid;
firstalbumdetails=_fb_albums_temp[firstalbum]
delete _fb_albums_temp[firstalbum];
break
}
if(firstalbum!=false){
move2picasa.showMessage('Fetching list of photos of Album: <b>'+firstalbumdetails+'</b>...')
move2picasa.$.ajax({
dataType:'xml',
url:'https://api.facebook.com/method/photos.get?aid='+firstalbum+'&access_token='+move2picasa.oauth,
success:function(data){
photos=data.getElementsByTagName('photo')
no_of_photos=photos.length
for(counterphotos=0; counterphotos<no_of_photos; counterphotos++){
pid=photos[counterphotos].getElementsByTagName('pid')[0].firstChild.nodeValue
src_big=photos[counterphotos].getElementsByTagName('src_big')[0].firstChild.nodeValue
src_small=photos[counterphotos].getElementsByTagName('src_small')[0].firstChild.nodeValue
selected=move2picasa.getItem('psel_'+pid)
if(selected==null){
selected=true
move2picasa.setItem('psel_'+pid, selected)
}
uploaded=move2picasa.getItem('upd_'+pid)
if(uploaded=='true'){
html='<li class="imgwrapper"><img pid="'+pid+'" class="uploaded" aid="'+firstalbum+'" fbaid="'+(firstalbum)+'" src_big="'+(src_big)+'" src="'+(src_small)+'"/></li>'
}else{
html='<li class="imgwrapper"><img pid="'+pid+'" class="img '+((selected=='false')?'notselected':'selected')+'" aid="'+firstalbum+'" fbaid="'+(firstalbum)+'" src_big="'+(src_big)+'" src="'+(src_small)+'"/></li>'
}
move2picasa.$('#aid_'+firstalbum+' .photo-list ul').append(html)
}
if(move2picasa.$('#aid_'+firstalbum).find('.img').length!==0){
if(move2picasa.$('#aid_'+firstalbum).find('.img').length!=move2picasa.$('#aid_'+firstalbum+' .selected').length){
move2picasa.$('#aid_'+firstalbum).find('.uploadall').attr('checked', false)
}
if(move2picasa.$('#aid_'+firstalbum).find('.img').length==move2picasa.$('#aid_'+firstalbum+' .notselected').length){
move2picasa.$('#aid_'+firstalbum).find('.uploadnone').attr('checked', true)
}
}else{
move2picasa.$('#aid_'+firstalbum).find('.uploadall').attr('checked', false)
move2picasa.$('#aid_'+firstalbum).find('.uploadnone').attr('checked', false)
}
move2picasa.fetchFBPhotos(_fb_albums_temp)
},
error:function(data, textstatus){
move2picasa.loggedout(data, textstatus)
}
})
}else{
move2picasa.$('#album-list').show()
move2picasa.hideMessage()
move2picasa.attachImageHandler()
move2picasa.attachAlbumSelector()
move2picasa.disableImageDrag()
move2picasa.hideMessage()
move2picasa.AttachUploadHandler()
move2picasa.uploadedPhotos()
move2picasa.$('#uncheckall').click(function(){
move2picasa.$('.selected').click()
})
move2picasa.$('#checkall').click(function(){
move2picasa.$('.notselected').click()
})
}
}
move2picasa.fetchFbAlbums=function(){
oauth=move2picasa.oauth
move2picasa.$.ajax({
url:'https://api.facebook.com/method/photos.getAlbums?access_token='+oauth,
dataType:'xml',
success:function(data){
var _fb_albums={};
albums=data.getElementsByTagName('album')
no_of_albums=albums.length
for(alcounter=0; alcounter<no_of_albums; alcounter++){
var aid=albums[alcounter].getElementsByTagName('aid')[0].firstChild.nodeValue
var name=albums[alcounter].getElementsByTagName('name')[0].firstChild.nodeValue
_fb_albums[aid]=name
move2picasa.$('#album-list').append('<ul class="albumcontainer" id="aid_'+aid+'"><li><h5>'+name+'</h2><label><input type="checkbox" checked class="albumselector uploadall"/> Upload All</label> <label><input type="checkbox" class="albumselector uploadnone"/> Upload None</label></li><li class="photo-list"><ul></ul></li></ul>')
}
move2picasa._fb_albums=_fb_albums
move2picasa.setItem('_fb_albums', JSON.stringify(_fb_albums))
move2picasa.fetchFBPhotos(_fb_albums)
},
error:function(data, textstatus){
if(data.status==0){
move2picasa.showMessage('Looks like you have aborted an ongoing request. Please refresh to continue.', true)
}else{
move2picasa.showMessage('Some error occured while fetching list of your albums. Please refresh in sometime.', true)
}
}
})
}
/** Clear All Data And Close Current Tab**/
move2picasa.clearData=function(){
answer=false
if((typeof move2picasa.startUploadFlag!='undefined') && move2picasa.startUploadFlag==true){
answer=confirm("Upload in progress. \nIf you want to save your selection/continue later (to prevent duplicates), consider option 'pause & close'.\nPress OK to logout.")
}else{
answer=confirm("Logging out will delete all your image selections. \nIf you want to save your selection & continue later, consider option 'pause & close'.\nPress OK to logout.")
}
if(answer){
move2picasa.$(window).unbind('beforeunload')
window.localStorage.clear();
chrome.tabs.getSelected(null, function(tab){
chrome.tabs.remove(tab.id)
move2picasa.removeItem('close-notification')
});
}
}
/** Attach Logout **/
move2picasa.$('#clearall').click(function(){
move2picasa.clearData()
})
/** Attach Pause **/
move2picasa.$('#pause').click(function(){
move2picasa.$(window).unbind('beforeunload')
chrome.tabs.getSelected(null, function(tab){
chrome.tabs.remove(tab.id)
move2picasa.removeItem('close-notification')
});
})
move2picasa.$(window).bind('beforeunload', function() {
if(typeof move2picasa.startUploadFlag!='undefined' && move2picasa.startUploadFlag){
return "Upload to Picasa in progress. \n Are you sure you want to leave this page?";
}
});
/** For Deleting Empty Albums **/
move2picasa.deleteFirstEmptyAlbum=function(empty_albums, delete_empty_albums_link, callback){
album=empty_albums.pop()
move2picasa.$.ajax({
url:delete_empty_albums_link[album],
type:'DELETE',
success:function(data){
move2picasa.$('#delete-status-done').text(move2picasa.emptyAlbumCount-empty_albums.length)
if(empty_albums.length!=0){
move2picasa.deleteFirstEmptyAlbum(empty_albums, delete_empty_albums_link, callback)
}else{
alert('Congratulation! All your empty album has been deleted.')
move2picasa.deletingEmptyAlbums=false;
move2picasa.$('#delete-status').hide()
callback(move2picasa.fetchFbAlbums)
}
},
error:function(data, textstatus){
move2picasa.loggedout(data, textstatus)
}
})
}
move2picasa.showMessage('Verifying if your are loggedin to Picasa...')
move2picasa.$.ajax({
url:'https://picasaweb.google.com/data/feed/api/user/default',
success:function(data){
/** Empty Albums **/
userdata=move2picasa.$.xml2json(data)
empty_albums=[]
delete_empty_albums_link={}
for(entry in userdata.entry){
if(userdata.entry[entry].numphotos == 0){
id=userdata.entry[entry].id.split('/')
id=id[id.length-1]
empty_albums.push(id)
for(link in userdata.entry[entry].link){
if(userdata.entry[entry].link[link].rel=='edit'){
delete_empty_albums_link[id]=userdata.entry[entry].link[link].href
}
}
}
}
move2picasa.deletingEmptyAlbums=false
if(empty_albums.length>0){
answer=confirm("Looks like you have "+(empty_albums.length)+" empty albums in picasa. Click 'Ok' if you want me to delete the empty albums.")
if(answer){
move2picasa.deletingEmptyAlbums=true
move2picasa.emptyAlbumCount=empty_albums.length
move2picasa.$('#delete-status').show()
move2picasa.$('#delete-status-total').text(move2picasa.emptyAlbumCount)
move2picasa.$('#delete-status-done').text(0)
move2picasa.deleteFirstEmptyAlbum(empty_albums, delete_empty_albums_link, move2picasa.getOAuth)
}else{
move2picasa.getOAuth(move2picasa.fetchFbAlbums)
}
}else{
move2picasa.getOAuth(move2picasa.fetchFbAlbums)
}
},
error:function(data, textstatus){
move2picasa.loggedout(data, textstatus)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment