首页 > 解决方案 > 剪贴板通过背景页面粘贴只能工作一次

问题描述

将我的 Firefox 扩展程序转换为 Web 扩展程序并在使用 FF 60 从剪贴板粘贴时遇到问题。

我从我的后台脚本(定义它的地方)调用以下函数:

var readClipboard = function() {
  let txtToPaste = document.createElement('textarea');
  txtToPaste.id = "txtToPaste";
  document.body.appendChild(txtToPaste);

  txtToPaste.focus();

  let res = document.execCommand("paste");
  let result = txtToPaste.value;

  txtToPaste.parentNode.removeChild(txtToPaste);

  return result;
}

但是,这只适用于一次。当我单击调用此函数的按钮res时,第一次为真,以后每次为假。

我已经尝试将 textarea 硬编码到背景 html 页面中并这样做,但没有运气。权限等必须是正确的,因为它工作一次。

编辑:我稍微更改了代码,以便它可以在 Chrome 中运行。它仍然无法在 FF 中使用。

标签: javascriptgoogle-chrome-extensionfirefox-addon-webextensions

解决方案


推荐阅读