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

阅读:4006回复:10
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 好像挂了,同样不介绍如何申请。
2019-10-29 11:31
写私信
1楼#
大侠出手太好了,
期待一波本地化的翻译
2019-10-30 23:39
写私信
2楼#
找到个可以像Chrome一样弹出Google翻译工具栏的扩展
https://github.com/andreicristianpetcu/google_translate_this
2019-10-31 16:34
写私信
3楼#
好人好梦:找到个可以像Chrome一样弹出Google翻译工具栏的扩展
https://github.com/andreicristianpetcu/google_translate_this
回到原帖
刚才去装,结果火狐不让装。
2019-10-31 16:37
写私信
4楼#
好人好梦:找到个可以像Chrome一样弹出Google翻译工具栏的扩展
https://github.com/andreicristianpetcu/google_translate_this
回到原帖
装上了但翻译不了呀
2019-10-31 16:40
写私信
5楼#
好人好梦:找到个可以像Chrome一样弹出Google翻译工具栏的扩展
https://github.com/andreicristianpetcu/google_translate_this
回到原帖
可以代替我的小书签了。谢谢啦。
2019-10-31 16:41
写私信
6楼#
好人好梦:找到个可以像Chrome一样弹出Google翻译工具栏的扩展
https://github.com/andreicristianpetcu/google_translate_this
回到原帖
这么好的东西怎么在amd找不到。
2019-10-31 16:56
写私信
7楼#
好人好梦:找到个可以像Chrome一样弹出Google翻译工具栏的扩展
https://github.com/andreicristianpetcu/google_translate_this
回到原帖
经过测试不如我的小书签好用,经常点击后不能跳出工具条
2020-03-26 11:19
写私信
8楼#
貌似在FF73下失效了
2020-03-26 13:28
写私信
9楼#
xmtomcom:貌似在FF73下失效了回到原帖
我这里 Nightly 76 都还能用啊。
2021-03-25 17:30
写私信
10楼#
大神,yandex的API是不是也改收费失效了,最近的翻译都是出错了