此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.greasyfork.org/scripts/452362/1281581/Baidu%20Translate.js
您需要先安装一款用户样式管理器扩展(如 Stylus)后才能安装此样式。
您需要先安装一款用户样式管理器扩展(如 Stylus)后才能安装此样式。
您需要先安装一款用户样式管理器扩展(如 Stylus)后才能安装此样式。
您需要先安装一款用户样式管理器扩展后才能安装此样式。
您需要先安装一款用户样式管理器扩展后才能安装此样式。
您需要先安装一款用户样式管理器扩展后才能安装此样式。
(我已经安装了用户样式管理器,让我安装!)
百度翻译API 用户脚本版
无需向百度申请开发者令牌,@require即可使用
用法:
baidu_translate(details): details = {
text: Text you want to translate.
Required argument, no default value.
callback: Callback function when translation succeed,
e.g. function(result) {console.log(result);}.
Argument `result` will be translate result.
Required argument, no default value.
[src]: Source language; e.g. 'zh' for Chinese, 'en' for English, 'ja' for Japanese, ....
Default: Auto detect.
[dst]: Destination language,
Default: 'zh'.
[split]: Text spliting max length, just leave blank if you don't know what this means.
What it is: Baidu has limited that up to 5000 letters can be translated each single API
request, so the translate function will split text by about every ${split} letters, and
translates each of them with a single API request then concatenate the translate result
together again. Don't worry if any sentence to be splited, actually the translate
function splits text with '\n' first, then concatenate the '\n'-splited strings as long
as possible while keeping its length <= ${split}.
Default: 5000
[onerror]: Callback function when translation failed,
e.g. function(reason) {console.log(reason);}.
Argument: `reason` may be anything that causes its failure, just for debugging and do
not use it in production.
Default: function() {}.
[retry]: Times to retry before onerror function being called or an error being thrown,
Default: 3.
}
Or:
baidu_translate(text, src, dst, callback, onerror, split, retry)
Overloads:
baidu_translate(text, src, dst, callback, onerror, split)
baidu_translate(text, dst, callback, onerror, split)
baidu_translate(text, callback, onerror, split)
baidu_translate(text, callback, onerror)
baidu_translate(text, callback)
注意:
百度翻译需要一定时间来进行初始化,请把需要用到baidu_translate的代码放在bdTransReady(callback)
的回调函数中执行。
示例代码:
// ==UserScript==
// @name Baidu translate API test
// @name:zh-CN 百度翻译API测试
// @name:en Baidu translate API test
// @namespace Baidu-API-Test
// @version 0.1
// @description Baidu translate API test script
// @description:zh-CN 百度翻译API测试脚本
// @description:en Baidu translate API test script
// @author PY-DNG
// @license WTFPL - See https://www.wtfpl.net/
// @match https://greasyfork.org/zh-CN/scripts/452362-baidu-translate
// @require https://greasyfork.org/scripts/452362-baidu-translate/code/Baidu%20Translate.js
// @grant GM_xmlhttpRequest
// @connect fanyi.baidu.com
// ==/UserScript==
(function __MAIN__() {
bdTransReady(function() {
baidu_translate({
text: '欢迎来到 Greasy Fork,这里是一个提供用户脚本的网站。',
dst: 'en',
callback: function(result_text) {
console.log(result_text);
},
onerror: function(reason) {
console.log('something unexpected happened');
debugger;
}
});
});
})();
预期结果: