1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 __gCrWeb['translate'] = {};
9 * The delay a wait performed (in milliseconds) before checking whether the
10 * translation has finished.
13 __gCrWeb.translate.TRANSLATE_STATUS_CHECK_DELAY = 400;
16 * The delay in milliseconds that we'll wait to check if a page has finished
17 * loading before attempting a translation.
20 __gCrWeb.translate.TRANSLATE_LOAD_CHECK_DELAY = 150;
23 * The maximum number of times a check is performed to see if the translate
24 * library injected in the page is ready.
27 __gCrWeb.translate.MAX_TRANSLATE_INIT_CHECK_ATTEMPTS = 5;
29 // The number of times polling for the ready status of the translate script has
31 var translationAttemptCount = 0;
34 * Polls every TRANSLATE_LOAD_CHECK_DELAY milliseconds to check if the translate
35 * script is ready and informs the host when it is.
37 __gCrWeb.translate['checkTranslateReady'] = function() {
38 translationAttemptCount += 1;
39 if (cr.googleTranslate.libReady) {
40 translationAttemptCount = 0;
41 __gCrWeb.message.invokeOnHost({
42 'command': 'translate.ready',
44 'loadTime': cr.googleTranslate.loadTime,
45 'readyTime': cr.googleTranslate.readyTime});
46 } else if (translationAttemptCount >=
47 __gCrWeb.translate.MAX_TRANSLATE_INIT_CHECK_ATTEMPTS) {
48 __gCrWeb.message.invokeOnHost({
49 'command': 'translate.ready',
52 // The translation is still pending, check again later.
53 window.setTimeout(__gCrWeb.translate.checkTranslateReady,
54 __gCrWeb.translate.TRANSLATE_LOAD_CHECK_DELAY);
59 * Polls every TRANSLATE_STATUS_CHECK_DELAY milliseconds to check if translate
60 * is ready and informs the host when it is.
62 __gCrWeb.translate['checkTranslateStatus'] = function() {
63 if (cr.googleTranslate.error) {
64 __gCrWeb.message.invokeOnHost({'command': 'translate.status',
66 } else if (cr.googleTranslate.finished) {
67 __gCrWeb.message.invokeOnHost({
68 'command': 'translate.status',
70 'originalPageLanguage': cr.googleTranslate.sourceLang,
71 'translationTime': cr.googleTranslate.translationTime});
73 // The translation is still pending, check again later.
74 window.setTimeout(__gCrWeb.translate.checkTranslateStatus,
75 __gCrWeb.translate.TRANSLATE_STATUS_CHECK_DELAY);
79 }()); // anonymous function