2010-12-02 02:41:51 +00:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<script type="text/javascript" src="base64.js"></script>
|
|
|
|
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
|
|
|
|
<script type="text/javascript" src="chrome_ex_oauthsimple.js"></script>
|
|
|
|
<script type="text/javascript" src="chrome_ex_oauth.js"></script>
|
2010-12-02 04:35:13 +00:00
|
|
|
<script type="text/javascript" src="Crypto.js"></script>
|
|
|
|
<script type="text/javascript" src="SHA1.js"></script>
|
2010-12-02 02:41:51 +00:00
|
|
|
|
|
|
|
<script type="text/javascript" charset="utf-8">
|
|
|
|
|
|
|
|
var OAUTH = ChromeExOAuth.initBackgroundPage({
|
|
|
|
'request_url' : 'https://www.google.com/accounts/OAuthGetRequestToken',
|
|
|
|
'authorize_url' : 'https://www.google.com/accounts/OAuthAuthorizeToken',
|
|
|
|
'access_url' : 'https://www.google.com/accounts/OAuthGetAccessToken',
|
|
|
|
'consumer_key' : 'anonymous',
|
|
|
|
'consumer_secret' : 'anonymous',
|
|
|
|
'scope' : 'http://picasaweb.google.com/data/',
|
|
|
|
'app_name' : 'Clip It Good: Chrome Extension'
|
|
|
|
});
|
|
|
|
|
|
|
|
// Constants for various album types.
|
|
|
|
var PICASA = 'picasa';
|
2010-12-02 04:35:13 +00:00
|
|
|
var CAMLISTORE = 'camlistore';
|
2010-12-02 02:41:51 +00:00
|
|
|
var ALBUM_TYPE_STRING = {
|
2010-12-02 04:35:13 +00:00
|
|
|
picasa: 'Picasa Web Albums',
|
|
|
|
camlistore: 'Camlistore'
|
2010-12-02 02:41:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Preferences
|
|
|
|
var ALBUM_CONFIG = {}; // 'type' -> ('id' -> 'name')
|
|
|
|
|
|
|
|
function loadAlbumConfig() {
|
|
|
|
var newAlbumConfig = localStorage.getItem('config:albums');
|
|
|
|
if (newAlbumConfig) {
|
|
|
|
ALBUM_CONFIG = $.parseJSON(newAlbumConfig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveAlbumConfig() {
|
|
|
|
localStorage.setItem('config:albums', JSON.stringify(ALBUM_CONFIG));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort albums by name.
|
|
|
|
function getSortedAlbums(albumIdNameDict) {
|
|
|
|
var albumIdNameArray = [];
|
|
|
|
$.each(albumIdNameDict, function(id, name) {
|
|
|
|
albumIdNameArray.push({'id': id, 'name': name});
|
|
|
|
});
|
|
|
|
albumIdNameArray.sort(function(a, b) {
|
|
|
|
if (a['name'] < b['name']) {
|
|
|
|
return -1;
|
|
|
|
} else if (a['name'] > b['name']) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return albumIdNameArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupMenus() {
|
|
|
|
loadAlbumConfig();
|
|
|
|
|
|
|
|
chrome.contextMenus.removeAll(function() {
|
|
|
|
$.each(ALBUM_CONFIG, function(albumType, albumIdNameDict) {
|
|
|
|
chrome.contextMenus.create({
|
|
|
|
title: ALBUM_TYPE_STRING[albumType],
|
|
|
|
contexts: ['image']
|
|
|
|
});
|
|
|
|
chrome.contextMenus.create({
|
|
|
|
type: 'separator',
|
|
|
|
contexts: ['image']
|
|
|
|
});
|
|
|
|
|
|
|
|
$.each(getSortedAlbums(albumIdNameDict), function(index, albumDict) {
|
|
|
|
chrome.contextMenus.create({
|
|
|
|
title: albumDict.name,
|
|
|
|
contexts: ['image'],
|
|
|
|
onclick: function(data, tab) {
|
|
|
|
return handleMenuClick(
|
2010-12-02 04:35:13 +00:00
|
|
|
albumType, albumDict.name, albumDict.id, data, tab);
|
2010-12-02 02:41:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2010-12-02 04:35:13 +00:00
|
|
|
|
|
|
|
// XXX a hack for camli short term
|
|
|
|
chrome.contextMenus.create({
|
|
|
|
title: 'Dummy',
|
|
|
|
contexts: ['image'],
|
|
|
|
onclick: function(data, tab) {
|
|
|
|
return handleMenuClick(
|
|
|
|
CAMLISTORE, 'my album', 'http://localhost:8080', data, tab);
|
|
|
|
}
|
|
|
|
});
|
2010-12-02 02:41:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-12-02 03:51:14 +00:00
|
|
|
// Upload actions
|
|
|
|
var ALBUM_TYPE_UPLOAD_FUNC = {
|
|
|
|
|
2010-12-02 04:35:13 +00:00
|
|
|
picasa: function(albumId, albumName, dataSrcUrl, dataBuffer, xhrDone) {
|
|
|
|
var builder = new BlobBuilder();
|
|
|
|
builder.append(dataBuffer);
|
|
|
|
|
2010-12-02 03:51:14 +00:00
|
|
|
OAUTH.authorize(function() {
|
|
|
|
OAUTH.sendSignedRequest(
|
|
|
|
'http://picasaweb.google.com/data/feed/api/' +
|
|
|
|
'user/default/albumid/' + albumId,
|
|
|
|
xhrDone,
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'image/png',
|
|
|
|
'Slug': dataSrcUrl
|
|
|
|
},
|
|
|
|
parameters: {
|
|
|
|
alt: 'json'
|
|
|
|
},
|
2010-12-02 04:35:13 +00:00
|
|
|
body: builder.getBlob('image/png')
|
2010-12-02 03:51:14 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2010-12-02 04:35:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
camlistore: function(albumId, albumName, dataSrcUrl, dataBuffer, xhrDone) {
|
|
|
|
//var preUploadXhr = new XMLHttpRequest();
|
|
|
|
//preUploadXhr.open('POST', albumId, true);
|
|
|
|
//preUploadXhr.onreadystatechange = function() {
|
|
|
|
// if (xhr.readyState == XMLHttpRequest.DONE) {
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
//preUploadXhr.send();
|
|
|
|
|
|
|
|
var dataView = new Uint8Array(dataBuffer, 0);
|
|
|
|
console.log("Sha1 is: ", Crypto.SHA1(dataView));
|
2010-12-02 03:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
function handleMenuClick(albumType, albumName, albumId, data, tab) {
|
2010-12-02 02:41:51 +00:00
|
|
|
chrome.pageAction.setTitle({
|
|
|
|
tabId: tab.id,
|
|
|
|
title: 'Clip It Good: Uploading (' + data.srcUrl.substr(0, 100) + ')'
|
|
|
|
});
|
|
|
|
chrome.pageAction.show(tab.id);
|
|
|
|
|
|
|
|
var img = document.createElement('img');
|
|
|
|
img.onload = function() {
|
|
|
|
var canvas = document.createElement('canvas');
|
|
|
|
canvas.width = img.width;
|
|
|
|
canvas.height = img.height;
|
|
|
|
canvas.getContext('2d').drawImage(img, 0, 0);
|
|
|
|
|
|
|
|
var dataUrl = canvas.toDataURL();
|
|
|
|
var dataUrlAdjusted = dataUrl.replace('data:image/png;base64,', '');
|
|
|
|
|
2010-12-02 04:35:13 +00:00
|
|
|
var arrayBuffer = Base64.decode(dataUrlAdjusted).buffer;
|
2010-12-02 02:41:51 +00:00
|
|
|
|
|
|
|
function complete(resp, xhr) {
|
|
|
|
chrome.pageAction.hide(tab.id);
|
|
|
|
if (!(xhr.status >= 200 && xhr.status <= 299)) {
|
|
|
|
alert('Error: Response status = ' + xhr.status +
|
|
|
|
', response body = "' + xhr.responseText + '"');
|
|
|
|
}
|
|
|
|
} // end complete
|
|
|
|
|
2010-12-02 03:51:14 +00:00
|
|
|
var uploadFunc = ALBUM_TYPE_UPLOAD_FUNC[albumType];
|
2010-12-02 04:35:13 +00:00
|
|
|
uploadFunc(albumId, albumName, data.srcUrl, arrayBuffer, complete);
|
2010-12-02 02:41:51 +00:00
|
|
|
} // end onload
|
|
|
|
|
|
|
|
img.src = data.srcUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
$(document).ready( function() {
|
|
|
|
setupMenus();
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
</body>
|
|
|
|
</html>
|