修改剪贴板内容以将其视为HTML [英] Modifying a clipboard content to be treated as HTML

查看:153
本文介绍了修改剪贴板内容以将其视为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在网络上复制图像"(通过突出显示图像和ctrl + C),然后将其传递到HTML WYSIWYG编辑器(不是源代码编辑器)的文本视图中时,将显示图片.即使我粘贴了文本编辑器(源代码编辑器),剪贴板的内容也被编辑器理解为html代码.

When I "copy an image" on the web (by highlighting the image and ctrl+C) and then passed it into the text view of the HTML WYSIWYG editor (not the source code editor) the picture is displayed. Even though I paste in the text editor ( source code editor), the content of the clipboard is understood by the editor as an html code.

例如,如果我只是在文本编辑器中粘贴<img src="someURL" />,它将在源代码中添加为<p>&lt;img src="someURL" /&gt;</p>,因此编辑器不会将此剪贴板理解为html代码.

For example, if I simply paste "<img src="someURL" />in the text editor, it will be added in the source code as "<p>&lt;img src="someURL" /&gt;</p>" so this clipboard isn’t understood by the editor as an html code.

那么,即使我将其粘贴到文本视图中(而不是源代码编辑器中),也应该如何修改剪贴板的内容,以使HTML所见即所得编辑器将其理解为html代码?

So how should I modify the content of my clipboard so that an HTML WYSIWYG editor understand it as an html code even though I am pasting it in the text view (not source code editor)?

我想做的更多细节:

当我将图像的URL存储在剪贴板中时,我希望能够将图像添加到HTML WYSIWYG编辑器,而不必切换到源代码编辑器.因此,我想转换剪贴板的内容(通过在URL前后添加一些代码),以便HTML WYSIWYG编辑器将​​其理解为HTML代码(就像上面提到的示例一样).

when I have the URL of an image stored in my clipboard, I want to be able to add the image to the HTML WYSIWYG editor without having to switch to the source code editor. So I would like to transform the content of my clipboard (by adding some code before and after the URL) so it is understood as HTML code (just like the example mentioned above) by my HTML WYSIWYG editor.

为了更好地定位答案,这是我尝试实现的目标.当我使用shareX上传图片时,ShareX会自动将此(私有)可共享URL存储在剪贴板中. https://drive.google.com/open?id=XXXX 此代码将其转换为嵌入式格式.但我仍在寻找一种将其存储为html格式的方法.

to better target the answer here is what I try to achieve. When I use shareX to upload a picture, ShareX store automatically this (private) shareable URL in the clipboard. https://drive.google.com/open?id=XXXX This code convert it in an embedded format. But I'm still looking for a way to store that as html format.

#URL_to_Picture.py 
#
#(xxx=FileID)
#
#You need that kind of URL to be able to embed the picture in an editor:  https://drive.google.com/uc?export=view&id=XXXX
#
#This script does a part of the job by converting Google drive URL into an embedded (and permanent) link:



from jaraco import clipboard
UrlShareX = clipboard.paste_text()
UrlShareX=UrlShareX.replace("https://drive.google.com/file/d/", "")
UrlShareX=UrlShareX.replace("/view?usp=drivesdk", "")
UrlShareX=UrlShareX.replace("/view?usp=sharing", "")
UrlShareX=UrlShareX.replace("https://drive.google.com/open?id=", "")
URL= '<img src="https://drive.google.com/uc?export=view&id={}" />'.format(UrlShareX)
clipboard.copy_html(URL)

要尝试使用ShareX:

To try on ShareX:

  1. 您必须在ShareX菜单中设置对Google云端硬盘的访问权限: 目的地/目的地设置/Google云端硬盘.
  2. 您必须将ShareX菜单设置为:上传任务后"为将URL复制到 剪贴板"
  1. You must set the access to Google drive in ShareX menu: destination/destination settings/google drive.
  2. You must set the ShareX menu: "after upload task" to "copy URL to clipboard"

推荐答案

您可以执行以下操作:

  1. 安装 HtmlClipboard :复制脚本,并将其另存为C:\中的HtmlClipboard.py Python ## \ Lib \ site-packages \
  2. 将以下脚本另存为image_link_as_html.py(我在问题中使用了一些代码):
  3. 在执行脚本的步骤中创建脚本快捷方式(右键单击文件image_link_as_html.py,然后选择创建快捷方式)
  4. 右键单击快捷方式,选择Properties,然后在Shorcut键中添加键盘快捷方式.
  1. Install HtmlClipboard : copy the script, save it as HtmlClipboard.py in C:\Python##\Lib\site-packages\
  2. Save the script below as image_link_as_html.py(I used some of your code in your question):
  3. Create a shorcut for the script in step to (right click on the file image_link_as_html.py, and select create a shorcut)
  4. Right click on the shorcut, select Properties, and and add a keyboard shorcut in Shorcut key.

就是这样.当我们剪贴板中有图像URL时,您只需按键盘快捷键,就可以直接在编辑器的html模式下粘贴图像.

That's it. When you have an image url in our clipboard, you can just press your keyboard shorcut and you can paste your image directly in the html mode of you editor.

image_link_as_html.py(Python34):

image_link_as_html.py (Python34):

from tkinter import Tk
root = Tk()
root.withdraw()
image_url = root.clipboard_get()

# send <img src="https://image_url" alt="" />  to an "HTML format clipboard"
import HtmlClipboard
HtmlClipboard.PutHtml("<img src=\"http://"+image_url+" \" alt=\"\"/>")


要解决有关ShareX的问题,您可以改用此脚本:


To address the part about ShareX, you could use this scrip instead:

from tkinter import Tk
root = Tk()
root.withdraw()
UrlShareX = root.clipboard_get()

# remove everything except the file google ID: this part is not needed 
UrlShareX=UrlShareX.replace("https://drive.google.com/file/d/", "") 
UrlShareX=UrlShareX.replace("/view?usp=drivesdk", "")
UrlShareX=UrlShareX.replace("/view?usp=sharing", "")
UrlShareX=UrlShareX.replace("https://drive.google.com/open?id=", "")
UrlShareX=UrlShareX.replace("/view", "")

# send <img src="https://drive.google.com/uc?export=view&amp;id=xxx " alt="" />  to an "HTML format clipboard"
import HtmlClipboard
HtmlClipboard.PutHtml("<img src=\"https://drive.google.com/uc?export=view&id="+UrlShareX+" \" alt=\"\"/>")

这篇关于修改剪贴板内容以将其视为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