使用Microsoft Translator时,有没有办法删除小部件,但保留翻译?

时间:2015-09-25 15:22:37

标签: javascript ajax microsoft-translator

我正在使用微软翻译小工具,我想用它来自动翻译网页而无需用户互动。

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

有没有人知道解决这个问题的方法?我到处寻找,无法找到解决方案。

2 个答案:

答案 0 :(得分:4)

哇,经过一段时间的游戏,我终于实现了你想要的目标。

由于某些需要的解决方法,它很难看,但它有效,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标记时,就像微软所说的那样:

    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添加script事件监听器,并且下面的代码已完全注释:

    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();
      }
    });
    
  4. 更新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

答案 1 :(得分:0)

对我来说,这是解决方案: 在你的&lt;风格&gt;部分添加此类

.LTRStyle { display: none !important }

此外,如果您以这种方式调用翻译小部件:

Microsoft.Translator.Widget.Translate('en', lang, null, null, TranslationDone, null, 3000);

然后将此添加到您的回调(在此示例中为TranslationDone)函数:

function TranslationDone() {
  Microsoft.Translator.Widget.domTranslator.showHighlight = false;
  Microsoft.Translator.Widget.domTranslator.showTooltips = false;
  document.getElementById('WidgetFloaterPanels').style.display = 'none';
};