firefox 插件 - 文本结果 -> XML

发布于 2025-01-01 17:29:33 字数 1035 浏览 0 评论 0原文

我正在开发一个 Firefox 附加组件。目前,一旦它收集了必要的信息并在后面做了一些正则表达式魔法,插件就会在另一个面板中将所有内容吐出为 html 格式。下面是格式化/解析收集的正则表达式数据的代码:

self.on("message", function onMessage(storedHistories) {
var historyList = $('#history-list');
historyList.empty();
storedHistories.forEach(
function(storedHistory) {
    var historyHtml = $('#template .history-details').clone();    
    historyHtml.find('.generator-text').text(storedHistory.generatorText);
    historyHtml.find('.selection-parent-text').html(storedHistory.anchorpText);
    historyList.append(historyHtml);
  });
 });

它将上述结果绑定到的 html 文件如下所示:

 <div id="template">
   <div class="history-details">
     <div class="generator-text"></div>
     <div class="selection-parent-text"></div>    
   </div>
 </div>

有没有办法从 js 中获取文本,并将其输出为 XML?我需要它来显示正确的 XML 容器,并且当其以 HTML 格式格式化时,容器就会消失(我假设它们被视为 HTML 标签)。这可能是一个非常愚蠢的问题,但我对这一切确实是个菜鸟,几乎没有任何 XML/js 经验。我确实在某处读到可以使用 XMLhttpRequest 来做到这一点,但是我不知道从哪里开始以及如何去做。

I'm developing a firefox add-on. Currently, once it has gathered the necessary information and done some regex magic in the back, the addon spits everything out html formatted in another panel. Below is the code that formats/parses the gathered regex data:

self.on("message", function onMessage(storedHistories) {
var historyList = $('#history-list');
historyList.empty();
storedHistories.forEach(
function(storedHistory) {
    var historyHtml = $('#template .history-details').clone();    
    historyHtml.find('.generator-text').text(storedHistory.generatorText);
    historyHtml.find('.selection-parent-text').html(storedHistory.anchorpText);
    historyList.append(historyHtml);
  });
 });

The html file that it binds the above results to looks like this:

 <div id="template">
   <div class="history-details">
     <div class="generator-text"></div>
     <div class="selection-parent-text"></div>    
   </div>
 </div>

Is there a way to take the text from the js, and output it as XML? I need it to show the proper XML containers and when its formatted in HTML, the containers just disappear (I'm assuming they're considered HTML tags). This might be a really dumb question, but I'm a real noob at all of this and have barely any experience with XML/js. I did read somewhere that one could use a XMLhttpRequest to do this, however I have no idea where to start and how to go about this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只有一腔孤勇 2025-01-08 17:29:33

您正确地假设浏览器会将 XML 标签视为元素并仅显示该标签的内容。要让浏览器也显示标签,您需要将 XML 编码为 HTML(例如,< 变为 <)。我能想到的最简单的方法是更改​​,因为您已经在使用 jQuery(并假设storedHistory.generatorText 和storedHistory.anchorpText 是XML 字符串)这两行:

historyHtml.find('.generator-text').text(storedHistory.generatorText);
historyHtml.find('.selection-parent-text').html(storedHistory.anchorpText);

至:

historyHtml.find('.generator-text').text($('<div />').text(storedHistory.generatorText).html());
historyHtml.find('.selection-parent-text').text($('<div />').text(storedHistory.anchorpText).html());

请参阅从输入字段读取属性时 HTML 编码丢失了解更多信息 细节。

希望这有帮助。

You are correct in assuming that the browser will consider an XML tag an element and just display the contents of the tag. To have the browser display the tags as well, you need to encode the XML as HTML (e.g., < becomes <). The easiest way I can think of to do this, since you're already using jQuery (and assuming storedHistory.generatorText and storedHistory.anchorpText are XML strings), is to change these two lines:

historyHtml.find('.generator-text').text(storedHistory.generatorText);
historyHtml.find('.selection-parent-text').html(storedHistory.anchorpText);

to:

historyHtml.find('.generator-text').text($('<div />').text(storedHistory.generatorText).html());
historyHtml.find('.selection-parent-text').text($('<div />').text(storedHistory.anchorpText).html());

See HTML-encoding lost when attribute read from input field for more details.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文