浏览器书签拉取和打开存储活动窗口中选定的文本

发布于 2024-10-06 16:09:25 字数 352 浏览 3 评论 0原文

首先,我应该澄清我不是程序员(只是制定即将进行的项目的细节)。所以,如果有些问题听起来很愚蠢,请对我宽容一些。

基本上,我需要创建一个浏览器书签。可以将其拖到用户的状态栏上。所以它应该是这样工作的:当我在任何特定网页上时,我可以从页面中选择一些内容块并单击书签。执行此操作时,小书签应将所有选定的数据保存在我的数据库中。

现在,我想知道的是,如果我还想存储所选文本中的所有格式、超链接等,这是否可行。例如,我在浏览器窗口上选择的网页部分可能包括一些图像、链接、粗体文本和三个段落。我是否可以通过简单地单击书签来捕获所有这些内容?

非常感谢您的帮助。

PS:我尝试在该网站上搜索此信息,但似乎没有答案可以深入研究这个特定问题。

Firstly, I should make it clear that I am not a programmer (merely working out the details of an upcoming project). So please go easy on me if some questions sound silly.

Basically, I have a requirement to create a browser bookmarklet. This can be dragged on to the user's status bar. So this is how it should work : when I am on any particular webpage, I can select some chunk of content from the page and click on the bookmarklet. On doing this, the bookmarklet should save all the selected data in my database.

Now, what I would like to know is if this is possible if I also want to store all formatting, hyperlinks,etc from the selected text. For instance, the portion of webpage I select on a browser window may include some images, links, bold text and three paragraphs. Is it possible for me to capture all of these by a simple click of a bookmarklet?

Thanks a lot for the help.

PS : I tried searching for this information on this website, but no answers seem to delve with this specific problem.

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

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

发布评论

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

评论(1

时光倒影 2024-10-13 16:09:25

您可以通过提取选定的 HTML 来获取一些信息。为了获得CSS格式,你需要一些相当繁重的处理,这超出了我的答案的范围。您还需要将选定的 HTML 发送到服务器,为此您需要使用 Ajax。

Ajax 部分相当简单。有大量关于如何使用 Ajax 将值发布到服务器的教程。但是,您需要注意,它只能在与您发布到的服务器相同域的页面上工作,因此这可能会否定将其作为书签的意义,因为它不能在任何地方使用在网络上。

这是一个获取所有主要浏览器中当前选定的 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;
}

alert(getSelectionHtml());

You can get some of the way by extracting the selected HTML. To get CSS formatting as well, you'd need some fairly heavy processing, which is beyond the scope of my answer. You'll also need to send the selected HTML to the server, for which you'll need to use Ajax.

The Ajax part is fairly simple. There are tons of tutorials around on how to post a value to the server using Ajax. However, you need to be aware that it will only work on a page from the same domain as the server you're posting to, so this may negate the point of doing this as a bookmarklet, since it can't just be used anywhere on the web.

Here's a function that gets the current selected HTML in all major browsers:

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;
}

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