kazatca / Access token debug

// ==UserScript==
// @name         Access token debug
// @namespace    http://tampermonkey.net/
// @version      0.1
// @author       kazatca
// @require      https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.2/babel.js
// @copyright    2020, kazatca (https://openuserjs.org/users/kazatca)
// @license      MIT
// @match        https://unitedtraders.com/*
// ==/UserScript==

var inline_src = (<><![CDATA[
const widgetId = 'access-token-debug-widget';

const log = [];

function getAccessToken() {
  const token = document.cookie
    .split(';')
    .find(c => c.trim().match(/^access_token/))

  if(token) return token.split('=').pop();

}

function getStats(accessToken, time) {
  const [, payload] = accessToken.split('.');

  const parsed = atob(payload);
  const exp = JSON.parse(parsed).exp * 1000;

  return [
    'access_token: ...' + accessToken.substr(accessToken.length - 10),
    'exp: ' + new Date(exp).toLocaleString(),
    formatTime((exp - time) / 1000)
  ]
}

function padLeft(str, count, fill) {
  str = '' + str;
  return Array(Math.max(0, count - str.length)).fill(fill) + str;
}

function formatTime(value) {
  const sign = value < 0 ? '-' : '';
  value = Math.abs(value);
  const m = Math.floor(value / 60);
  const s = Math.floor(value % 60);

  return sign + padLeft(m, 2, '0') + ':' + padLeft(s, 2, '0');
}

function get(name) {
  return document.querySelector('#' + name);
}

function addWidget() {
  let widget = get(widgetId);
  if (widget) {
    return widget;
  }

  widget = document.createElement('pre');
  widget.style.position = 'fixed';
  widget.style.bottom = '10px';
  widget.style.left = '10px';
  widget.style.color = 'red';
  widget.style.zIndex = '100';
  document.body.appendChild(widget);
  return widget;
}

const widget = addWidget();

function getContent() {
  const accessToken = getAccessToken();

    if(!accessToken) {
      return 'not authorized';
    }

  if(!log.length || log[log.length - 1].accessToken !== accessToken) {
    if(log.length > 0) {
      log[log.length - 1].time = Date.now();
    }
    log.push({accessToken});
  }

  return log.map(({accessToken, time}, i) => {
      return getStats(accessToken, time || Date.now()).join('\n');
  }).join('\n\n');
}

function draw() {
  if (!widget) {
    return;
  }
  widget.innerHTML = getContent();

}

setInterval(draw, 500);

]]></>).toString();
var c = Babel.transform(inline_src, { presets: [ "es2015", "es2016" ] });
eval(c.code);