Skip to content

Instantly share code, notes, and snippets.

@YungSang
Created August 28, 2012 00:38
Show Gist options
  • Save YungSang/3493861 to your computer and use it in GitHub Desktop.
Save YungSang/3493861 to your computer and use it in GitHub Desktop.
Models.register({
name : 'LoveIt',
ICON : 'http://assets.loveit.com/assets/favicon.ico',
LINK : 'http://loveit.com/',
LOGIN_URL : 'http://loveit.com/me/login',
BOOKMARK_URL : 'http://loveit.com/loves/bookmarklet/create',
UPLOAD_URL : 'http://loveit.com/loves/upload/image',
POST_URL : 'http://loveit.com/loves/upload/create',
check : function(ps) {
return (/photo/).test(ps.type) && !ps.file;
},
collections : null,
getCollections : function() {
return this.collections;
},
_getCollections : function(check_login) {
var self = this;
return request(this.BOOKMARK_URL + '?' + queryString({
type : 'image',
src : 'http://assets.loveit.com/assets/loveIt_squirrel.jpg'
})).addCallback(function(res) {
var doc = createHTML(res.responseText);
if (check_login && !$X('id("board_id")', doc)) {
throw new Error(chrome.i18n.getMessage('error_notLoggedin', self.name));
}
var collections = [];
$X('id("board_id")/option', doc).forEach(function(option) {
collections.push({
id : $X('./@value', option)[0],
name : $X('./text()', option)[0]
});
});
return self.collections = collections;
});
},
getCSRFToken : function() {
var self = this;
return request(this.LINK).addCallback(function(res) {
var doc = createHTML(res.responseText);
if ($X('//a[@href="/me/login"]', doc)[0]) {
throw new Error(chrome.i18n.getMessage('error_notLoggedin', self.name));
}
return $X('//meta[@name="csrf-token"]/@content', doc)[0];
});
},
getMostRecentCollectionId : function() {
var self = this;
return getCookies('loveit.com', 'most_recent_board_id').addCallback(function(cookies) {
if (cookies.length) {
return cookies[cookies.length-1].value;
} else {
return self._getCollections(true).addCallback(function(collections) {
return collections && collections[0] && collections[0].id;
});
}
});
},
post : function(ps) {
var self = this;
var description = '';
if (ps.description || ps.body) {
description = joinText([
ps.description,
(ps.body) ? '“' + ps.body + '”' : ''
], "\n\n", true);
}
var sendContent = {};
if (ps.file) {
description = joinText([
description,
'(via ' + ps.pageUrl + ' )'
], "\n\n", true);
return this.upload(ps, description);
}
else {
sendContent = {
utf8 : '✓',
form_submit : 1,
src : ps.itemUrl,
ref : ps.pageUrl,
title : ps.item || ps.page,
description : description,
};
}
return this.getCSRFToken().addCallback(function(csrftoken) {
sendContent.authenticity_token = csrftoken;
return self.getMostRecentCollectionId().addCallback(function(board_id) {
sendContent.board_id = board_id;
return request(self.BOOKMARK_URL, {
sendContent : sendContent
}).addCallback(function(res) {
});
});
});
},
uploadImage : function(ps, csrftoken) {
return request(this.UPLOAD_URL, {
sendContent : {
the_image : ps.file
},
headers : {
'X-CSRF-Token' : csrftoken,
'X-Requested-With' : 'XMLHttpRequest'
}
}).addCallback(function(res) {
try {
var json = JSON.parse(res.responseText);
}
catch(e) {
throw new Error("Couldn't upload an image properly");
}
if (json && json.image) {
return json.image;
}
else {
throw new Error("Couldn't upload an image properly");
}
});
},
upload : function(ps, description) {
var self = this;
return this.getCSRFToken().addCallback(function(csrftoken) {
return self.uploadImage(ps, csrftoken).addCallback(function(image) {
return self.getMostRecentCollectionId().addCallback(function(board_id) {
return request(self.POST_URL, {
sendContent : {
image_id : image.id,
board_id : board_id,
description : description
},
headers : {
'X-CSRF-Token' : csrftoken,
'X-Requested-With' : 'XMLHttpRequest'
}
}).addCallback(function(res) {
try {
var json = JSON.parse(res.responseText);
}
catch(e) {
throw new Error("Couldn't upload an image properly");
}
if (json && json.pin && json.pin.pin && json.pin.pin.id) {
return;
}
else {
throw new Error("Couldn't upload an image properly");
}
});
});
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment