从 X 剪贴板获取 HTML 源代码或富文本

发布于 2024-09-09 19:34:54 字数 444 浏览 7 评论 0原文

如何从X剪贴板获取富文本或HTML源代码?例如,如果您从 Web 浏览器复制一些文本并将其粘贴到 kompozer 中,它将粘贴为 HTML,并保留链接等。但是,对于相同的选择, xclip -o 仅输出纯文本,并以类似于 elinks -dump 的方式重新格式化。我想将 HTML 拉出并放入文本编辑器(特别是 vim)中。

我在 superuser.com 上问了同样的问题,因为我希望有一个实用程序可以做到这一点,但我没有得到任何信息丰富的答复。 X 剪贴板 API 对我来说仍然是一个神秘的野兽;任何关于破解某些东西以获取此信息的提示都是非常受欢迎的。我现在选择的语言是 Python,但几乎任何语言都可以。

How can rich text or HTML source code be obtained from the X clipboard? For example, if you copy some text from a web browser and paste it into kompozer, it pastes as HTML, with links etc. preserved. However, xclip -o for the same selection just outputs plain text, reformatted in a way similar to that of elinks -dump. I'd like to pull the HTML out and into a text editor (specifically vim).

I asked the same question on superuser.com, because I was hoping there was a utility to do this, but I didn't get any informative responses. The X clipboard API is to me yet a mysterious beast; any tips on hacking something up to pull this information are most welcome. My language of choice these days is Python, but pretty much anything is okay.

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

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

发布评论

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

