2022-10-17 23:35:53 -07:00
|
|
|
const reData = new RegExp('^data:(.*?);base64,(.*)$');
|
|
|
|
|
|
2022-10-17 23:43:37 -07:00
|
|
|
async function handleClick(tab, e) {
|
2022-10-18 00:02:09 -07:00
|
|
|
const imgURL = await browser.tabs.captureTab(tab.id);
|
|
|
|
|
const [_, imgType, imgBase64] = imgURL.match(reData);
|
|
|
|
|
const img = atob(imgBase64);
|
|
|
|
|
|
|
|
|
|
const store = {};
|
|
|
|
|
store[`create_${crypto.randomUUID()}`] = {
|
|
|
|
|
title: tab.title,
|
|
|
|
|
url: tab.url,
|
|
|
|
|
imgURL: imgURL,
|
|
|
|
|
};
|
|
|
|
|
await browser.storage.local.set(store);
|
|
|
|
|
|
|
|
|
|
if (!e.modifiers.includes('Command')) {
|
|
|
|
|
browser.tabs.remove([tab.id]);
|
|
|
|
|
}
|
2022-10-17 23:35:53 -07:00
|
|
|
|
2022-10-17 09:58:11 -07:00
|
|
|
const cfg = await browser.storage.sync.get();
|
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-13 11:09:24 -07:00
|
|
|
name: tab.title,
|
|
|
|
|
notes: tab.url,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
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',
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(req),
|
|
|
|
|
},
|
2022-10-17 09:58:11 -07:00
|
|
|
);
|
|
|
|
|
const create = await createResp.json();
|
|
|
|
|
|
2022-10-17 23:35:53 -07:00
|
|
|
const imgBytes = new Uint8Array(img.length);
|
|
|
|
|
for (let i = 0; i < img.length; i++) {
|
|
|
|
|
imgBytes[i] = img.charCodeAt(i);
|
|
|
|
|
}
|
|
|
|
|
const blob = new Blob(
|
|
|
|
|
[imgBytes],
|
|
|
|
|
{
|
|
|
|
|
type: imgType,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const data = new FormData();
|
|
|
|
|
data.append('file', blob, 'screenshot.png');
|
|
|
|
|
|
|
|
|
|
const attachResp = await fetch(
|
|
|
|
|
`https://app.asana.com/api/1.0/tasks/${encodeURIComponent(create['data']['gid'])}/attachments`,
|
|
|
|
|
{
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Authorization': `Bearer ${cfg['token']}`,
|
|
|
|
|
},
|
|
|
|
|
body: data,
|
|
|
|
|
},
|
|
|
|
|
);
|
2022-10-13 11:09:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
browser.browserAction.onClicked.addListener(handleClick);
|