Is there a method for removing the Microsoft Translator widget while still keeping the translation feature intact?

I am currently utilizing the Microsoft Translation Widget to automatically translate a webpage without requiring any user interaction.

Unfortunately, I have been facing an issue where I am unable to remove or hide the widget that keeps popping up on the page because the CSS and JS are loaded directly from Microsoft's script within the widget!

If anyone has a solution or workaround for this problem, I would greatly appreciate the help. I have searched extensively but have not been able to find a resolution so far.

Answer №1

Wow, after spending some time experimenting with it, I've finally managed to achieve the desired outcome.

Although it may not look perfect due to some necessary workarounds, it does the job. Feel free to check out the fiddle to see it in action.

Here are the steps I took:

  1. Firstly, I had to override the default behavior of 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. Next, I created a helper function called removeEvents. This function removes all event listeners from an element.

    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. When creating the script tag according to Microsoft's guidelines:

    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);
    

    We need to add a load event listener to that script, and the code below explains it thoroughly:

    s.addEventListener('load', function() {
      // When the translation changes, the plugin calls the TranslateArray method
      // So, we store the original method in a variable and then 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 have no control over when it will be ready
        // I created a function to wait for it
        waitForTranslation(function() {
          // Once the translation is available
          // We select all elements with a lang attribute 
          // And remove all their mouseover event listeners
          [].forEach.call(d.querySelectorAll('[lang]'), function(item, i) {
            removeEvents(item, 'mouseover');
          });
        });
      }
    
      // This helper function waits for the translation to be ready
      function waitForTranslation(cb) {
        // Since we can't control the translation callback
        // I checked if the Translating label is visible as a workaround
        // The function keeps running until the label is hidden again
        // Then it calls our callback function
        var visible = d.getElementById('FloaterProgressBar').style.visibility;
        if (visible === 'visible') {
          setTimeout(function() {
            waitForTranslation(cb);
          }, 0);
          return;
        }
        cb();
      }
    });
    

Update 1

Upon re-evaluating your question, it appears you want to hide all widgets entirely.

To do so, you need to insert the following code once the translation is retrieved:

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';
  // additional code
});

I've prepared another version of the fiddle for you to demonstrate this new behavior. You can access it here.

Update 2

You can prevent the widget from appearing completely by applying the following CSS code:

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

Furthermore, you can also hide the pre-translated text initially by setting the document.body display to none and revealing it only when the page is fully translated:

(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';
      });
    }
  });
});

Feel free to check out the final version of the fiddle I've crafted here.

Answer №2

In my case, I found the following solution to be effective: in your < style > section, include this class definition

.StyleRTL { visibility: hidden !important }

If you are using the translation widget like this:

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

make sure to add the following lines to your callback function (named TranslationComplete in this example):

