如何通过 Firefox 扩展对 JavaScript 中的字符串进行 HTML 编码

发布于 2024-07-13 14:57:24 字数 349 浏览 3 评论 0原文

所以我知道我可以编写自己的 HTML 编码函数,如下所示:

function getHTMLEncode(t) {
    return t.toString().replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
}

但我想知道是否有任何本机工具可用于 XPCOM 组件。 我正在编写一个组件,而不是一个覆盖层,因此我没有 DOM 来执行创建 DOM 元素并设置其 innerHTML 等技巧。

So I know I can write my own HTML-encoding function like this:

function getHTMLEncode(t) {
    return t.toString().replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">");
}

But I was wondering if there were any native facility for this that is available to XPCOM components. I'm writing a component, not an overlay, so I don't have a DOM around to do tricks like creating a DOM element and setting its innerHTML.

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

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

发布评论

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

评论(2

短叹 2024-07-20 14:57:24

答案似乎是否定的 - Firefox 中没有内置函数可以对来自 XPCOM 组件的字符串进行 HTML 编码。

The answer appears to be no - there is no built in function in Firefox to HTML-encode a string from an XPCOM component.

橘寄 2024-07-20 14:57:24

理论上,您可以创建一个 XML 文档,使用它创建一个 HTML div,将其文本内容设置为未编码的字符串并读取其内部 HTML。 请注意,这仅编码 lt、gt 和 amp 字符,而不编码 quot。

In theory you could create an XML document, use that to create an HTML div, set its text content to your unencoded string and read off its innerHTML. Note that this only encodes lt, gt and amp characters, not quot.

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