【问题标题】:Auto paste clipboard on load page @ chrome extension在加载页面上自动粘贴剪贴板 @ chrome 扩展
【发布时间】:2018-11-28 04:18:16
【问题描述】:

我正在创建一个网站,其中的主要问题是从剪贴板粘贴内容,以及一个扩展。

我想在打开特定页面时自动粘贴剪贴板内容。

由于某种原因,页面加载时 execCommand("Paste") 没有执行。

content.js

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

background.js

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

ma​​nifest.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"
}
]

一切都很好,直到需要完成粘贴并且它不起作用......

谢谢:)

【问题讨论】:

  • 你有什么东西显示在 javascript 控制台中吗?
  • 像一个错误?不..

标签: javascript google-chrome google-chrome-extension


【解决方案1】:

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

那里说使用 document.execCommand('paste') 不会触发“粘贴”命令

显然还有其他方法可以进行复制和粘贴,但这完全取决于兼容性。 我仍在四处寻找一个好的解决方案。

【讨论】:

    【解决方案2】:

    不确定这是否是您要查找的内容,但这里是 Google 关于剪贴板 API 的教程:

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

    显然,您需要一个变量来保存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.');
      }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-31
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      • 2016-01-30
      • 2017-12-23
      • 2015-07-29
      相关资源
      最近更新 更多