如何访问contentEditable元素的nsIEditor?

发布于 2024-12-16 14:24:39 字数 694 浏览 2 评论 0 原文

当我的扩展程序可以访问 Firefox 中当前显示的网页的 INPUT(输入 "text")或 TEXTAREA 元素时,我可以使用 < code>QueryInterface 将其转换为 nsIDOMNSEditableElement 类型的 JS 对象,然后我可以使用 .editor 访问关联的 nsIEditor

但是,Firefox 也支持其他可编辑元素,例如,如果任何元素(及其子元素)将 contentEditable 属性设置为 true,则该元素(及其子元素)都是可编辑的。此外,如果将 designMode 设置为 on,则整个文档都可以编辑。无论哪种情况,可编辑元素都不是 nsIDOMNSEditableElement 的实例; nsIDOMNSEditableElementQueryInterface 失败。

由于这些可编辑元素也可以启用拼写检查(如果它们的属性 spellcheck 设置为 true),我假设有一个实例 nsIEditor > 与他们相关。

我如何获得它的参考?

When my extension gets access to an INPUT (type "text") or TEXTAREA element of the currently displayed webpage in Firefox, I can use QueryInterface to cast it to a JS object of type nsIDOMNSEditableElement and then I can access the associated nsIEditor using .editor.

However, Firefox supports other elements to be editable as well, e.g. any element (and its children) is editable if it has the attribute contentEditable set to true. Further a whole document can be editable if it has designMode set to on. In either case, the editable elements are not instances of nsIDOMNSEditableElement; QueryInterface for nsIDOMNSEditableElement fails.

Since those editable elements can also have spell checking enabled (if they have the attribute spellcheck set to true), I assume that there is an instance nsIEditor associated with them.

How would I get a reference to it?

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

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

发布评论

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

评论(1

时间海 2024-12-23 14:24:39

对于可编辑框架(通过 designMode 属性),您可以像这样检索它:

var session = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                    .getInterface(Components.interfaces.nsIWebNavigation)
                    .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                    .getInterface(Components.interfaces.nsIEditingSession);
if (session.windowIsEditable(window)
  editor = session.getEditorForWindow(window);

至于 contentEditable - 您似乎不走运。我找不到从 JavaScript 访问编辑器的方法:nsGenericHTMLElement 类有一个 GetEditor 方法,但只能通过 nsIDOMNSEditableElement 接口访问只有很少的元素实现。 nsIAccessibleEditableText 接口 有一个 linkedEditor属性,但由于某种原因它被标记为 [noscript]contentEditable 是 Gecko 中相对较新的功能,看起来有人忘记让编辑器可用于 JavaScript - 可能值得在 https://bugzilla.mozilla.org/

For editable frames (via designMode property) you can retrieve it like this:

var session = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                    .getInterface(Components.interfaces.nsIWebNavigation)
                    .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                    .getInterface(Components.interfaces.nsIEditingSession);
if (session.windowIsEditable(window)
  editor = session.getEditorForWindow(window);

As to contentEditable - you seem to be out of luck. I couldn't find a way to access the editor from JavaScript: nsGenericHTMLElement class has a GetEditor method but it is only accessible through nsIDOMNSEditableElement interface that only few elements implement. nsIAccessibleEditableText interface has an associatedEditor property but it is marked as [noscript] for some reason. contentEditable is a relatively new feature in Gecko and it looks like somebody forgot to make the editor accessible for JavaScript - probably worth filing a bug at https://bugzilla.mozilla.org/.

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