function TranslationComplete() {
  Microsoft.Translator.Widget.domTranslator.showHighlight = false;
  Microsoft.Translator.Widget.domTranslator.showTooltips = false;
  document.getElementById('WidgetFloaterPanels').style.visibility = 'hidden';
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Targeting lightgallery.js on dynamically added elements in Javascript: The solution to dynamically add elements to

I am facing a challenge in targeting dynamically added elements to make them work with lightgallery.js. Take a look at the example below: <div id="animated-thumbs" class="page-divs-middle"> <!-- STATIC EXAMPLE --> ...

Using an External JavaScript Library in TypeScript and Angular 4: A Comprehensive Guide

My current project involves implementing Google Login and Jquery in Typescript. I have ensured that the necessary files are included in the project: jquery.min and the import of Google using <script defer src="https://apis.google.com/js/platform.js"> ...

Switching Vue.js from the standalone build to the runtime-only build midway through a project?

Opted for the runtime-only version of Vue.js for a new project. I came across in the documentation that to switch to the standalone version, one must include an alias in webpack like this: resolve: { alias: { 'vue$': 'vue/dist/vue.js& ...

Ways to generate an Angular 7 component

Seeking guidance on creating an angular 7 component. I have forked a jsFiddle at this link: https://jsfiddle.net/gauravshrestha/fdxsywLv/. The chart in the fiddle allows data points to be dragged up and down. My goal is to convert this into a component whe ...

Send the user to the codeigniter controller with a click

Is there a way to redirect a user to a different page when they click on a specific division element? I am looking for guidance on how to redirect the user to CI's new controller from an HTML view. Thank you in advance. ...

Combining Multiple Pie Charts with a Line Chart in Highcharts

Can anyone provide guidance on creating a chart similar to the one shown in the Highcharts library? https://i.sstatic.net/BoX4i.jpg ...

How can the distinctions between two bars be illustrated in high charts?

Is there a way to display different values on a new line between two bar charts using Highcharts plugins? I have created a simple bar chart in Highcharts, but I am looking to show the difference between the two bars on a new line. The concept is illustrate ...

Angular error ReferenceError: $Value is not defined in this context

As a newcomer to AngularJS, I am facing an issue while passing a list from the HTML file to the backend for processing. The error message ReferenceError: $Value is not defined keeps popping up. Within my controller file, I have a function named test. The ...

Error encountered: string literal not closed while attempting to load text using ajax

It appears that many programmers from various languages have encountered this issue. I am using jQuery to fetch AJAX text onto a screen, but firebug is indicating an unterminated string literal with an unhelpful example : $("#content").html("<div cla ...

Leveraging Backbone.js without using client-side JavaScript

Exploring the idea of using Backbone.js and node.js to develop a compact web application. The concept of sharing code between the client and server is quite appealing. The challenge arises when considering how users without JavaScript-enabled browsers (in ...

getting rid of the angular hash symbol and prefix from the anchor tag

I am having difficulty creating a direct anchor link to a website. Whenever I attempt to link to the ID using: where #20841 is my anchor tag. Angular interferes with the URL and changes it to: This functions properly in Chrome and Firefox, but i ...

Obtain the identifier of a div within nested HTML components by utilizing jQuery

How do I retrieve the id of the first div with the class: post, which is "367", using jquery in the given HTML code: <div id="own-posts"> <span class="title">Me</span> <div class="posts_container"> <div class="post"& ...

Experiencing varying outcomes from a single form in Laravel

I am attempting to build an ajax form with Laravel. The interface consists of a table displaying names with buttons next to each name that are enclosed within forms to trigger certain actions. Below is the HTML code: <div style="margin-top: 100px;"> ...

Top method for troubleshooting JavaScript code in Visual Studio 2010

Is there a way to troubleshoot JavaScript code in Visual Studio 2010 for MVC Razor projects? ...

Utilizing an ajax request in ClojureScript

I am a beginner in clojurescript and I want to further my understanding by converting a pre-existing application entirely into clojurescript. However, I am struggling with implementing an ajax call. Does anyone know of any online examples or could provid ...

localStorage is functional on desktop devices; however, it does not work on mobile devices running iOS version 12

After developing a basic Todos application using React, I decided to introduce the use of localStorage to maintain data persistence between page reloads. Below is an overview of how I implemented it: loadStateFromLocalStorage() { for (let key in this.st ...

Is it possible to alter the value of a global variable within a function and switch its value based on a local variable?

Currently, I am facing an issue with changing the value of a global variable dynamically from a function. The variable has a global scope and is also present in the same function as a local variable. However, despite my efforts, I am unable to successful ...

Creating HTML elements using Vue.js

Currently, I am facing an issue while attempting to render a template using the push mutation method. My goal is to push a section component, but instead of seeing the desired template content on my page, I am only getting the raw output of <vsection> ...

The correct method for creating an external JSON file

As I browse through numerous JSON tutorials online, including those on STO, I find myself in a state of confusion when it comes to determining the right approach for writing an external JSON file. I have come across various examples such as: (although Ad ...

Display an error popup if a server issue occurs

I'm considering implementing an Error modal to be displayed in case of a server error upon submitting a panel. I'm contemplating whether the appropriate approach would be within the catch statement? The basic code snippet I currently have is: u ...