-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bd108dc
Showing
5 changed files
with
482 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>CFBundleIconFile</key> | ||
<string>icon.png</string> | ||
<key>CFBundleIdentifier</key> | ||
<string>com.hopsken.LaunchBar.action.YoudaoTranslate</string> | ||
<key>CFBundleName</key> | ||
<string>Youdao Translate</string> | ||
<key>CFBundleVersion</key> | ||
<string>0.0.1</string> | ||
<key>LBAbbreviation</key> | ||
<string>yd</string> | ||
<key>LBDescription</key> | ||
<dict> | ||
<key>LBAuthor</key> | ||
<string>Hopsken</string> | ||
<key>LBSummary</key> | ||
<string>A quick action to translate word using youdao dict api.</string> | ||
<key>LBTwitter</key> | ||
<string>@hopsken_</string> | ||
<key>LBWebsiteURL</key> | ||
<string>https://blog.hopsken.com</string> | ||
</dict> | ||
<key>LBScripts</key> | ||
<dict> | ||
<key>LBDefaultScript</key> | ||
<dict> | ||
<key>LBAcceptedArgumentTypes</key> | ||
<array> | ||
<string>string</string> | ||
</array> | ||
<key>LBKeepWindowActive</key> | ||
<true/> | ||
<key>LBLiveFeedbackEnabled</key> | ||
<true/> | ||
<key>LBRequiresArgument</key> | ||
<true/> | ||
<key>LBResultType</key> | ||
<string>unknown</string> | ||
<key>LBReturnsResult</key> | ||
<true/> | ||
<key>LBScriptName</key> | ||
<string>default.js</string> | ||
</dict> | ||
</dict> | ||
<key>LBTextInputTitle</key> | ||
<string>Input a word</string> | ||
<key>NSHumanReadableCopyright</key> | ||
<string>Copyright © 2018 hopsken.com</string> | ||
</dict> | ||
</plist> |
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
include('md5.js') | ||
|
||
const API_HOST = 'https://openapi.youdao.com/api?from=auto&to=auto' | ||
|
||
function run(argument) { | ||
runWithString(argument) | ||
} | ||
|
||
function runWithString(argument) { | ||
if (!argument) { | ||
return [{ | ||
title: '请输入' | ||
}] | ||
} | ||
|
||
if (argument.startsWith('~')) { | ||
const actionArgument = argument.substring(1).trim() | ||
return [ | ||
{ | ||
title: '设置为应用 ID', | ||
action: 'setCustomClientId', | ||
actionArgument | ||
}, | ||
{ | ||
title: '设置为应用密钥', | ||
action: 'setCustomApiKey', | ||
actionArgument | ||
} | ||
] | ||
} | ||
|
||
return getTranslations(argument) | ||
} | ||
|
||
/** | ||
* 配置 ClientID 与 API_KEY | ||
* @param argument string | ||
*/ | ||
function setCustomClientId(argument) { | ||
Action.preferences.CLIENT_ID = argument | ||
LaunchBar.alert('成功设置应用 ID!', `应用 ID 为:${argument}`, '确认') | ||
} | ||
function setCustomApiKey(argument) { | ||
Action.preferences.API_KEY = argument | ||
LaunchBar.alert('成功设置应用密钥!', `应用 ID 为:${argument}`, '确认') | ||
} | ||
|
||
/** | ||
* 获取翻译结果 | ||
* @param word string | ||
* @return Obj{ value, subtitle, alwaysShowsSubtitle, quickLookURL} | ||
*/ | ||
function getTranslations(word) { | ||
if (!word || !word.trim()) { | ||
return [{ | ||
title: '请输入' | ||
}] | ||
} | ||
|
||
const result = HTTP.getJSON(getQueryUrl(word)).data | ||
if (!result || result.errorCode !== '0') { | ||
if (result.errorCode === '108') { | ||
return [{ | ||
title: '应用 ID 或应用密钥配置出错,点击查看配置方法', | ||
url: 'https://github.com/hopsken/YoudaoTranslate' | ||
}] | ||
} | ||
return [{ | ||
title: '翻译出错' | ||
}] | ||
} | ||
|
||
let translations = [] | ||
if (result.hasOwnProperty('translation')) { | ||
translations.push(parseTranslation(result.translation)) | ||
} | ||
if (result.hasOwnProperty('basic')) { | ||
translations.push(parseBasic(result.basic)) | ||
} | ||
if (result.hasOwnProperty('web')) { | ||
translations = translations.concat(parseWeb(result.web)) | ||
} | ||
|
||
return translations.map(item => { | ||
return { | ||
title: item.value, | ||
subtitle: item.key || '', | ||
alwaysShowsSubtitle: true, | ||
quickLookURL: `https://youdao.com/w/${word}` | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* 解析 Translation 字段, 释义 | ||
* @param Translation Object | ||
* @return Object | ||
*/ | ||
function parseTranslation(arr) { | ||
return { value: arr[0], key: null } | ||
} | ||
|
||
/** | ||
* 解析 Basic 字段, 基础释义 | ||
* @param Basic Object | ||
* @return Object | ||
*/ | ||
function parseBasic(obj) { | ||
const phonetics = [] | ||
if (obj.hasOwnProperty('phonetic')) { | ||
phonetics.push(`[${obj['phonetic']}]`) | ||
} | ||
if (obj.hasOwnProperty('uk-phonetic')) { | ||
phonetics.push(`[英:${obj['uk-phonetic']}]`) | ||
} | ||
if (obj.hasOwnProperty('us-phonetic')) { | ||
phonetics.push(`[美:${obj['us-phonetic']}]`) | ||
} | ||
|
||
return { value: obj.explains.join('; '), key: phonetics.join(' ') } | ||
} | ||
|
||
/** | ||
* 解析 Web 字段, 网络释义 | ||
* @param Web Object | ||
* @return array | ||
*/ | ||
function parseWeb(arr) { | ||
return arr.map(item => ({ | ||
value: item.value.join('; '), | ||
key: item.key | ||
})) | ||
} | ||
|
||
/** | ||
* 拼接 URL | ||
* @param Query string | ||
* @return string | ||
*/ | ||
function getQueryUrl(query) { | ||
const clientId = Action.preferences.CLIENT_ID || CLIENT_ID | ||
const apiKey = Action.preferences.API_KEY || API_KEY | ||
|
||
const encodedQuery = encodeURIComponent(query) | ||
const salt = Math.round(Math.random() * 100) | ||
const sign = md5(clientId + query + salt + apiKey) | ||
|
||
return `${API_HOST}&q=${encodedQuery}&appKey=${clientId}&salt=${salt}&sign=${sign}` | ||
} |
Oops, something went wrong.