OLD | NEW |
---|---|
(Empty) | |
1 /** | |
2 * @fileoverview Orchestrates loading of suggestion content in several | |
3 * chrome-search://suggestion iframes. | |
4 */ | |
5 | |
6 /** | |
7 * The origin of the embedding page. | |
8 * Must be in double quotes for proper escaping. | |
9 * @type {string} | |
10 * @const | |
11 */ | |
12 var EMBEDDER_ORIGIN = "%s"; | |
13 | |
14 /** | |
15 * Checks whether a string is a valid color code. | |
16 * @param {string} color A color code. | |
17 * @return {boolean} True if color is a valid color code and false otherwise. | |
18 */ | |
19 function isValidColor(color) { | |
20 // Accept 3 or 6 digit hex colors preceded by '#'. | |
21 return /^#[0-9A-Fa-f]{3}|#[0-9A-Fa-f]{6}$/.test(color); | |
22 } | |
23 | |
24 /** | |
25 * Checks and returns suggestion style. | |
26 * @param {Object} userStyle User-specified overrides for suggestion styles. | |
samarth
2013/04/02 23:26:58
"User" is confusing here. How about pageStyle or
Jered
2013/04/03 18:49:33
Done.
Jered
2013/04/03 18:49:33
Done.
| |
27 * @return {Object} Checked styles or defaults. | |
28 */ | |
29 function getStyle(userStyle) { | |
30 var apiHandle = chrome.embeddedSearch.searchBox; | |
31 var safeStyle = { | |
32 urlColor: '#093', | |
33 titleColor: '#666', | |
34 font: apiHandle.font, | |
samarth
2013/04/02 23:26:58
Any reason we don't want to allow the page to over
Jered
2013/04/03 18:49:33
I was thinking we'd always want suggestions to hav
| |
35 fontSize: apiHandle.fontSize | |
36 }; | |
37 if ('urlColor' in userStyle && isValidColor(userStyle.urlColor)) | |
38 safeStyle.urlColor = userStyle.urlColor; | |
39 if ('titleColor' in userStyle && isValidColor(userStyle.titleColor)) | |
40 safeStyle.titleColor = userStyle.titleColor; | |
41 return safeStyle; | |
42 } | |
43 | |
44 /** | |
45 * Renders a native history suggestion. | |
46 * @param {Document} resultDoc The suggestion template document. | |
47 * @param {Object} suggestion The NativeSuggestion to render. | |
48 * @param {Object} style Checked (not user-set) result style. | |
49 */ | |
50 function updateResult(resultDoc, suggestion, style) { | |
51 resultDoc.body.style.font = style.fontSize + 'px "' + style.font + '";'; | |
52 var urlNode = resultDoc.querySelector('#url'); | |
53 urlNode.textContent = suggestion.destination_url; | |
samarth
2013/04/02 23:26:58
As mentioned in a different comment, this needs to
Jered
2013/04/03 18:49:33
Done.
| |
54 urlNode.style.color = style.urlColor; | |
55 var optionalNode = resultDoc.querySelector('#optional'); | |
56 if (suggestion.description) { | |
57 var titleNode = resultDoc.querySelector('#title'); | |
58 titleNode.textContent = suggestion.description; | |
59 optionalNode.style.color = style.titleColor; | |
60 optionalNode.classList.remove('hide'); | |
61 } else { | |
62 optionalNode.classList.add('hide'); | |
63 } | |
64 } | |
65 | |
66 /** | |
67 * Handles a postMessage from the embedding page requesting to populate history | |
68 * suggestion iframes. | |
69 * @param {Object} message The message. | |
70 */ | |
71 function handleMessage(message) { | |
72 // Only allow messages from the embedding page, which should be an Instant | |
73 // search provider or the local omnibox dropdown (and not e.g. a site which | |
74 // it has iframed.) | |
75 if (message.origin != EMBEDDER_ORIGIN) | |
76 return; | |
77 var apiHandle = chrome.embeddedSearch.searchBox; | |
78 if ('load' in message.data) { | |
79 var loaded = []; | |
80 for (var id in message.data.load) { | |
81 var restrictedId = message.data.load[id]; | |
82 var suggestion = apiHandle.getSuggestionData(restrictedId); | |
83 updateResult(window.parent.frames[id].document, suggestion, | |
84 getStyle(message.data.style || {})); | |
85 loaded.push(id); | |
86 } | |
87 message.source.postMessage( | |
88 {'loaded': message.data.requestId}, | |
89 message.origin); | |
90 } | |
91 } | |
92 | |
93 window.addEventListener('message', handleMessage, false); | |
OLD | NEW |