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
|
(async function() {
/*
"init" => what it sounds like, basic setup things
"tumblr" => ok, we have oauth2 creds, now let's authorize with the user!
"ready" => alright! main thing!!
*/
let {system_state, tumblr_expiry_date} = await chrome.storage.local.get({"system_state": "init", "tumblr_expiry_date": 0});
let refreshTimeout = (system_state == "ready") ? setTimeout(refreshToken, Date.now() - tumblr_expiry_date) : null;
async function refreshToken() {
const { tumblr_refresh_token, oauth_consumer_key, oauth_secret_key } = await chrome.storage.local.get(["tumblr_refresh_token", "oauth_consumer_key", "oauth_secret_key"]);
const form = new FormData();
form.append("grant_type", "refresh_token");
form.append("refresh_token", tumblr_refresh_token);
form.append("client_id", oauth_consumer_key);
form.append("client_secret", oauth_secret_key);
const {expires_in, access_token, refresh_token} = await fetch("https://api.tumblr.com/v2/oauth2/token", {body: form, method: "POST"}).then(a => a.json());
const tumblr_expiry_date = expires_in * 1000 + Date.now();
await chrome.storage.local.set({tumblr_expiry_date, tumblr_access_token: access_token, tumblr_refresh_token: refresh_token});
refreshTimeout = setTimeout(refreshToken, expires_in * 1000);
}
async function doNextThing(startup) {
switch (system_state) {
case "init":
await chrome.tabs.create({url: "chrome-extension://"+chrome.runtime.id+"/init.html"});
break;
case "tumblr":
let {oauth_consumer_key} = await chrome.storage.local.get(["oauth_consumer_key"]);
await chrome.tabs.create({url: "https://tumblr.com/oauth2/authorize?response_type=code&scope=basic%20offline_access%20write&state=NA&client_id="+oauth_consumer_key});
}
}
chrome.runtime.onMessage.addListener(async function({id, type, payload}, sender) {
console.debug(`Got message "${type}"@"${id}", payload ${JSON.stringify(payload)}`);
switch (type) {
case "set_creds":
const {oauth_consumer_key, oauth_secret_key} = payload;
clearTimeout(refreshTimeout);
system_state = "tumblr";
await chrome.storage.local.set({system_state, oauth_consumer_key, oauth_secret_key});
doNextThing();
if (sender.tab) {
await chrome.tabs.sendMessage(sender.tab.id, {id, status: "OK"});
}
return;
default:
if (sender.tab) {
chrome.tabs.sendMessage(sender.tab.id, {id, status: "ERRUNDEF", message: "No message type `"+type+"`!"});
}
return;
}
});
chrome.tabs.onUpdated.addListener(async function(tabId, {url}, tab) {
if (system_state == "tumblr" && url?.startsWith("https://example.com/tfm_redirect")) {
const { oauth_consumer_key, oauth_secret_key } = await chrome.storage.local.get(["oauth_consumer_key", "oauth_secret_key"]);
const code = /code=([a-fA-F0-9]+)/.exec(url)[1];
const form = new FormData();
form.append("grant_type", "authorization_code");
form.append("code", code);
form.append("client_id", oauth_consumer_key);
form.append("client_secret", oauth_secret_key);
const {expires_in, access_token, refresh_token} = await fetch("https://api.tumblr.com/v2/oauth2/token", {body: form, method: "POST"}).then(a => a.json());
const tumblr_expiry_date = expires_in * 1000 + Date.now();
await chrome.storage.local.set({system_state: "ready", tumblr_expiry_date, tumblr_access_token: access_token, tumblr_refresh_token: refresh_token});
clearTimeout(refreshTimeout);
refreshTimeout = setTimeout(refreshToken, expires_in * 1000);
await chrome.tabs.update(tabId, {url: "chrome-extension://"+chrome.runtime.id+"/ui.html"});
}
});
doNextThing(true);
})();
|