捕获选定内容(包括 html 标签)的书签

发布于 2024-11-06 20:31:02 字数 1098 浏览 2 评论 0原文

我正在构建一个 JS 书签,它允许我捕获用户在浏览器中选择的文本并将其发送到 Web 应用程序。我目前已经检查了几个教程,并有一个如下所示的脚本:

javascript:var t;try {t=((window.getSelection&&window.getSelection())||(document.getSelection&&document.getSelection())||(document.selection&&document.selection.createRange&&document.selection.createRange().text));}catch(e){t="";}alert(t);

为了使它更容易一些,这里的代码以更易读的方式:

javascript:
      var t;
      try {
        t=((window.getSelection&&window.getSelection()) || (document.getSelection&&document.getSelection()) || (document.selection&&document.selection.createRange&&document.selection.createRange().text));
      }catch(e)
      {
        t="";
      }
      alert(t);

我当前的代码抓取所选的文本并发出警报,以便我可以查看捕获的内容。然而,文本的格式对我来说很重要,所以我真正想做的是获取该文本中的任何 HTML。事实上,我认为最好还是弄清楚用户在页面中选择的位置,然后从所选内容的开头和结尾查找,找到最近的 HTML 标签,然后获取其中的内容。 (因为用户无法轻易判断他们是否正在选择 HTML)

我更习惯于使用 JQuery 来进行 DOM 选择,所以目前这有点超出我的理解。 (除非您可以将 JQuery 与书签一起使用...可以吗?)

任何人都可以帮助我吗? (即使只是为我指明正确的方向也很棒,我正在将其作为一个业余爱好学习项目,所以我很高兴自己解决一些问题。)

I'm building a JS bookmarklet which allows me to capture text that a user has selected in their browser and sends it off to a web app. I've currently checked out a couple of tutorials and have a script which looks like this:

javascript:var t;try {t=((window.getSelection&&window.getSelection())||(document.getSelection&&document.getSelection())||(document.selection&&document.selection.createRange&&document.selection.createRange().text));}catch(e){t="";}alert(t);

To make it a bit easier, here's the code in a more readable fashion:

javascript:
      var t;
      try {
        t=((window.getSelection&&window.getSelection()) || (document.getSelection&&document.getSelection()) || (document.selection&&document.selection.createRange&&document.selection.createRange().text));
      }catch(e)
      {
        t="";
      }
      alert(t);

The current code I have grabs the selected text and alerts out so I can see what's been captured. However, the formatting of the text is important to me so what I'd really like to do is grab any HTML in this text too. In fact, I think it might be better yet to work out where the user has selected in the page and look up from the start and back from the end of the selected content to find the nearest HTML tags and then grab what's within that. (as the user can't tell if they're selecting HTML or not easily)

I'm much more used to working with JQuery to do DOM selections so this is a bit over my head at the moment. (Unless you can use JQuery with a bookmarklet... can you?)

Can anyone help me with this? (Even just pointing me in the right direction would be great, I'm doing this as a hobby learning project so I'm happy to figure some stuff out myself.)

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

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

发布评论

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

评论(2

じее 2024-11-13 20:31:02

以下函数将返回一个包含用户选择的 HTML 的字符串:

function getSelectionHtml() {
    var html = "";
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        if (sel.rangeCount) {
            var container = document.createElement("div");
            for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                container.appendChild(sel.getRangeAt(i).cloneContents());
            }
            html = container.innerHTML;
        }
    } else if (typeof document.selection != "undefined") {
        if (document.selection.type == "Text") {
            html = document.selection.createRange().htmlText;
        }
    }
    return html;
}

这是小书签的精简版本:

javascript:(function(){var h="",s,g,c,i;if(window.getSelection){s=window.getSelection();if(s.rangeCount){c=document.createElement("div");for(i=0;i<s.rangeCount;++i){c.appendChild(s.getRangeAt(i).cloneContents());}h=c.innerHTML}}else if((s=document.selection)&&s.type=="Text"){h=s.createRange().htmlText;}alert(h);})()

The following function will return a string containing the HTML of the user's selection:

function getSelectionHtml() {
    var html = "";
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        if (sel.rangeCount) {
            var container = document.createElement("div");
            for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                container.appendChild(sel.getRangeAt(i).cloneContents());
            }
            html = container.innerHTML;
        }
    } else if (typeof document.selection != "undefined") {
        if (document.selection.type == "Text") {
            html = document.selection.createRange().htmlText;
        }
    }
    return html;
}

Here's a cut down version for a bookmarklet:

javascript:(function(){var h="",s,g,c,i;if(window.getSelection){s=window.getSelection();if(s.rangeCount){c=document.createElement("div");for(i=0;i<s.rangeCount;++i){c.appendChild(s.getRangeAt(i).cloneContents());}h=c.innerHTML}}else if((s=document.selection)&&s.type=="Text"){h=s.createRange().htmlText;}alert(h);})()
孤千羽 2024-11-13 20:31:02

将 getSelection().getRangeAt(0).cloneContents() 的结果附加到 div 中,然后获取 div 的innerHTML。

javascript:(function()%7Bvar%20node%3Ddocument.createElement('div')%3Bnode.appendChild(getSelection().getRangeAt(0).cloneContents())%3Balert(node.innerHTML)%3B%7D)()%3B

如果您在 GET 请求中传递标记,则需要首先对其使用encodeURIComponent()。

另请注意,GET 请求可能只接受这么多数据。

Append the results of getSelection().getRangeAt(0).cloneContents() to a div and then get the innerHTML of the div.

javascript:(function()%7Bvar%20node%3Ddocument.createElement('div')%3Bnode.appendChild(getSelection().getRangeAt(0).cloneContents())%3Balert(node.innerHTML)%3B%7D)()%3B

If you pass the markup in a GET request, you'll need to use encodeURIComponent() on it first.

Also note that a GET request might only accept so much data.

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