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

159 lines
4.5 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" 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 ALBUM_TYPE_STRING = {
'picasa': 'Picasa Web Albums'
};
// 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() {
// TODO: Sort the list somehow? Or is it already pre-sorted because
// of the way the albums are presented during configuration?
$.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)
}
});
});
});
});
}
// Upload actions
var ALBUM_TYPE_UPLOAD_FUNC = {
'picasa': function(albumId, albumName, dataSrcUrl, dataBlob, xhrDone) {
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: dataBlob
}
);
});
}
};
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 builder = new BlobBuilder();
builder.append(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,
builder.getBlob('image/png'), complete);
} // end onload
img.src = data.srcUrl;
}
$(document).ready( function() {
setupMenus();
});
</script>
</head>
<body>
</body>
</html>