OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // This script contains privileged chrome extension related javascript APIs. | 5 // This script contains privileged chrome extension related javascript APIs. |
6 // It is loaded by pages whose URL has the chrome-extension protocol. | 6 // It is loaded by pages whose URL has the chrome-extension protocol. |
7 | 7 |
8 var chrome = chrome || {}; | 8 var chrome = chrome || {}; |
9 (function() { | 9 (function() { |
10 native function GetExtensionAPIDefinition(); | 10 native function GetExtensionAPIDefinition(); |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 // if there is one. | 278 // if there is one. |
279 var id = arguments[0].menuItemId; | 279 var id = arguments[0].menuItemId; |
280 var onclick = chromeHidden.contextMenus.handlers[id]; | 280 var onclick = chromeHidden.contextMenus.handlers[id]; |
281 if (onclick) { | 281 if (onclick) { |
282 onclick.apply(null, arguments); | 282 onclick.apply(null, arguments); |
283 } | 283 } |
284 }); | 284 }); |
285 }; | 285 }; |
286 } | 286 } |
287 | 287 |
288 // Parses the xml syntax supported by omnibox suggestion results. Returns an | |
289 // object with two properties: 'description', which is just the text content, | |
290 // and 'descriptionStyles', which is an array of style objects in a format | |
291 // understood by the C++ backend. | |
292 function parseOmniboxDescription(input) { | |
293 var domParser = new DOMParser(); | |
294 | |
295 // The XML parser requires a single top-level element, but we want to | |
296 // support things like 'hello, <match>world</match>!'. So we wrap the | |
297 // provided text in generated root level element. | |
298 var root = domParser.parseFromString( | |
299 '<fragment>' + input + '</fragment>', 'text/xml'); | |
300 | |
301 // DOMParser has a terrible error reporting facility. Errors come out nested | |
302 // inside the returned document. | |
303 var error = root.querySelector('parsererror div'); | |
304 if (error) { | |
305 throw new Error(error.textContent); | |
306 } | |
307 | |
308 // Otherwise, it's valid, so build up the result. | |
309 var result = { | |
310 description: '', | |
311 descriptionStyles: [] | |
312 }; | |
313 | |
314 // Recursively walk the tree. | |
315 (function(node) { | |
316 for (var i = 0, child; child = node.childNodes[i]; i++) { | |
317 // Append text nodes to our description. | |
318 if (child.nodeType == Node.TEXT_NODE) { | |
319 result.description += child.nodeValue; | |
320 continue; | |
321 } | |
322 | |
323 // Process and descend into a subset of recognized tags. | |
324 if (child.nodeType == Node.ELEMENT_NODE && | |
325 (child.nodeName == 'dim' || child.nodeName == 'match' || | |
326 child.nodeName == 'url')) { | |
327 var style = { | |
328 'type': child.nodeName, | |
329 'offset': result.description.length | |
330 }; | |
331 result.descriptionStyles.push(style); | |
332 arguments.callee(child); | |
333 style.length = result.description.length - style.offset; | |
334 continue; | |
335 } | |
336 | |
337 // Descend into all other nodes, even if they are unrecognized, for | |
338 // forward compat. | |
339 arguments.callee(child); | |
340 } | |
341 })(root); | |
342 | |
343 return result; | |
344 } | |
345 | |
288 function setupOmniboxEvents(extensionId) { | 346 function setupOmniboxEvents(extensionId) { |
289 chrome.omnibox.onInputChanged.dispatch = | 347 chrome.omnibox.onInputChanged.dispatch = |
290 function(text, requestId) { | 348 function(text, requestId) { |
291 var suggestCallback = function(suggestions) { | 349 var suggestCallback = function(userSuggestions) { |
350 var suggestions = []; | |
351 for (var i = 0; i < userSuggestions.length; i++) { | |
Matt Perry
2010/11/29 22:06:52
maybe we should run the sendSuggestions validator
Aaron Boodman
2010/11/30 01:13:57
Done.
| |
352 var parseResult = parseOmniboxDescription( | |
353 userSuggestions[i].description); | |
354 parseResult.content = userSuggestions[i].content; | |
355 suggestions.push(parseResult); | |
356 } | |
292 chrome.omnibox.sendSuggestions(requestId, suggestions); | 357 chrome.omnibox.sendSuggestions(requestId, suggestions); |
293 }; | 358 }; |
294 chrome.Event.prototype.dispatch.apply(this, [text, suggestCallback]); | 359 chrome.Event.prototype.dispatch.apply(this, [text, suggestCallback]); |
295 }; | 360 }; |
296 } | 361 } |
297 | 362 |
298 chromeHidden.onLoad.addListener(function (extensionId) { | 363 chromeHidden.onLoad.addListener(function (extensionId) { |
299 if (!extensionId) { | 364 if (!extensionId) { |
300 return; | 365 return; |
301 } | 366 } |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
633 apiFunctions["contextMenus.create"].handleRequest = | 698 apiFunctions["contextMenus.create"].handleRequest = |
634 function() { | 699 function() { |
635 var args = arguments; | 700 var args = arguments; |
636 var id = chromeHidden.contextMenus.nextId++; | 701 var id = chromeHidden.contextMenus.nextId++; |
637 args[0].generatedId = id; | 702 args[0].generatedId = id; |
638 sendRequest(this.name, args, this.definition.parameters, | 703 sendRequest(this.name, args, this.definition.parameters, |
639 this.customCallback); | 704 this.customCallback); |
640 return id; | 705 return id; |
641 }; | 706 }; |
642 | 707 |
708 apiFunctions["omnibox.setDefaultSuggestion"].handleRequest = | |
709 function(details) { | |
710 var parseResult = parseOmniboxDescription(details.description); | |
711 sendRequest(this.name, [parseResult], this.definition.parameters); | |
712 }; | |
713 | |
643 apiFunctions["contextMenus.create"].customCallback = | 714 apiFunctions["contextMenus.create"].customCallback = |
644 function(name, request, response) { | 715 function(name, request, response) { |
645 if (chrome.extension.lastError) { | 716 if (chrome.extension.lastError) { |
646 return; | 717 return; |
647 } | 718 } |
648 | 719 |
649 var id = request.args[0].generatedId; | 720 var id = request.args[0].generatedId; |
650 | 721 |
651 // Set up the onclick handler if we were passed one in the request. | 722 // Set up the onclick handler if we were passed one in the request. |
652 var onclick = request.args.length ? request.args[0].onclick : null; | 723 var onclick = request.args.length ? request.args[0].onclick : null; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
696 // the rest of the API. See crbug/29215 . | 767 // the rest of the API. See crbug/29215 . |
697 if (arguments.length == 2 && typeof(arguments[1]) == "function") { | 768 if (arguments.length == 2 && typeof(arguments[1]) == "function") { |
698 // If the old signature is used, add a null details object. | 769 // If the old signature is used, add a null details object. |
699 newArgs = [arguments[0], null, arguments[1]]; | 770 newArgs = [arguments[0], null, arguments[1]]; |
700 } else { | 771 } else { |
701 newArgs = arguments; | 772 newArgs = arguments; |
702 } | 773 } |
703 return newArgs; | 774 return newArgs; |
704 }; | 775 }; |
705 | 776 |
706 apiFunctions["omnibox.styleUrl"].handleRequest = function(offset, length) { | |
707 return {type: "url", offset: offset, length: length}; | |
708 }; | |
709 apiFunctions["omnibox.styleMatch"].handleRequest = | |
710 function(offset, length) { | |
711 return {type: "match", offset: offset, length: length}; | |
712 }; | |
713 apiFunctions["omnibox.styleDim"].handleRequest = function(offset, length) { | |
714 return {type: "dim", offset: offset, length: length}; | |
715 }; | |
716 | |
717 if (chrome.test) { | 777 if (chrome.test) { |
718 chrome.test.getApiDefinitions = GetExtensionAPIDefinition; | 778 chrome.test.getApiDefinitions = GetExtensionAPIDefinition; |
719 } | 779 } |
720 | 780 |
721 setupPageActionEvents(extensionId); | 781 setupPageActionEvents(extensionId); |
722 setupToolstripEvents(GetRenderViewId()); | 782 setupToolstripEvents(GetRenderViewId()); |
723 setupPopupEvents(GetRenderViewId()); | 783 setupPopupEvents(GetRenderViewId()); |
724 setupHiddenContextMenuEvent(extensionId); | 784 setupHiddenContextMenuEvent(extensionId); |
725 setupOmniboxEvents(extensionId); | 785 setupOmniboxEvents(extensionId); |
726 }); | 786 }); |
727 | 787 |
728 if (!chrome.experimental) | 788 if (!chrome.experimental) |
729 chrome.experimental = {}; | 789 chrome.experimental = {}; |
730 | 790 |
731 if (!chrome.experimental.accessibility) | 791 if (!chrome.experimental.accessibility) |
732 chrome.experimental.accessibility = {}; | 792 chrome.experimental.accessibility = {}; |
733 | 793 |
734 if (!chrome.experimental.tts) | 794 if (!chrome.experimental.tts) |
735 chrome.experimental.tts = {}; | 795 chrome.experimental.tts = {}; |
736 })(); | 796 })(); |
OLD | NEW |