javascript 如何阅读谷歌浏览器扩展中的剪贴板文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8509670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:39:14  来源:igfitidea点击:

How to read the Clipboard text in google chrome extension

javascriptgoogle-chromegoogle-chrome-extensiongoogle-chrome-devtools

提问by Exception

I am trying to read clipboard text in Google chrome extension. As of now I have tried with tis code and it is returning me undefined. Please help me on this.

我正在尝试阅读谷歌浏览器扩展中的剪贴板文本。截至目前,我已尝试使用 tis 代码,但它返回给我未定义。请帮我解决这个问题。

In background.html my code is

在 background.html 我的代码是

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
 if (request.method == "getClipData")
   sendResponse({data: document.execCommand('paste')});
 else
   sendResponse({}); // snub them.
 });

In My content script my code is

在我的内容脚本中,我的代码是

chrome.extension.sendRequest({method: "getClipData"}, function(response) {
   alert(response.data);
});

采纳答案by hamczu

Once upon a time there was an experimental API chrome.experimental.clipboard, but there is no more http://code.google.com/chrome/extensions/trunk/experimental.clipboard.html

从前有一个实验性的 API chrome.experimental.clipboard,但现在没有了http://code.google.com/chrome/extensions/trunk/experimental.clipboard.html

Maybe you should try: How do I copy to the clipboard in JavaScript?

也许您应该尝试:如何在 JavaScript 中复制到剪贴板?

UPDATE: I was wrong - there is a possibility. As permissions pagesays there are "clipboardRead" and "clipboardWrite" permissions. So maybe they will work for you.

更新:我错了 - 有可能。正如权限页面所说,有“clipboardRead”和“clipboardWrite”权限。所以也许他们会为你工作。

回答by David Jeske

In order to read Clipboard text in a chrome extension, you have to:

为了在 chrome 扩展中读取剪贴板文本,您必须:

  • request "clipboardRead" permission in your manifest
  • create a background script, since only the background script can access the clipboard
  • create an element in your background page to accept the clipboard paste action. If you make this a textarea, you will get plain-text, if you make it a div with contentEditable=true, you will get Formatted HTML
  • if you want to pass the clipboard data back to an in page script, you'll need to use the message-passing API
  • 在您的清单中请求“clipboardRead”权限
  • 创建一个后台脚本,因为只有后台脚本可以访问剪贴板
  • 在您的背景页面中创建一个元素以接受剪贴板粘贴操作。如果您将其设为 textarea,您将获得纯文本,如果您将其设为 contentEditable=true 的 div,您将获得格式化的 HTML
  • 如果要将剪贴板数据传递回页内脚本,则需要使用消息传递 API

To see an example of this all working, see my BBCodePaste extension:

要查看所有工作的示例,请参阅我的 BBCodePaste 扩展:

https://github.com/jeske/BBCodePaste

https://github.com/jeske/BBCodePaste

Here is one example of how to read the clipboard text in the background page:

以下是如何阅读背景页面中剪贴板文本的一个示例:

bg = chrome.extension.getBackgroundPage();        // get the background page
bg.document.body.innerHTML= "";                   // clear the background page

// add a DIV, contentEditable=true, to accept the paste action
var helperdiv = bg.document.createElement("div");
document.body.appendChild(helperdiv);
helperdiv.contentEditable = true;

// focus the helper div's content
var range = document.createRange();
range.selectNode(helperdiv);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
helperdiv.focus();    

// trigger the paste action
bg.document.execCommand("Paste");

// read the clipboard contents from the helperdiv
var clipboardContents = helperdiv.innerHTML;