1

I'm trying to put together a bookmarklet that can take the current page's URL, title, plus any selected text and then post this as a new task to Asana via the API.

I've created a specific project for these to go in, captured the project id, and have identified the workspace id. Testing these values using curl on the command line works fine:

curl --user MYAPIKEY: https://app.asana.com/api/1.0/workspaces/MYWORKSPACEID/tasks --data-urlencode 'name=Hello World' --data-urlencode 'notes=How are you' --data-urlencode 'projects[0]=MYPROJECTID'

Borrowing from other example javascript bookmarklet's I've put together the following code:

javascript:
apikeyhash='MYBASE64ENCODEDAPIKEYPLUSCOLON';
workspaceid='MYWORKSPACEID';
projectid='MYPROJECTID';
title=document.title;
loc=location.href;
if(document.getSelection) { 
  text=location+'\r\r'+document.getSelection(); }
else {
  text=location;
}
xml = new XMLHttpRequest();
xml.open('POST', 'https://app.asana.com/api/1.0/workspaces/'+workspaceid+'/tasks', false);
xml.setRequestHeader("Authorization", 'Basic '+apikeyhash);
xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xml.send('name='+encodeURIComponent(title)+'&notes='+encodeURIComponent(text)+'&projects%5B0%5D='+projectid);
alert(xml.responseText);

(Where MYBASE64ENCODEDAPIKEYPLUSCOLON is the output from: echo -n 'MYAPIKEY:' | openssl enc -base64)

Using this as bookmarklet (accessed via the bookmark bar in Safari) just results in the current page appearing to refresh, and nothing is added to Asana. I'd appreciate any help in fixing the javascript.

Note: My ideal solution would be to pop up the quick-add-task dialogue from Asana and pre-populate that form (using the credentials of the currently logged on Asana user) but I'm not sure if this is possible. For now simply being able to quickly send the URL, title, plus any selected text to Asana as a new task would be very useful. With only a handful of users, creating custom bookmarklets with each user's API key is acceptable.

4

2 回答 2

0

我怀疑如果您在浏览器中检查 javascript 控制台,您会看到它拒绝了跨站点请求。Asana API 当前不支持CORS(尽管我们计划在未来支持),所以通常的规则适用:您只能向与当前网站相同的 host:port 发出请求。

于 2013-09-09T12:28:50.177 回答
0

相关的,对于其他人,您可能知道,但还有一个非常好的 Chrome 扩展程序(但您必须将 URL 复制粘贴到任务中)。当然,使用 Asana API 自己构建东西也很酷!:)

此外,我还构建了一个小的 Asana Macbook Touch Bar 按钮来快速创建任务,但这当然只有当你在 Mac 上并且它不会自动输入 URL 时,它更适用于任何类型的任务。此时此刻:

视频:https ://www.youtube.com/watch?v=K4tDfE_i1BQ

说明:https ://github.com/danieliversen/asana-touchbar-helper

于 2019-05-24T00:23:25.187 回答