从 X 剪贴板获取 HTML 源代码或富文本
如何从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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了补充 @rkhayrov 的答案,已经存在一个命令:
xclip
。或者更准确地说,有一个 xclip 的补丁,它是于 2010 年晚些时候添加到xclip
,但尚未'还没有发布,就可以做到这一点。因此,假设您的操作系统(例如 Debian)附带了xclip
的颠覆头(2019 编辑:版本 0.13 进行了这些更改,最终于 2016 年发布(并纳入 Debian 2019 年 1 月)):列出 CLIPBOARD 选择的目标:
选择特定目标:
以及
xclip
还可以设置并拥有一个选择(-i
而不是-o
)。To complement @rkhayrov's answer, there exists a command for that already:
xclip
. Or more exactly, there's a patch toxclip
which was added toxclip
later on in 2010, but hasn't been released yet that does that. So, assuming your OS like Debian ships with the subversion head ofxclip
(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:
To select a particular target:
And
xclip
can also set and own a selection (-i
instead of-o
).在 X11 中,您必须与选择所有者进行通信,询问支持的格式,然后请求特定格式的数据。我认为最简单的方法是使用现有的窗口工具包。例如。使用 Python 和 GTK:
输出将如下所示:
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:
Output will look like this:
扩展 Stephane Chazelas 的想法,您可以:
|
)将该 HTML 放回剪贴板,再次使用相同的xclip
更进一步,您可以将其设为快捷方式,这样您就不必每次都打开终端并运行确切的命令。 ✨
为此:
复制为 HTML
注意:请注意,它与上面的命令相同,但放在内联 Bash 脚本内。为了能够使用
|
(管道)将一个命令的输出作为输入发送到下一个命令,这是必需的。Extending the ideas from Stephane Chazelas, you can:
|
) put that HTML back in the clipboard, again using the samexclip
: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:
Copy as HTML
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.在 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