-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpopup.js
67 lines (62 loc) · 1.5 KB
/
popup.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
/**
* Get the URL of the tab that launched this popup.
* @return A Promise that resolves to the URL.
*/
function getURL() {
return new Promise(function(resolve, reject) {
chrome.tabs.query({currentWindow:true, active:true}, function(tabs) {
var url = null;
for (var i = 0, tab; tab = tabs[i]; i++) {
if (tab.url) {
resolve(tab.url);
return;
}
}
reject(Error("Couldn't get active tab URL"));
});
});
}
/**
* Set the popup status content to a string.
* @param text Status string.
*/
function setStatus(text) {
document.body.innerText = text;
}
function setHTML(html) {
document.body.innerHTML = html;
}
/**
* Report an error to the user.
* @param err An Error object.
*/
function logError(err) {
console.log(err);
window.err = err;
setStatus(err.toString() + '. ' +
'See the "err" variable in the JS console for details.');
}
/**
* main popup control flow.
*/
function main() {
if (Auth.isNeeded()) {
setStatus('Authorizing...');
Auth.go().catch(logError);
// The auth flow will open a new tab, which closes this popup.
// There's no reason to try to add a .then() to catch it.
return;
}
getURL()
.then(function(url) {
setStatus('Saving ' + url + '...');
return url;
})
.then(API.add)
.then(function(data) {
setHTML("Saved <a href='" + data.location + "'>" + data.url + "</a>");
setTimeout(function(){window.close()}, 5000);
})
.catch(logError);
}
main();