评论(4

娇女薄笑 2024-09-16 19:34:54

为了补充 @rkhayrov 的答案,已经存在一个命令:xclip。或者更准确地说,有一个 xclip 的补丁,它是于 2010 年晚些时候添加到 xclip,但尚未'还没有发布,就可以做到这一点。因此,假设您的操作系统(例如 Debian)附带了 xclip 的颠覆头(2019 编辑版本 0.13 进行了这些更改,最终于 2016 年发布(并纳入 Debian 2019 年 1 月)):

列出 CLIPBOARD 选择的目标:

$ xclip -selection clipboard -o -t TARGETS
TIMESTAMP
TARGETS
MULTIPLE
SAVE_TARGETS
text/html
text/_moz_htmlcontext
text/_moz_htmlinfo
UTF8_STRING
COMPOUND_TEXT
TEXT
STRING
text/x-moz-url-priv

选择特定目标:

$ xclip -selection clipboard -o -t text/html
 <a href="https://stackoverflow.com/users/200540/rkhayrov" title="3017 reputation" class="comment-user">rkhayrov</a>
$ xclip -selection clipboard -o -t UTF8_STRING
 rkhayrov
$ xclip -selection clipboard -o -t TIMESTAMP
684176350

以及 xclip 还可以设置并拥有一个选择(-i 而不是 -o)。

To complement @rkhayrov's answer, there exists a command for that already: xclip. Or more exactly, there's a patch to xclip which was added to xclip later on in 2010, but hasn't been released yet that does that. So, assuming your OS like Debian ships with the subversion head of xclip (2019 edit: version 0.13 with those changes was eventually released in 2016 (and pulled into Debian in January 2019)):

To list the targets for the CLIPBOARD selection:

$ xclip -selection clipboard -o -t TARGETS
TIMESTAMP
TARGETS
MULTIPLE
SAVE_TARGETS
text/html
text/_moz_htmlcontext
text/_moz_htmlinfo
UTF8_STRING
COMPOUND_TEXT
TEXT
STRING
text/x-moz-url-priv

To select a particular target:

$ xclip -selection clipboard -o -t text/html
 <a href="https://stackoverflow.com/users/200540/rkhayrov" title="3017 reputation" class="comment-user">rkhayrov</a>
$ xclip -selection clipboard -o -t UTF8_STRING
 rkhayrov
$ xclip -selection clipboard -o -t TIMESTAMP
684176350

And xclip can also set and own a selection (-i instead of -o).

海风掠过北极光 2024-09-16 19:34:54

在 X11 中,您必须与选择所有者进行通信,询问支持的格式,然后请求特定格式的数据。我认为最简单的方法是使用现有的窗口工具包。例如。使用 Python 和 GTK:

#!/usr/bin/python

import glib, gtk

def test_clipboard():
    clipboard = gtk.Clipboard()
    targets = clipboard.wait_for_targets()
    print "Targets available:", ", ".join(map(str, targets))
    for target in targets:
        print "Trying '%s'..." % str(target)
        contents = clipboard.wait_for_contents(target)
        if contents:
            print contents.data

def main():
    mainloop = glib.MainLoop()
    def cb():
        test_clipboard()
        mainloop.quit()
    glib.idle_add(cb)
    mainloop.run()

if __name__ == "__main__":
    main()

输出将如下所示:

$ ./clipboard.py 
Targets available: TIMESTAMP, TARGETS, MULTIPLE, text/html, text/_moz_htmlcontext, text/_moz_htmlinfo, UTF8_STRING, COMPOUND_TEXT, TEXT, STRING, text/x-moz-url-priv
...
Trying 'text/html'...
I asked <a href="http://superuser.com/questions/144185/getting-html-source-or-rich-text-from-the-x-clipboard">the same question on superuser.com</a>, because I was hoping there was a utility to do this, but I didn't get any informative responses.
Trying 'text/_moz_htmlcontext'...
<html><body class="question-page"><div class="container"><div id="content"><div id="mainbar"><div id="question"><table><tbody><tr><td class="postcell"><div><div class="post-text"><p></p></div></div></td></tr></tbody></table></div></div></div></div></body></html>
...
Trying 'STRING'...
I asked the same question on superuser.com, because I was hoping there was a utility to do this, but I didn't get any informative responses.
Trying 'text/x-moz-url-priv'...
http://stackoverflow.com/questions/3261379/getting-html-source-or-rich-text-from-the-x-clipboard

In X11 you have to communicate with the selection owner, ask about supported formats, and then request data in the specific format. I think the easiest way to do this is using existing windowing toolkits. E,g. with Python and GTK:

#!/usr/bin/python

import glib, gtk

def test_clipboard():
    clipboard = gtk.Clipboard()
    targets = clipboard.wait_for_targets()
    print "Targets available:", ", ".join(map(str, targets))
    for target in targets:
        print "Trying '%s'..." % str(target)
        contents = clipboard.wait_for_contents(target)
        if contents:
            print contents.data

def main():
    mainloop = glib.MainLoop()
    def cb():
        test_clipboard()
        mainloop.quit()
    glib.idle_add(cb)
    mainloop.run()

if __name__ == "__main__":
    main()

Output will look like this:

$ ./clipboard.py 
Targets available: TIMESTAMP, TARGETS, MULTIPLE, text/html, text/_moz_htmlcontext, text/_moz_htmlinfo, UTF8_STRING, COMPOUND_TEXT, TEXT, STRING, text/x-moz-url-priv
...
Trying 'text/html'...
I asked <a href="http://superuser.com/questions/144185/getting-html-source-or-rich-text-from-the-x-clipboard">the same question on superuser.com</a>, because I was hoping there was a utility to do this, but I didn't get any informative responses.
Trying 'text/_moz_htmlcontext'...
<html><body class="question-page"><div class="container"><div id="content"><div id="mainbar"><div id="question"><table><tbody><tr><td class="postcell"><div><div class="post-text"><p></p></div></div></td></tr></tbody></table></div></div></div></div></body></html>
...
Trying 'STRING'...
I asked the same question on superuser.com, because I was hoping there was a utility to do this, but I didn't get any informative responses.
Trying 'text/x-moz-url-priv'...
http://stackoverflow.com/questions/3261379/getting-html-source-or-rich-text-from-the-x-clipboard
您的好友蓝忘机已上羡 2024-09-16 19:34:54

扩展 Stephane Chazelas 的想法,您可以:

  • 从格式化源复制。
  • 运行此命令从剪贴板中提取内容,转换为 HTML,然后(使用管道 |)将该 HTML 放回剪贴板,再次使用相同的 xclip
xclip -selection clipboard -o -t text/html | xclip -selection clipboard
  • : ,当您使用 Ctrl+v 粘贴时,它将粘贴 HTML 源代码。

更进一步,您可以将其设为快捷方式,这样您就不必每次都打开终端并运行确切的命令。 ✨

为此:

  • 打开操作系统的设置(在我的例子中是 Ubuntu)
  • 找到键盘部分
  • 然后找到快捷方式部分
  • 创建一个新快捷方式
  • 设置一个名称,例如:复制为 HTML
  • 然后作为快捷方式的命令,输入:
bash -c "xclip -selection clipboard -o -t text/html | xclip -selection clipboard"

注意:请注意,它与上面的命令相同,但放在内联 Bash 脚本内。为了能够使用 | (管道)将一个命令的输出作为输入发送到下一个命令,这是必需的。

  • 将快捷方式设置为您想要的任何组合,最好不要覆盖您使用的其他快捷方式。就我而言,我将其设置为: Ctrl+Shift+c

  • 之后,您可以像平常一样复制一些格式化文本: Ctrl+c
  • 然后,在粘贴之前,使用以下命令将其转换为 HTML:Ctrl+Shift+c
  • 接下来,当您使用以下命令粘贴时:Ctrl+v,它将把内容粘贴为 HTML。

Extending the ideas from Stephane Chazelas, you can:

  • Copy from the formatted source.
  • Run this command to extract from the clipboard, convert to HTML, and then (with a pipe |) put that HTML back in the clipboard, again using the same xclip:
xclip -selection clipboard -o -t text/html | xclip -selection clipboard
  • Next, when you paste with Ctrl+v, it will paste the HTML source.

Going further, you can make it a shortcut, so that you don't have to open the terminal and run the exact command each time. ✨

To do that:

  • Open the settings for your OS (in my case it's Ubuntu)
  • Find the section for the Keyboard
  • Then find the section for shortcuts
  • Create a new shortcut
  • Set a Name, e.g.: Copy as HTML
  • Then as the command for the shortcut, put:
bash -c "xclip -selection clipboard -o -t text/html | xclip -selection clipboard"

Note: notice that it's the same command as above, but put inside of an inline Bash script. This is necessary to be able to use the | (pipe) to send the output from one command as input to the next.

  • Set the shortcut to whatever combination you want, preferably not overwriting another shortcut you use. In my case, I set it to: Ctrl+Shift+c

  • After this, you can copy some formatted text as normally with: Ctrl+c
  • And then, before pasting it, convert it to HTML with: Ctrl+Shift+c
  • Next, when you paste it with: Ctrl+v, it will paste the contents as HTML. ????✨

Shortcut edition window

无戏配角 2024-09-16 19:34:54

在 OSX 上,我使用 Christopher Brown 创建的这个很酷的 Swift 工具 MacOS Pastboard

https ://github.com/chbrown/macos-pasteboard

On OSX, I use this cool Swift tool MacOS Pastboard created by Christopher Brown:

https://github.com/chbrown/macos-pasteboard

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