阅读:4296回复:10
[UC脚本] FF 内置翻译功能增强脚本。功能详情如代码中注释说明。 location.href.startsWith('chrome://browser/content/browser.x') && (() => { let Translation, TranslationUI; try{ // https://bugzilla.mozilla.org/show_bug.cgi?id=1614750 ({ Translation, TranslationParent: TranslationUI } = ChromeUtils.import('resource:///modules/translation/TranslationParent.jsm')); }catch(ex){ ({ Translation, TranslationUI } = Cu.import('resource:///modules/translation/Translation.jsm', {})); } if(Translation._defaultTargetLanguage === 'zh') return; //无论在使用什么语言包均默认翻译为中文 Translation._defaultTargetLanguage = 'zh'; //阻止翻译通知栏弹出,仅在地址栏显示翻译图标,点击翻译图标后才弹出通知栏。 TranslationUI.prototype.shouldShowInfoBar = () => false; //在翻译栏添加翻译引擎切换按钮 const showTranslationInfoBar = TranslationUI.prototype.showTranslationInfoBar; TranslationUI.prototype.showTranslationInfoBar = function (...args) { const rt = showTranslationInfoBar.apply(this, args); const obtn = rt.querySelector('button.translate-infobar-element.options-menu-button, button[anonid="options"]'); if(!obtn) return rt; const engines = Object.keys(Translation.supportedEngines); const menulist = obtn.ownerDocument.createXULElement('menulist'); for(const name of engines) menulist.appendItem(name, name).setAttribute('accesskey', name.charAt(0)); menulist.addEventListener('command', (event) => { const name = event.target.value; Services.prefs.setCharPref('browser.translation.engine', name); const notification = menulist.closest('notification'); if(!notification.translation.originalShown) notification.showOriginal(); notification.translation.state = 0; notification.translate(); }); menulist.style.width = '6em'; const label = obtn.ownerDocument.createXULElement('label'); label.value = obtn.label.includes('Options') ? 'Engine:' : '翻译引擎:'; const index = engines.indexOf(Services.prefs.getCharPref('browser.translation.engine', engines[0])); menulist.selectedIndex = index < 0 ? 0 : index; obtn.before(label, menulist); return rt; } })(); 内置翻译目前共有三个,分别为 Google、Yandex 和 Bing(Bing 提供的 API 好像挂了,所以实际就两个) 以后可能会集成本地翻译:https://www.cnbeta.com/articles/tech/901309.htm 如何开启内置翻译? 在 about:config 中 设置 browser.translation.detectLanguage 为 true, 设置 browser.translation.ui.show 为 true, 如果你有 Yandex 翻译的 apiKey 在 about:config 中新建字符串类型名称为 browser.translation.yandex.apiKeyOverride 的设置项,值为你申请得到的 Yandex 翻译的 apiKey 使用该翻译引擎时将 browser.translation.engine 的值改为 Yandex(首字母必须大写) 免费申请 Yandex 翻译 apiKey 的地址为 https://tech.yandex.com/keys/get/?service=trnsl Google 翻译的新建字符串类型名称为 browser.translation.google.apiKey 的设置项,值为你申请得到的 Google 翻译的 apiKey 使用该翻译引擎时将 browser.translation.engine 的值改为 Google(首字母必须大写,此为默认值) 由于 Google 翻译 apiKey 为收费的,就不介绍如何申请了。 小声地提醒一下,Google 翻译的 apiKey 可以在 github 白嫖别人代码中泄漏的,为了防止被滥用关键字自己琢磨一下吧。 Bing 翻译的分别新建字符串类型名称为 browser.translation.bing.apiKeyOverride browser.translation.bing.clientIdOverride 的设置项,值为你申请得到的 Bing 翻译的 apiKey 和 clientId 使用该翻译引擎时将 browser.translation.engine 的值改为 Bing(首字母必须大写) 由于 Bing 提供的 API 好像挂了,同样不介绍如何申请。 |
|
1楼#
发布于:2019-10-29 11:31
大侠出手太好了,
期待一波本地化的翻译 |
|
2楼#
发布于:2019-10-30 23:39
找到个可以像Chrome一样弹出Google翻译工具栏的扩展
https://github.com/andreicristianpetcu/google_translate_this |
|
3楼#
发布于:2019-10-31 16:34
好人好梦:找到个可以像Chrome一样弹出Google翻译工具栏的扩展刚才去装,结果火狐不让装。 |
|
4楼#
发布于:2019-10-31 16:37
好人好梦:找到个可以像Chrome一样弹出Google翻译工具栏的扩展装上了但翻译不了呀 |
|
5楼#
发布于:2019-10-31 16:40
好人好梦:找到个可以像Chrome一样弹出Google翻译工具栏的扩展可以代替我的小书签了。谢谢啦。 |
|
6楼#
发布于:2019-10-31 16:41
好人好梦:找到个可以像Chrome一样弹出Google翻译工具栏的扩展这么好的东西怎么在amd找不到。 |
|
7楼#
发布于:2019-10-31 16:56
好人好梦:找到个可以像Chrome一样弹出Google翻译工具栏的扩展经过测试不如我的小书签好用,经常点击后不能跳出工具条 |
|
8楼#
发布于:2020-03-26 11:19
貌似在FF73下失效了
|
|
9楼#
发布于:2020-03-26 13:28
|
|
10楼#
发布于:2021-03-25 17:30
大神,yandex的API是不是也改收费失效了,最近的翻译都是出错了
|
|