Chrome Omnibox Special Characters Throw Error

时间秒杀一切 提交于 2019-12-10 23:01:34

问题


I'm writing a basic Chrome Extension to add suggestions in the Omnibox from a JSON feed. Nearly all queries entered will display results as expected in the suggestions dropdown.

However, it seems that whenever an ampersand (&) is returned as part of the description, Chrome throws an error.

The error thrown reads "xmlParseEntityRef: no name(...)" and is called from the parseOmniboxDescription method within Chrome.

Any help with this matter would be greatly appreciated. I'm not sure if this is the only character with that problem or if it is more widespread.


回答1:


The current API for omnibox suggestions requires that they be specified as encoded XML text, not just plain text. Some characters including & will need to be appropriately encoded.

To encode an entire XML string in browser JavaScript, you may do something like this:

function encodeXml(s) {
  var holder = document.createElement('div');
  holder.textContent = s;
  return holder.innerHTML;
}

console.log(encodeXml("Good & Bad > Bad & Good"));
// "Good & Bad > Bad & Good"

If you perform this operation on your text before passing it to the omnibox API, the error should go away.

Per the documentation you can use <url>, <match>, and <dim> to further annotate your result. However, you may want to use a more structured XML-building approach for that, rather than simply concatenating strings. (I don't know if these XML elements have any attributes, but if they do, the approach above may not be adequate for encoding attribute values.)



来源:https://stackoverflow.com/questions/35802159/chrome-omnibox-special-characters-throw-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!