如何从X剪贴板中获取富文本或HTML源代码?例如,如果您从网络浏览器复制一些文本并将其粘贴到kompozer中,则它将粘贴为HTML,并保留了链接等。但是,对于相同的选择,xclip -o只会输出纯文本,其格式与elinks -dump
相似。我想将HTML拉出并插入文本编辑器(特别是vim
)。
我问the same question on superuser.com,因为我希望有一个实用程序来执行此操作,但是我没有得到任何有益的答复。对我来说,X剪贴板API还是一个神秘的野兽。我们非常欢迎您提供有关破解某些东西以获取此信息的任何提示。这些天,我选择的语言是Python,但是几乎所有的东西都可以。
在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
为了补充@rkhayrov's answer,已经存在一个命令:xclip
。或更确切地说,有一个xclip
原为patch to xclip
,但尚未发布。因此,假设像Debian这样的操作系统附带了xclip
的颠覆头(2019 edit:added to xclip
later on in 2010,这些更改最终于2016年发布):
要列出CLIPBOARD选择的目标:
xclip
选择特定目标:
xclip
[version 0.13也可以设置并拥有一个选择($ 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
)。