lonely_8
非常火狐
非常火狐
  • UID30273
  • 注册日期2009-09-03
  • 最后登录2022-08-09
  • 发帖数733
  • 经验469枚
  • 威望0点
  • 贡献值86点
  • 好评度147点
  • 社区居民
  • 忠实会员
阅读:3964回复:10

[UC脚本] FF 内置翻译功能增强脚本。

楼主#
更多 发布于:2019-10-25 23:25

功能详情如代码中注释说明。
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 好像挂了,同样不介绍如何申请。
aunsen
小狐狸
小狐狸
  • UID34416
  • 注册日期2010-11-03
  • 最后登录2024-03-27
  • 发帖数56
  • 经验63枚
  • 威望0点
  • 贡献值32点
  • 好评度4点
  • 社区居民
  • 忠实会员
1楼#
发布于:2019-10-29 11:31
大侠出手太好了,
期待一波本地化的翻译
好人好梦
火狐狸
火狐狸
  • UID26935
  • 注册日期2008-11-06
  • 最后登录2022-11-08
  • 发帖数254
  • 经验89枚
  • 威望0点
  • 贡献值24点
  • 好评度4点
  • 社区居民
2楼#
发布于:2019-10-30 23:39
找到个可以像Chrome一样弹出Google翻译工具栏的扩展
https://github.com/andreicristianpetcu/google_translate_this
marb
非常火狐
非常火狐
  • UID56238
  • 注册日期2017-12-25
  • 最后登录2023-04-12
  • 发帖数894
  • 经验1041枚
  • 威望0点
  • 贡献值1150点
  • 好评度50点
3楼#
发布于:2019-10-31 16:34
好人好梦:找到个可以像Chrome一样弹出Google翻译工具栏的扩展
https://github.com/andreicristianpetcu/google_translate_this
回到原帖
刚才去装,结果火狐不让装。
marb
非常火狐
非常火狐
  • UID56238
  • 注册日期2017-12-25
  • 最后登录2023-04-12
  • 发帖数894
  • 经验1041枚
  • 威望0点
  • 贡献值1150点
  • 好评度50点
4楼#
发布于:2019-10-31 16:37
好人好梦:找到个可以像Chrome一样弹出Google翻译工具栏的扩展
https://github.com/andreicristianpetcu/google_translate_this
回到原帖
装上了但翻译不了呀
marb
非常火狐
非常火狐
  • UID56238
  • 注册日期2017-12-25
  • 最后登录2023-04-12
  • 发帖数894
  • 经验1041枚
  • 威望0点
  • 贡献值1150点
  • 好评度50点
5楼#
发布于:2019-10-31 16:40
好人好梦:找到个可以像Chrome一样弹出Google翻译工具栏的扩展
https://github.com/andreicristianpetcu/google_translate_this
回到原帖
可以代替我的小书签了。谢谢啦。
marb
非常火狐
非常火狐
  • UID56238
  • 注册日期2017-12-25
  • 最后登录2023-04-12
  • 发帖数894
  • 经验1041枚
  • 威望0点
  • 贡献值1150点
  • 好评度50点
6楼#
发布于:2019-10-31 16:41
好人好梦:找到个可以像Chrome一样弹出Google翻译工具栏的扩展
https://github.com/andreicristianpetcu/google_translate_this
回到原帖
这么好的东西怎么在amd找不到。
marb
非常火狐
非常火狐
  • UID56238
  • 注册日期2017-12-25
  • 最后登录2023-04-12
  • 发帖数894
  • 经验1041枚
  • 威望0点
  • 贡献值1150点
  • 好评度50点
7楼#
发布于:2019-10-31 16:56
好人好梦:找到个可以像Chrome一样弹出Google翻译工具栏的扩展
https://github.com/andreicristianpetcu/google_translate_this
回到原帖
经过测试不如我的小书签好用,经常点击后不能跳出工具条
xmtomcom
小狐狸
小狐狸
  • UID57439
  • 注册日期2020-01-20
  • 最后登录2023-01-06
  • 发帖数25
  • 经验33枚
  • 威望0点
  • 贡献值34点
  • 好评度3点
  • 社区居民
8楼#
发布于:2020-03-26 11:19
貌似在FF73下失效了
lonely_8
非常火狐
非常火狐
  • UID30273
  • 注册日期2009-09-03
  • 最后登录2022-08-09
  • 发帖数733
  • 经验469枚
  • 威望0点
  • 贡献值86点
  • 好评度147点
  • 社区居民
  • 忠实会员
9楼#
发布于:2020-03-26 13:28
xmtomcom:貌似在FF73下失效了回到原帖
我这里 Nightly 76 都还能用啊。
xmtomcom
小狐狸
小狐狸
  • UID57439
  • 注册日期2020-01-20
  • 最后登录2023-01-06
  • 发帖数25
  • 经验33枚
  • 威望0点
  • 贡献值34点
  • 好评度3点
  • 社区居民
10楼#
发布于:2021-03-25 17:30
大神,yandex的API是不是也改收费失效了,最近的翻译都是出错了
游客

返回顶部