Google Chrome 扩展:如何检测复制操作(Ctrl-C 和编辑复制)?

发布于 2024-09-02 08:09:31 字数 138 浏览 5 评论 0原文

如何检测用户已在 Google Chrome 扩展程序的当前活动选项卡中选择并复制了某些内容?

似乎没有合适的事件来处理 chrome.tabs 或 chrome.windows 中的剪贴板。

有没有办法通过内容脚本检测此类操作?

How might I detect that a user has selected and copied some content in the currently active tab in a Google Chrome Extension?

It appears that there are no suitable Events that deal with the Clipboard in chrome.tabs or chrome.windows.

Is there a way to detect such actions through Content Scripts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

揪着可爱 2024-09-09 08:09:31

我找到了以下解决方案:

  1. 设置一个清单文件来定义添加到每个页面的内容脚本,以及一个单独的背景页面。
  2. 在内容脚本 .js 文件中,为文档或窗口的“复制”事件添加事件侦听器。每当用户启动复制操作时都会调用此事件侦听器。
  3. 由于内容脚本存在于安全沙箱中(例如,没有跨站点XMLHttpRequests),我们可能希望响应后台页面中的事件。为此,请使用 Chrome 消息传递 API,以便向后台页面发送消息。

一个小的工作示例:

ma​​nifest.json

{
  "background_page": "background.html",
  "content_scripts": [
      {
        "matches": ["http://*/*"],
        "js": ["oncopy.js"]
      }
    ]
}

oncopy.js

// on copy event, send a message to background.html
function onCopy(e) { 
    chrome.extension.sendRequest({event: "copy"});
}

//register event listener for copy events on document
document.addEventListener('copy',onCopy,true); 

background.html

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    if (request.event == "copy") {
       alert("copy detected");
    }
    sendResponse({});
  });

I found the following solution:

  1. Set up a manifest file to define a content script that is added to every page, and a separate background page.
  2. In the Content Script .js file, add an event listener for the 'copy' event, either for the document or the window. This event listener is called whenever the user initiates a copy action.
  3. Since content scripts exist in a security sandbox (e.g., no cross-site XMLHttpRequests), we probably want to respond to the event in the background page. To do so, use the Chrome message passing API so send a message to the background page.

A small working example:

manifest.json

{
  "background_page": "background.html",
  "content_scripts": [
      {
        "matches": ["http://*/*"],
        "js": ["oncopy.js"]
      }
    ]
}

oncopy.js

// on copy event, send a message to background.html
function onCopy(e) { 
    chrome.extension.sendRequest({event: "copy"});
}

//register event listener for copy events on document
document.addEventListener('copy',onCopy,true); 

background.html

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    if (request.event == "copy") {
       alert("copy detected");
    }
    sendResponse({});
  });
〗斷ホ乔殘χμё〖 2024-09-09 08:09:31

您必须使用自己的事件处理程序捕获文档元素上的 onkeypress,检查 event.keyCode = 'C' (或 ascii 代码)以及 event.ctrlKey = true

如果这是有效的,那么很可能是用户复制了当前页面上的文本。

您还需要捕获 tab.updated 事件,以便知道何时绑定 onkeypress 事件。

You will have to capture onkeypress on the document element using your own event Handler, check that event.keyCode = 'C' (or the ascii code) and that the event.ctrlKey = true

If this is valid, then it is likely that the user copied text on the current page.

You will need to also capture the tab.updated event so that you know when to bind the onkeypress event.

初见 2024-09-09 08:09:31

Google Chrome 扩展程序有一个实验性 API 供您使用,它允许您执行复制、剪切和粘贴。它目前存在于测试版本中,所以很快,它将从实验状态转变为稳定状态。

http://code.google.com/chrome/extensions/dev/实验.剪贴板.html

Google Chrome Extensions has an experimental API for you to use that will allow you to execute a copy, cut, and paste. It currently exists in the beta build, so very soon, it will be out of experimental into stable.

http://code.google.com/chrome/extensions/dev/experimental.clipboard.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文