使用JavaScript和Google Chrome API将剪贴板数据存储到变量中 [英] Storing clipboard data into a variable with JavaScript and the Google Chrome API

查看:758
本文介绍了使用JavaScript和Google Chrome API将剪贴板数据存储到变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以将剪贴板的内容存储在字符串变量中。



以下来源指出,这不可能使用纯JS,但我认为希望Chrome API能够实现。 (我正在开发一个Chrome扩展)。



有一个 clipboardRead 权限,这会让你相信它是可能的,但它的描述只是:


如果扩展名或应用程序使用document.execCommand('paste'),则必需。 来源


澄清:我不是试图将数据复制到剪贴板或从中粘贴数据。我想将剪贴板的内容以任何方式存储到变量中,而不用突变剪贴板内容。



为了进一步说明,我不能假定剪贴板数据恰好位于 info.selectionText code>在操作时。



如果这是不可能的,那么我想我只能忍受它,但是看起来似乎可以通过扩展来实现。




编辑:
我想存储这个值的原因是因为我需要利用剪贴板来执行操作,但是当我完成后我想恢复它的内容,所以用户不会放弃曾经在那里的任何东西。



如果我只是将数据粘贴到文本区域并将其存储,那么当我将数据放回时,用户将丢失它们的任何格式有,这比没有好,但不是最佳。



另外,有没有一种方法可以存储这些数据,即使它不是字符串, (例如图像)?一个允许我这样做的解决方案无疑也隐含地回答了上述问题,但没有必要。我主要是看保存字符串数据,并希望一种方法来保持格式。

解决方案

有一个实验剪贴板API以前,我想他们删除它。您可以将内容粘贴到textarea / contenteditable中并获取它的值:

  function getClipboard(){
var el = document.createElement('textarea');
document.body.appendChild(el);
el.focus();
document.execCommand('paste');
var value = el.value;
document.body.removeChild(el)
返回值;
}
console.log(getClipboard());


I am wondering if it is possible to store the contents of the clipboard in a string variable.

The following source states that this is not possible using pure JS, but I was thinking hoping that the chrome API would make it possible. (I am developing a chrome extension).

There is a clipboardRead permission, which would lead you to believe that it is possible, but it's description is just:

Required if the extension or app uses document.execCommand('paste'). source

To clarify: I am not trying to copy data to the clipboard or paste data from it. I would like to store the contents of the clipboard into a variable without mutating the clipboard contents in any way.

To further clarify, I can not assume that the clipboard data happens to be sitting in info.selectionText at the time of the operation.

If it is not possible, than I will just have to live with it I suppose, but it seems like something that would be possible to do with an extension.


EDIT: The reason I want to store the value is because I need to utilize the clipboard to perform an operation, but I would like to restore its contents when I am done, so the user doesn't loose whatever used to be in there.

If I just paste the data into a text area and store it from there, then, when I go to put the data back, the user will loose any formatting that they had, which is much better than nothing, but is not optimal.

Also, is there a way that I could store that data, even if it is not a string, (an image for example)? A solution that allows me to do this would undoubtedly implicitly answer the above question as well, but is not necessary. I am primarily looking save string data and would prefer a way to keep the formatting.

解决方案

There was an experimental clipboard api a while ago, I guess they removed it though. You could always paste the contents into a textarea/contenteditable and get it's value:

function getClipboard() {    
    var el = document.createElement('textarea');
    document.body.appendChild(el);
    el.focus();
    document.execCommand('paste');
    var value = el.value;
    document.body.removeChild(el)
    return value;
}
console.log(getClipboard());

这篇关于使用JavaScript和Google Chrome API将剪贴板数据存储到变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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