Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: chrome/browser/resources/translate_internals/translate_internals.js

Issue 14494004: Added the 'Translation Logs' tab to chrome://translate-internals/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: (Rebasing) Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 (function() { 5 (function() {
6 'use strict'; 6 'use strict';
7 7
8 cr.define('cr.translateInternals', function() { 8 cr.define('cr.translateInternals', function() {
9 9
10 /** 10 /**
11 * Initializes UI and sends a message to the browser for 11 * Initializes UI and sends a message to the browser for
12 * initialization. 12 * initialization.
13 */ 13 */
14 function initialize() { 14 function initialize() {
15 cr.ui.decorate('tabbox', cr.ui.TabBox); 15 cr.ui.decorate('tabbox', cr.ui.TabBox);
16 chrome.send('requestInfo'); 16 chrome.send('requestInfo');
17 } 17 }
18 18
19 /** 19 /**
20 * Creates a new LI element with a button to dismiss the item. 20 * Creates a new LI element with a button to dismiss the item.
21 * @param {string} text lable of the LI element. 21 *
22 * @param {Function} func callback when the button is clicked. 22 * @param {string} text The lable of the LI element.
23 * @param {Function} func Callback called when the button is clicked.
23 */ 24 */
24 function createLIWithDismissingButton(text, func) { 25 function createLIWithDismissingButton(text, func) {
25 var span = document.createElement('span'); 26 var span = document.createElement('span');
26 span.textContent = text; 27 span.textContent = text;
27 28
28 var li = document.createElement('li'); 29 var li = document.createElement('li');
29 li.appendChild(span); 30 li.appendChild(span);
30 31
31 var button = document.createElement('button'); 32 var button = document.createElement('button');
32 button.textContent = 'X'; 33 button.textContent = 'X';
33 button.addEventListener('click', function(e) { 34 button.addEventListener('click', function(e) {
34 e.preventDefault(); 35 e.preventDefault();
35 func(); 36 func();
36 }, false); 37 }, false);
37 38
38 li.appendChild(button); 39 li.appendChild(button);
39 return li; 40 return li;
40 } 41 }
41 42
42 /** 43 /**
44 * Formats the language name to a human-readable text. For example, if
45 * |langCode| is 'en', this may return 'en (English)'.
46 *
47 * @param {string} langCode ISO 639 language code.
48 * @return {string} The formatted string.
49 */
50 function formatLanguageCode(langCode) {
51 var key = 'language-' + langCode;
52 if (key in templateData) {
53 var langName = templateData[key];
54 return langCode + ' (' + langName + ')';
55 }
56
57 return langCode;
58 }
59
60 /**
43 * Handles the message of 'prefsUpdated' from the browser. 61 * Handles the message of 'prefsUpdated' from the browser.
44 * @param {Object} detail The object which represents pref values. 62 *
63 * @param {Object} detail the object which represents pref values.
45 */ 64 */
46 function onPrefsUpdated(detail) { 65 function onPrefsUpdated(detail) {
47 var ul; 66 var ul;
48 ul = document.querySelector('#prefs-language-blacklist ul'); 67 ul = document.querySelector('#prefs-language-blacklist ul');
49 ul.innerHTML = ''; 68 ul.innerHTML = '';
50 69
51 if ('translate_language_blacklist' in detail) { 70 if ('translate_language_blacklist' in detail) {
52 var langs = detail['translate_language_blacklist']; 71 var langs = detail['translate_language_blacklist'];
53 72
54 langs.forEach(function(langCode) { 73 langs.forEach(function(langCode) {
55 var langName = templateData['language-' + langCode]; 74 var text = formatLanguageCode(langCode);
56 var text = langCode + ' (' + langName + ')';
57 75
58 var li = createLIWithDismissingButton(text, function() { 76 var li = createLIWithDismissingButton(text, function() {
59 chrome.send('removePrefItem', 77 chrome.send('removePrefItem',
60 ['language_blacklist', langCode]); 78 ['language_blacklist', langCode]);
61 }); 79 });
62 ul.appendChild(li); 80 ul.appendChild(li);
63 }); 81 });
64 } 82 }
65 83
66 ul = document.querySelector('#prefs-site-blacklist ul'); 84 ul = document.querySelector('#prefs-site-blacklist ul');
(...skipping 11 matching lines...) Expand all
78 } 96 }
79 97
80 ul = document.querySelector('#prefs-whitelists ul'); 98 ul = document.querySelector('#prefs-whitelists ul');
81 ul.innerHTML = ''; 99 ul.innerHTML = '';
82 100
83 if ('translate_whitelists' in detail) { 101 if ('translate_whitelists' in detail) {
84 var pairs = detail['translate_whitelists']; 102 var pairs = detail['translate_whitelists'];
85 103
86 Object.keys(pairs).forEach(function(fromLangCode) { 104 Object.keys(pairs).forEach(function(fromLangCode) {
87 var toLangCode = pairs[fromLangCode]; 105 var toLangCode = pairs[fromLangCode];
88 var fromLangName = templateData['language-' + fromLangCode]; 106 var text = formatLanguageCode(fromLangCode) + ' \u2192 ' +
89 var toLangName = templateData['language-' + toLangCode]; 107 formatLanguageCode(toLangCode);
90 var text = fromLangCode + ' (' + fromLangName + ') \u2192 ' +
91 toLangCode + ' (' + toLangName + ')';
92 108
93 var li = createLIWithDismissingButton(text, function() { 109 var li = createLIWithDismissingButton(text, function() {
94 chrome.send('removePrefItem', 110 chrome.send('removePrefItem',
95 ['whitelists', fromLangCode, toLangCode]); 111 ['whitelists', fromLangCode, toLangCode]);
96 }); 112 });
97 ul.appendChild(li); 113 ul.appendChild(li);
98 }); 114 });
99 } 115 }
100 116
101 var p = document.querySelector('#prefs-dump p'); 117 var p = document.querySelector('#prefs-dump p');
102 var content = JSON.stringify(detail, null, 2); 118 var content = JSON.stringify(detail, null, 2);
103 p.textContent = content; 119 p.textContent = content;
104 } 120 }
105 121
106 /** 122 /**
123 * Addes '0's to |number| as a string. |width| is length of the string
124 * including '0's.
125 *
126 * @param {string} number The number to be converted into a string.
127 * @param {number} width The width of the returned string.
128 * @return {string} The formatted string.
129 */
130 function padWithZeros(number, width) {
131 var numberStr = number.toString();
132 var restWidth = width - numberStr.length;
133 if (restWidth <= 0)
134 return numberStr;
135
136 return Array(restWidth + 1).join('0') + numberStr;
137 }
138
139 /**
140 * Formats |date| as a Date object into a string. The format is like
141 * '2006-01-02 15:04:05'.
142 *
143 * @param {Date} date Date to be formatted.
144 * @return {string} The formatted string.
145 */
146 function formatDate(date) {
147 var year = date.getFullYear();
148 var month = date.getMonth() + 1;
149 var day = date.getDay();
150 var hour = date.getHours();
151 var minute = date.getMinutes();
152 var second = date.getSeconds();
153
154 var yearStr = padWithZeros(year, 4);
155 var monthStr = padWithZeros(month, 2);
156 var dayStr = padWithZeros(day, 2);
157 var hourStr = padWithZeros(hour, 2);
158 var minuteStr = padWithZeros(minute, 2);
159 var secondStr = padWithZeros(second, 2);
160
161 var str = yearStr + '-' + monthStr + '-' + dayStr + ' ' +
162 hourStr + ':' + minuteStr + ':' + secondStr;
163
164 return str;
165 }
166
167 /**
168 * Returns a new TD element.
169 *
170 * @param {string} content The text content of the element.
171 * @param {string} className The class name of the element.
172 * @return {string} The new TD element.
173 */
174 function createTD(content, className) {
175 var td = document.createElement('td');
176 td.textContent = content;
177 td.className = className;
178 return td;
179 }
180
181 /**
182 * Handles the message of 'languageDetectionInfoAdded' from the
183 * browser.
184 *
185 * @param {Object} detail The object which represents the logs.
186 */
187 function onLanguageDetectionInfoAdded(detail) {
188 var tr = document.createElement('tr');
189
190 var date = new Date(detail['time']);
191 [
192 createTD(formatDate(date), 'detection-logs-time'),
193 createTD(detail['url'], 'detection-logs-url'),
194 createTD(formatLanguageCode(detail['content_language']),
195 'detection-logs-content-language'),
196 createTD(formatLanguageCode(detail['cld_language']),
197 'detection-logs-cld-language'),
198 createTD(detail['is_cld_reliable'],
199 'detection-logs-is-cld-reliable'),
200 createTD(formatLanguageCode(detail['language']),
201 'detection-logs-language'),
202 ].forEach(function(td) {
203 tr.appendChild(td);
204 });
205
206 var tbody = $('detection-logs').getElementsByTagName('tbody')[0];
207 tbody.appendChild(tr);
208 }
209
210 /**
107 * The callback entry point from the browser. This function will be 211 * The callback entry point from the browser. This function will be
108 * called by the browser. 212 * called by the browser.
109 * @param {string} message The name of the sent message 213 *
110 * @param {Object} detail The argument of the sent message 214 * @param {string} message The name of the sent message.
215 * @param {Object} detail The argument of the sent message.
111 */ 216 */
112 function messageHandler(message, detail) { 217 function messageHandler(message, detail) {
113 switch (message) { 218 switch (message) {
219 case 'languageDetectionInfoAdded':
220 cr.translateInternals.onLanguageDetectionInfoAdded(detail);
221 break;
114 case 'prefsUpdated': 222 case 'prefsUpdated':
115 cr.translateInternals.onPrefsUpdated(detail); 223 cr.translateInternals.onPrefsUpdated(detail);
116 break; 224 break;
117 default: 225 default:
118 console.error('Unknown message:', message); 226 console.error('Unknown message:', message);
119 break; 227 break;
120 } 228 }
121 } 229 }
122 230
123 return { 231 return {
124 initialize: initialize, 232 initialize: initialize,
125 messageHandler: messageHandler, 233 messageHandler: messageHandler,
126 onPrefsUpdated: onPrefsUpdated 234 onLanguageDetectionInfoAdded: onLanguageDetectionInfoAdded,
235 onPrefsUpdated: onPrefsUpdated,
127 }; 236 };
128 }); 237 });
129 238
130 /** 239 /**
131 * The entry point of the UI. 240 * The entry point of the UI.
132 */ 241 */
133 function main() { 242 function main() {
134 cr.doc.addEventListener('DOMContentLoaded', 243 cr.doc.addEventListener('DOMContentLoaded',
135 cr.translateInternals.initialize); 244 cr.translateInternals.initialize);
136 } 245 }
137 246
138 main(); 247 main();
139 })(); 248 })();
OLDNEW
« no previous file with comments | « chrome/browser/resources/translate_internals/translate_internals.html ('k') | chrome/browser/translate/translate_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698