-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
149 lines (129 loc) · 4.04 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) 2015 Chris Yuska. All rights reserved.
// TODO: license
var albumName, albumID = null;
var retry = true;
chrome.storage.sync.get({albumName: null, albumID: null}, function(items) {
albumName = items.albumName;
albumID = items.albumID;
});
chrome.storage.onChanged.addListener(function(changes, areaName) {
if (changes.albumName) {
albumName = changes.albumName.newValue;
}
if (changes.albumID) {
albumID = changes.albumID.newValue;
}
});
/*
* Create context menu for saving image to pre-determined google plus album
*/
chrome.contextMenus.create({
"title": "Save image to G+ Album",
"id": "simpl-image-save",
"type": "normal",
"contexts": ["image"]
});
chrome.contextMenus.onClicked.addListener(function(info, tab) {
retry = true;
if (albumID == null) {
chrome.tabs.create({url: "options.html"});
return;
}
console.log("Saving " + info.srcUrl + " to " + albumName + " album...");
var xhr = new XMLHttpRequest();
xhr.open("GET", info.srcUrl);
xhr.responseType = "blob";
xhr.onload = function(e) {
if (this.status == 200) {
var blob = this.response;
uploadImage(info.srcUrl, blob, tab);
}
};
xhr.send();
});
function uploadImage(imageUrl, imageBlob, tab) {
chrome.identity.getAuthToken({ 'interactive': true }, function(token) {
//var image = new Image();
//image.src = imageUrl;
var fileName = imageUrl.substring(imageUrl.lastIndexOf('/')+1);
var formData = new FormData();
formData.append(fileName, imageBlob);
xhr = new XMLHttpRequest();
xhr.open("POST", "https://picasaweb.google.com/data/feed/api/user/default/albumid/" + albumID + "?alt=json");
xhr.responseType = "json";
xhr.setRequestHeader("Slug", fileName);
xhr.setRequestHeader('Authorization',
'Bearer ' + token);
xhr.upload.onprogress = function(e) {
if (e.lengthComputable) {
chrome.tabs.sendMessage(tab.id, {
state: "PROGRESS",
imageUrl: imageUrl,
fileName: fileName,
progress: Math.round((e.loaded / e.total) * 100)
}, function(response) {
console.log("Upload in progress");
});
}
};
xhr.onload = function () {
if (this.status === 401 && retry) {
// This status may indicate that the cached
// access token was invalid. Retry once with
// a fresh token.
retry = false;
chrome.identity.removeCachedAuthToken(
{ 'token': token },
getTokenAndUpload);
return;
}
chrome.tabs.sendMessage(tab.id, {
state: "COMPLETE",
imageUrl: imageUrl,
fileName: fileName,
uploadStatus: this.status
}, function(response) {
console.log("Upload complete");
});
var viewLink = this.response.entry.link.filter(function(i) { return i.rel == "alternate"; })[0];
if (viewLink) {
chrome.tabs.create({url: viewLink.href});
}
}
xhr.send(imageBlob);
});
}
function imageUploadCallback(error, httpStatus, responseText) {
if (error != null) {
console.log("Error uploading: " + responseText);
}
}
function authenticatedXhr(method, url, callback) {
var retry = true;
function getTokenAndXhr() {
chrome.identity.getAuthToken({/* details */},
function (access_token) {
if (chrome.runtime.lastError) {
callback(chrome.runtime.lastError);
return;
}
var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.setRequestHeader('Authorization',
'Bearer ' + access_token);
xhr.onload = function () {
if (this.status === 401 && retry) {
// This status may indicate that the cached
// access token was invalid. Retry once with
// a fresh token.
retry = false;
chrome.identity.removeCachedAuthToken(
{ 'token': access_token },
getTokenAndXhr);
return;
}
callback(null, this.status, this.responseText);
}
});
}
}