diff options
Diffstat (limited to 'background.js')
| -rw-r--r-- | background.js | 89 | 
1 files changed, 89 insertions, 0 deletions
diff --git a/background.js b/background.js new file mode 100644 index 0000000..ff88355 --- /dev/null +++ b/background.js @@ -0,0 +1,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); + +})();  | 
