Skip to content

Instantly share code, notes, and snippets.

@soffchen
Last active October 18, 2025 20:45
Show Gist options
  • Select an option

  • Save soffchen/5989f7ef4c0cef9f7a4e29254f3fa0e2 to your computer and use it in GitHub Desktop.

Select an option

Save soffchen/5989f7ef4c0cef9f7a4e29254f3fa0e2 to your computer and use it in GitHub Desktop.
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);
@daizy008

Copy link
Copy Markdown

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

@daizy008

Copy link
Copy Markdown

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

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