在 Chrome 扩展 V3 中复制到剪贴板
我正在开发一个 chrome 扩展 V3。我想将 JS 文件中的内容复制到剪贴板。
Manifest.json 如下,
"background" :{
"service_worker" :"eventPage.js"
},
"permissions" : [
"contextMenus",
"clipboardWrite"
]
我尝试了 2 个复制功能的解决方案。
解决方案 1:
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
结果:
Error in event handler: ReferenceError: document is not defined at copyToClipboard
解决方案 2:
navigator.clipboard.writeText(str);
结果:
Error in event handler: TypeError: Cannot read properties of undefined (reading 'writeText')
chrome 扩展程序作为 Service Worker 运行。所以看来我无法访问 DOM 文档并且没有授予 writeText 权限。还有人有其他建议吗?
谢谢。
I am developing a chrome extension V3. I want to copy content to clipboard in my JS file.
The manifest.json as below,
"background" :{
"service_worker" :"eventPage.js"
},
"permissions" : [
"contextMenus",
"clipboardWrite"
]
I have try 2 solution for copy feature.
Solution 1:
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
The result:
Error in event handler: ReferenceError: document is not defined at copyToClipboard
Solution 2:
navigator.clipboard.writeText(str);
The result:
Error in event handler: TypeError: Cannot read properties of undefined (reading 'writeText')
The chrome extension is run as a service worker. So it seems I can't access DOM document and have no grant of writeText. Does anyone have another suggestion?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将遵循 wOxxOm 给您的出色建议,并通过具体示例对其进行详细说明。您想要做的是让 ContentScript.js 在任何带有网页的活动选项卡上运行,因为您无法从 backGround.js 访问 DOM,然后向此脚本发送消息,您可以从其中复制到剪贴板。
manifest.json
从 background.js 中,您将发送一条消息,该消息将在 ContentScript.js 中处理。
back.js
在 contentScript.js 中,您将捕获该消息并将其复制到剪贴板。
内容.js
I'll follow the excellent suggestion wOxxOm gave you, elaborating it in a concrete example. What you want to do is have a ContentScript.js that runs on any active tab with a web page, since you can't access the DOM from the backGround.js, and then send a message to this script, from where you would copy to the clipboard.
manifest.json
From the background.js, you would send a message, that will be handled in the ContentScript.js
background.js
In the contentScript.js, you would catch the message and copy it to the clipboard.
content.js
我不是 Chrome 插件方面的专家,但我相信请求
并不是最佳做法。相反,我通过请求 activeTab 来工作,放弃了内容脚本并向活动选项卡注入代码。
清单:
背景:
因此
contentCopy
被注入到活动选项卡并使用我想要复制到剪贴板的文本执行。效果很好,并且可以节省有问题的权限(根据 Google 的说法)
I'm not an expert in Chrome addons, but I believe that requesting
<all_urls>
is not best practice.Instead, I got something to work by request activeTab instead, ditching the content scripts and injecting code to the active tab instead.
Manifest:
Background:
So
contentCopy
is injected to the active tab and executed with the text I want to copy to clipboard.Works well, and saves on problematic permissions (according to Google)