perkeep/clients/chrome/clip-it-good/background.html

185 lines
5.3 KiB
HTML
Raw Normal View History

<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>
<script type="text/javascript" src="Crypto.js"></script>
<script type="text/javascript" src="SHA1.js"></script>
<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';
var CAMLISTORE = 'camlistore';
var ALBUM_TYPE_STRING = {
picasa: 'Picasa Web Albums',
camlistore: 'Camlistore'
};
// 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(
albumType, albumDict.name, albumDict.id, data, tab);
}
});
});
});
// 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);
}
});
});
}
// Upload actions
var ALBUM_TYPE_UPLOAD_FUNC = {
picasa: function(albumId, albumName, dataSrcUrl, dataBuffer, xhrDone) {
var builder = new BlobBuilder();
builder.append(dataBuffer);
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'
},
body: builder.getBlob('image/png')
}
);
});
},
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));
}
};
function handleMenuClick(albumType, albumName, albumId, data, tab) {
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,', '');
var arrayBuffer = Base64.decode(dataUrlAdjusted).buffer;
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
var uploadFunc = ALBUM_TYPE_UPLOAD_FUNC[albumType];
uploadFunc(albumId, albumName, data.srcUrl, arrayBuffer, complete);
} // end onload
img.src = data.srcUrl;
}
$(document).ready( function() {
setupMenus();
});
</script>
</head>
<body>
</body>
</html>