gpt4 book ai didi

javascript - 使用微软翻译时,有没有办法删除小部件,但保留翻译?

转载 作者:行者123 更新时间:2023-11-28 15:17:57 27 4
gpt4 key购买 nike

我正在使用 Microsoft Translation Widget,我想用它来自动翻译网页,而无需用户交互。

问题是,我无法摆脱不断弹出的小部件或将其隐藏在 document.ready 上,因为 CSS 和 JS 是从小部件中的 Microsoft 自己的脚本加载的!

有人知道解决这个问题的方法吗?我到处都找过了,但找不到解决方案。

最佳答案

哇哦,经过一段时间的尝试,我终于实现了你想要的。

由于需要一些解决方法,这有点丑陋,但它有效,take a look at the fiddle .

步骤是:

  1. 首先,我们必须覆盖默认的 addEventListener 行为:

    var addEvent = EventTarget.prototype.addEventListener;
    var events = [];

    EventTarget.prototype.addEventListener = function(type, listener) {
    addEvent.apply(this, [].slice.call(arguments));
    events.push({
    element: this,
    type: type,
    listener: listener
    });
    }
  2. 然后,我们创建一个辅助函数removeEvents。它删除元素的所有事件监听器。

    var removeEvents = function(el, type) {
    var elEvents = events.filter(function(ev) {
    return ev.element === el && (type ? ev.type === type : true);
    });

    for (var i = 0; i < elEvents.length; i++) {
    el.removeEventListener(elEvents[i].type, elEvents[i].listener);
    }
    }
  3. 创建 script 标记时,按照 Microsoft 所说的方式:

    var s = d.createElement('script');
    s.type = 'text/javascript';
    s.charset = 'UTF-8';
    s.src = ((location && location.href && location.href.indexOf('https') == 0) ? 'https://ssl.microsofttranslator.com' : 'http://www.microsofttranslator.com') + '/ajax/v3/WidgetV3.ashx?siteData=ueOIGRSKkd965FeEGM5JtQ**&ctf=True&ui=true&settings=Manual&from=';
    var p = d.getElementsByTagName('head')[0] || d.dElement;
    p.insertBefore(s, p.firstChild);

    我们必须向该脚本添加一个load事件监听器,并且下面的代码已完全注释:

    s.addEventListener('load', function() {
    // when someone changes the translation, the plugin calls the method TranslateArray
    // then, we save the original method in a variable, and we override it
    var translate = Microsoft.Translator.TranslateArray;

    Microsoft.Translator.TranslateArray = function() {
    // we call the original method
    translate.apply(this, [].slice.call(arguments));

    // since the translation is not immediately available
    // and we don't have control when it will be
    // I've created a helper function to wait for it
    waitForTranslation(function() {
    // as soon as it is available
    // we get all the elements with an attribute lang
    [].forEach.call(d.querySelectorAll('[lang]'), function(item, i) {
    // and we remove all the mouseover event listeners of them
    removeEvents(item, 'mouseover');
    });
    });
    }

    // this is the helper function which waits for the translation
    function waitForTranslation(cb) {
    // since we don't have control over the translation callback
    // the workaround was to see if the Translating label is visible
    // we keep calling the function, until it's hidden again
    // and then we call our callback
    var visible = d.getElementById('FloaterProgressBar').style.visibility;
    if (visible === 'visible') {
    setTimeout(function() {
    waitForTranslation(cb);
    }, 0);
    return;
    }
    cb();
    }
    });
<小时/>

更新1

重新阅读您的问题后,您似乎想隐藏所有小部件。

因此,您必须在获得翻译后立即添加以下代码:

waitForTranslation(function() {
document.getElementById('MicrosoftTranslatorWidget').style.display = 'none';
document.getElementById('WidgetLauncher').style.display = 'none';
document.getElementById('LauncherTranslatePhrase').style.display = 'none';
document.getElementById('TranslateSpan').style.display = 'none';
document.getElementById('LauncherLogo').style.display = 'none';
document.getElementById('WidgetFloaterPanels').style.display = 'none';
// rest of the code
});

我创建了another fiddle for you ,显示新行为。

更新2

您可以通过添加以下 CSS 代码来阻止小部件显示:

#MicrosoftTranslatorWidget, #WidgetLauncher, #LauncherTranslatePhrase, #TranslateSpan, #LauncherLogo, #WidgetFloaterPanels {
opacity: 0!important;
}

您甚至可以通过默认隐藏 document.body 来阻止显示翻译前的文本,然后在页面完全翻译时显示它:

(function(w, d) {
document.body.style.display = 'none';
/* (...) */

s.addEventListener('load', function() {
var translate = Microsoft.Translator.TranslateArray;

Microsoft.Translator.TranslateArray = function() {
translate.apply(this, [].slice.call(arguments));

waitForTranslation(function() {
/* (...) */
document.body.style.display = 'block';
});
}
});
});

看看at the final fiddle I've created .

关于javascript - 使用微软翻译时,有没有办法删除小部件,但保留翻译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32785525/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com