检索所选文本的 html 代码
Retrieve html code of selected text
在 mozilla 中,我可以 select 文本并使用 contentWindow.getSelection() 打印 selected 文本。但我正在尝试获取此 selected 文本的基础 html 代码块。有什么方法可以取回它吗?
我需要在用户 select 的任何可点击文本下方提取 url 和其他信息,如 src 等。我需要它的父节点的代码块。
谢谢。
在你做之前有没有鼠标事件侦听器之类的东西contentWindow.getSelection?
如果这样做,您可以通过以下方式获取选定的节点:
function onMouseUp(event) {
var aWindow = event.target.ownerDocument.defaultView;
// should test if aWindow is chrome area or actually content area
var contentWindow = aWindow.document instanceof Ci.nsIHTMLDocument ? aWindow : null; // i guessed here but testing if its content window is done in some similar way
if (!contentWindow) { return }
// do contentWindow.getSelection im not familiar with the code, if selection exists // check if more then one range selected then get node for each, however im going to assume only one range is selected
var nodeOfFirstRange = event.explicitOriginalTarget
var elementOfNode = nodeOfFirstRange.parentNode;
var htmlOfElement = elementOfNode.innerHTML;
}
Services.wm.getMostRecentWindow('navigator:browser').gBrowser.addEventListener('mouseup');
此代码的问题是,如果用户在内容 window 中按下鼠标,然后在将鼠标悬停在内容 window 之外时突出显示并向上移动鼠标,例如 chrome window甚至在浏览器之外(比如如果浏览器 window 不是最大的,或者如果用户在 os 的任务栏中使用 mousedup 等)所以只需使用此代码作为指南
检索 HTML 应该相对容易,但这取决于您想要什么。 window.getSelection()
returns a selection object。您可以使用:
例如:
let selection = contentWindow.getSelection();
let firstElement = selection.anchorNode;
let lastElement = selection.focusNode;
获得 nodes/elements 后的操作将取决于您实际想要查找的内容。您没有指定,因此在找到这些节点之后对其进行操作只是对您想要的内容的猜测。例如,您可能只想查找 anchorNode
的 parent,验证它是否包含 focusNode
(firstElement.parentNode.contains(lastElement)
)(如果不包含则继续查找下一个 parent 直到它出现)并使用 parent 的 innerHTML
。或者,也许您想找到包含 focusNode
的 anchorNode
的第一个 parent 元素,然后使用 TreeWalker 遍历 DOM 树,直到找到找到 anchorNode
并开始累积 HTML 直到遇到 focusNode
.
在 mozilla 中,我可以 select 文本并使用 contentWindow.getSelection() 打印 selected 文本。但我正在尝试获取此 selected 文本的基础 html 代码块。有什么方法可以取回它吗?
我需要在用户 select 的任何可点击文本下方提取 url 和其他信息,如 src 等。我需要它的父节点的代码块。
谢谢。
在你做之前有没有鼠标事件侦听器之类的东西contentWindow.getSelection?
如果这样做,您可以通过以下方式获取选定的节点:
function onMouseUp(event) {
var aWindow = event.target.ownerDocument.defaultView;
// should test if aWindow is chrome area or actually content area
var contentWindow = aWindow.document instanceof Ci.nsIHTMLDocument ? aWindow : null; // i guessed here but testing if its content window is done in some similar way
if (!contentWindow) { return }
// do contentWindow.getSelection im not familiar with the code, if selection exists // check if more then one range selected then get node for each, however im going to assume only one range is selected
var nodeOfFirstRange = event.explicitOriginalTarget
var elementOfNode = nodeOfFirstRange.parentNode;
var htmlOfElement = elementOfNode.innerHTML;
}
Services.wm.getMostRecentWindow('navigator:browser').gBrowser.addEventListener('mouseup');
此代码的问题是,如果用户在内容 window 中按下鼠标,然后在将鼠标悬停在内容 window 之外时突出显示并向上移动鼠标,例如 chrome window甚至在浏览器之外(比如如果浏览器 window 不是最大的,或者如果用户在 os 的任务栏中使用 mousedup 等)所以只需使用此代码作为指南
检索 HTML 应该相对容易,但这取决于您想要什么。 window.getSelection()
returns a selection object。您可以使用:
例如:
let selection = contentWindow.getSelection();
let firstElement = selection.anchorNode;
let lastElement = selection.focusNode;
获得 nodes/elements 后的操作将取决于您实际想要查找的内容。您没有指定,因此在找到这些节点之后对其进行操作只是对您想要的内容的猜测。例如,您可能只想查找 anchorNode
的 parent,验证它是否包含 focusNode
(firstElement.parentNode.contains(lastElement)
)(如果不包含则继续查找下一个 parent 直到它出现)并使用 parent 的 innerHTML
。或者,也许您想找到包含 focusNode
的 anchorNode
的第一个 parent 元素,然后使用 TreeWalker 遍历 DOM 树,直到找到找到 anchorNode
并开始累积 HTML 直到遇到 focusNode
.