Baidu Translate

Baidu translate api support for userscripts. No need for baidu's token.

Αυτός ο κώδικας δεν πρέπει να εγκατασταθεί άμεσα. Είναι μια βιβλιοθήκη για άλλους κώδικες που περιλαμβάνεται μέσω της οδηγίας meta // @require https://update.greasyfork.org/scripts/452362/1281581/Baidu%20Translate.js

Δημιουργός
PYUDNG
Έκδοση
0.2.7
Δημιουργήθηκε την
02/10/2022
Ενημερώθηκε την
17/11/2023
Άδεια
MIT

Baidu translate api support for userscripts.
No need for baidu's token.

Usage:

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)

Important Note:
baidu_translate needs to be initialized before using. Don't worry, the initialization is automatic, just call bdTransReady(callback) to get your callback function called while initialized.

e.g.
// ==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;
			}
		});
	});
})();

Expected output:
截屏2022-10-02 下午3.38.03.jpg