Skip to content

Instantly share code, notes, and snippets.

@soffchen

soffchen/alfred.js Secret

Last active May 23, 2021
Embed
What would you like to do?
Yet Another Alfred for iOS Drafts
var input = draft.content;
var action, content, uri, arr;
var key_list = [];
// default action
var default_action = 'gg';
// action List ["ACTION_NAME", "URL_SCHEME", "USE_INTERNAL_BROWSER"]
var action_list = [
["gg", "https://www.google.com.hk/search?q=", 1],
["map", "http://maps.apple.com/?q=", 0],
["app", "itms-apps://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?edia=software&term=", 0],
["url", "", 1],
["tel", "tel://", 0],
["sms", "sms://", 0],
["bing", "http://cn.bing.com/search?q=", 1],
["gm", "comgooglemaps://?q=", 0],
["tb", "taobao://s.taobao.com/?q=", 0],
["jd", "https://so.m.jd.com/ware/search.action?keyword=", 1],
["tweet", "drafts4://x-callback-url/runAction?action=Twitter&text=", 0],
["tweetbot", "tweetbot:///post?text=", 0],
["twitter", "twitter://post?message=", 0],
["ip", "http://ip.cn/?ip=", 1],
["us", "http://itunes.apple.com/us/app/region-chager/id0123456789", 0],
["cn", "http://itunes.apple.com/cn/app/region-chager/id0123456789", 0],
["hk", "http://itunes.apple.com/hk/app/region-chager/id0123456789", 0],
["alipay", "alipays://platformapi/startapp?appId=20000056", 0]
];
for (i = 0; i < action_list.length; i++) {
key_list.push(action_list[i][0]);
}
// parse keyword
if (key_list.indexOf(input.split(' ').shift().toLowerCase()) != -1) {
arr = input.split(' ');
action = arr.shift().toLowerCase();
uri = action_list[key_list.indexOf(action)][1];
content = arr.join(' ');
}
else if (key_list.indexOf(input.split(' ').pop().toLowerCase()) != -1) {
arr = input.split(' ');
action = arr.pop().toLowerCase();
uri = action_list[key_list.indexOf(action)][1];
content = arr.join(' ');
}
else if (key_list.indexOf(input.split('\n').shift().toLowerCase()) != -1) {
arr = input.split('\n');
arr.shift().toLowerCase()
uri = action_list[key_list.indexOf(action)][1];
content = arr.join('\n');
}
else if (key_list.indexOf(input.split('\n').pop().toLowerCase()) != -1) {
arr = input.split('\n');
action = arr.pop().toLowerCase();
uri = action_list[key_list.indexOf(action)][1];
content = arr.join('\n');
}
else {
action = default_action;
uri = action_list[key_list.indexOf(action)][1];
content = input.trim() ? input.trim() : getClipboard();
}
// don't append unnecessary clipboard content
if (content.trim().length === 0) {
if (uri.endsWith('//') || uri.endsWith('=')) {
content = getClipboard().trim();
}
}
if (draft.selectionLength > 0) {
content = draft.content.substr(draft.selectionStart, draft.selectionLength);
}
// url without http://
if (action == 'url') {
uri = content.startsWith('http') ? content : 'http://' + content;
content = '';
}
// parse mobile and phone number
if (action == 'tel' || action == 'sms' || action == 'ip') {
content = content.replace('\u202d', '');
content = content.replace('\u202c', '');
var num = content.match(/(86)?1[3|5|7|8][0-9]\d{4,8}/);
if (!num) {
num = content.match(/(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,8}/);
}
content = num ? num[0] : content;
}
// parse ip address
if (action == 'ip') {
var ip = content.match(/(\d+)\.(\d+)\.(\d+)\.(\d+)/);
content = ip ? ip[0] : content;
}
// use internal browser
if (uri.startsWith('http')) {
if (action_list[key_list.indexOf(action)][2]) {
content = encodeURIComponent(content);
setClipboard(uri + content);
uri = 'drafts4://x-callback-url/runAction?action=URL';
content = '';
}
}
draft.defineTag("uri", uri);
draft.defineTag("content", content);
@soffchen

This comment has been minimized.

Copy link
Owner Author

@soffchen soffchen commented Oct 14, 2017

Yet Another Alfred Action for Drafts 4

Inspired by Power+

Action 创建方法:

1、新建 action,名字图标任君选择;
2、添加 Steps,第一步,运行 JavaScript,把本 gist 的 raw 文本粘贴进去即可;第二步,Open URL,URL TEMPLATE 写 [[uri]]{{[[content]]}},URL encode tag output 关掉,Open in Drafts 关掉,After Success 设置为 Do Nothing;
3、再创建一个名为 URL 的 action,用来在内置浏览器中打开网页,Step 为 Open URL,URL TEMPLATE 为 [[clipboard]],URL encode tag output 关掉,Open in Drafts 打开;
4、没了
5、把 action 放到工具栏会更方便,毕竟咱是通用 action。

使用方法:

1、关键字 + 空格 + 内容
2、内容 + 空格 + 关键字
3、关键字 + 换行 + 大段内容
4、大段内容 + 换行 + 关键字
5、仅输入关键字,执行关键字对应动作 + 剪贴板内容
6、有选中文本,执行关键字对应动作 + 选中文本内容
7、只有文本,无关键字,执行 Google 搜索 + 文本
8、空白内容,执行 Google 搜索 + 剪贴板内容
9、keyword 为 tel, sms, ip 的时候,会自动匹配手机号、座机、IP 地址

演示视频:

Twitter 视频

打赏:

请我喝口咖啡吧

@daizy008

This comment has been minimized.

Copy link

@daizy008 daizy008 commented Oct 16, 2017

奇怪跑起来只能 Google 其他都是 alert

@daizy008

This comment has been minimized.

Copy link

@daizy008 daizy008 commented Oct 18, 2017

看来是 drafts 的bug 。升级后就正常了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment