Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

javascript - Detecting Google Chrome Translation

I've added the Google Translation Bar to our website but due to how the layout works if the translation on the main navigation is longer than English is pushes some links down to the next row and starts to cover up other elements. I've got some Javascript that detects if the translation bar is in use and makes the containing div for the menu and search box wider to compensate, while this does affect the layout it is by far preferable to covering parts of the page.

However Chrome now has translation built in to the browser, if someone uses this they obviously won't be using the embedded version and so I can't detect it to apply my width fix. Is there any way to detect if Chrome's built in translation is being used?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Maybe try using js to detect if menu content has changed and then apply new styles.

UPDATE

When Chrome translates a page it adds several elements to a page:

  • two script elements to head tag
  • global object window.google
  • class = "translated-ltr" to html tag
  • div id="goog-gt-tt" to body tag

You can watch for changes in DOM to find out when content is translated:

document.addEventListener('DOMSubtreeModified', function (e) {
    if(e.target.tagName === 'HTML' && window.google) {
        if(e.target.className.match('translated')) {
            // page has been translated
         } else {
            // page has been translated and translation was canceled
        }
   }
}, true);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...