Files
quickasana/background.js

142 lines
2.8 KiB
JavaScript
Raw Normal View History

2022-10-18 23:06:59 -07:00
'use strict';
const reData = new RegExp('^data:(.*?);base64,(.*)$');
async function handleClick(tab, e) {
2022-10-18 00:02:09 -07:00
const imgURL = await browser.tabs.captureTab(tab.id);
const store = {};
store[`create_${crypto.randomUUID()}`] = {
2022-10-18 23:06:59 -07:00
name: tab.title,
notes: tab.url,
attach: imgURL,
filename: 'screenshot.png',
2022-10-18 00:02:09 -07:00
};
await browser.storage.local.set(store);
if (!e.modifiers.includes('Command')) {
browser.tabs.remove([tab.id]);
}
2022-10-18 23:06:59 -07:00
}
let inHandleChange = false;
async function handleChange(e) {
if (inHandleChange) {
return;
}
try {
inHandleChange = true;
await handleChangeInt(e);
} finally {
inHandleChange = false;
}
}
async function handleChangeInt(e) {
const queue = await browser.storage.local.get();
const keys = Object.getOwnPropertyNames(queue);
if (keys.length == 0) {
return;
}
// If some tasks cause issues, make progress on the others over time
const rand = Math.floor(Math.random() * keys.length);
const key = keys[rand];
const task = queue[key];
2022-10-17 09:58:11 -07:00
const cfg = await browser.storage.sync.get();
2022-10-18 23:06:59 -07:00
const type = key.split('_', 1)[0];
await typeHandlers.get(type)(cfg, task);
await browser.storage.local.remove([key]);
}
async function create(cfg, task) {
2022-10-13 11:09:24 -07:00
const req = {
data: {
2022-10-17 09:58:11 -07:00
workspace: cfg['workspace'],
assignee: cfg['assignee'],
2022-10-18 23:06:59 -07:00
name: task.name,
notes: task.notes,
2022-10-13 11:09:24 -07:00
},
};
2022-10-18 00:02:09 -07:00
const createResp = await fetch(
2022-10-13 11:09:24 -07:00
'https://app.asana.com/api/1.0/tasks',
{
method: 'POST',
headers: {
2022-10-17 09:58:11 -07:00
'Authorization': `Bearer ${cfg['token']}`,
2022-10-13 11:09:24 -07:00
'Content-Type': 'application/json',
'Accept': 'application/json',
},
2022-10-18 23:06:59 -07:00
credentials: 'omit',
2022-10-13 11:09:24 -07:00
body: JSON.stringify(req),
},
2022-10-17 09:58:11 -07:00
);
2022-10-18 23:06:59 -07:00
if (!createResp.ok) {
throw createResp.statusText;
}
2022-10-17 09:58:11 -07:00
const create = await createResp.json();
2022-10-18 23:06:59 -07:00
if (task['attach']) {
const store = {};
store[`attach_${crypto.randomUUID()}`] = {
gid: create['data']['gid'],
attach: task.attach,
filename: task.filename,
};
await browser.storage.local.set(store);
}
}
async function attach(cfg, task) {
const [_, type, base64] = task.attach.match(reData);
const bytes = atob(base64);
const arr = new Uint8Array(bytes.length);
for (let i = 0; i < bytes.length; i++) {
arr[i] = bytes.charCodeAt(i);
}
const blob = new Blob(
2022-10-18 23:06:59 -07:00
[arr],
{
2022-10-18 23:06:59 -07:00
type: type,
},
);
const data = new FormData();
2022-10-18 23:06:59 -07:00
data.append('file', blob, task.filename);
const attachResp = await fetch(
2022-10-18 23:06:59 -07:00
`https://app.asana.com/api/1.0/tasks/${encodeURIComponent(task.gid)}/attachments`,
{
method: 'POST',
headers: {
'Authorization': `Bearer ${cfg['token']}`,
},
2022-10-18 23:06:59 -07:00
credentials: 'omit',
body: data,
},
);
2022-10-18 23:06:59 -07:00
if (!attachResp.ok) {
throw attachResp.statusText;
}
2022-10-13 11:09:24 -07:00
}
2022-10-18 23:06:59 -07:00
const typeHandlers = new Map([
['create', create],
['attach', attach],
]);
2022-10-13 11:09:24 -07:00
browser.browserAction.onClicked.addListener(handleClick);
2022-10-18 23:06:59 -07:00
browser.storage.local.onChanged.addListener(handleChange);
setInterval(handleChange, 10000);