简体   繁体   English

在加载页面上自动粘贴剪贴板@ chrome扩展

[英]Auto paste clipboard on load page @ chrome extension

I'm creating a website that the main issue in it is pasting stuff from clipboard, and an extension for that. 我正在创建一个网站,其中的主要问题是粘贴剪贴板中的内容以及该扩展的内容。

I want to auto-paste clipboard content when a specific page is opened. 我想在打开特定页面时自动粘贴剪贴板内容。

For some reason the execCommand("Paste") is not executed when the page is loaded. 由于某些原因,加载页面时不执行execCommand(“ Paste”)。

content.js content.js

setTimeout(function() { chrome.extension.sendMessage({greeting: "hello"},function(response){}); },200);

background.js background.js

chrome.extension.onMessage.addListener(
  function(request, sender, sendResponse) {
document.execCommand("Paste");
  sendResponse({});
  return true;
});

manifest.json manifest.json

"background": {
  "page": "src/bg/background.html",
  "persistent": true
},
"options_page": "src/options/index.html",
"permissions": [
  "clipboardRead",
  "clipboardWrite",
  "fileBrowserHandler",
  "*my website address*"
],
"content_scripts": [
{
"matches": [
        "*my website address*"
    ],
"js": ["js/content.js"],
    "run_at": "document_end"
}
]

everything works great until the paste is needed to be done and it's just not working... 一切都很好,直到需要完成粘贴并且它不起作用为止。

thanks :) 谢谢 :)

http://caniuse.com/#search=clipboardData http://caniuse.com/#search=clipboardData

There says that "paste" command is not fired by using document.execCommand('paste') 那里说,使用document.execCommand('paste')不会触发“粘贴”命令

Apparently there are other ways to make copy & paste but it all depends on compatibility. 显然,还有其他方法可以进行复制和粘贴,但这完全取决于兼容性。 I am still browsing around looking for a nice solution. 我仍在四处寻找理想的解决方案。

Not sure if this is what you're looking for, but here's Google's tutorial on the Clipboard API: 不确定这是否是您要查找的内容,但这是有关剪贴板API的Google教程:

https://developers.google.com/web/updates/2018/03/clipboardapi https://developers.google.com/web/updates/2018/03/clipboardapi

Apparently, you need a variable to hold the result of document.execCommand('paste') : 显然,您需要一个变量来保存document.execCommand('paste')

button.addEventListener('click', e => {
  const input = document.createElement('input');
  document.body.appendChild(input);
  input.value = text;
  input.focus();
  input.select();
  const result = document.execCommand('copy');
  if (result === 'unsuccessful') {
    console.error('Failed to copy text.');
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM