admin管理员组文章数量:1287785
I wanted to create very small extension for website that will automatically copy some value to the clipboard.
The problem is that I want it to copy the value even if browser is not focused e.g.:
- I open the website, my extension listens to the change on the page
- I open different application
- If something changes on the page then the extension should copy some value
- Main application that I'm working with is still focused but I can CTRL+V paste value copied from the website without alt+tab
I tried to use Clipboard API:
navigator.clipboard.writeText(...)
but I don't think it will work because browser have to be focused(I think).
When page is focused then copying works fine. If I try to switch to different application I get an exception when my extension tries to copy the value:
DOMException: Document is not focused.
Is there any way to do this?
I wanted to create very small extension for website that will automatically copy some value to the clipboard.
The problem is that I want it to copy the value even if browser is not focused e.g.:
- I open the website, my extension listens to the change on the page
- I open different application
- If something changes on the page then the extension should copy some value
- Main application that I'm working with is still focused but I can CTRL+V paste value copied from the website without alt+tab
I tried to use Clipboard API:
navigator.clipboard.writeText(...)
but I don't think it will work because browser have to be focused(I think).
When page is focused then copying works fine. If I try to switch to different application I get an exception when my extension tries to copy the value:
DOMException: Document is not focused.
Is there any way to do this?
Share Improve this question asked Jan 1, 2020 at 21:19 DarknovDarknov 1211 gold badge1 silver badge6 bronze badges 3- 3 No, and this is by-design, so malicious websites cannot interfere with the user's clipboard unless the user expects them to. – Dai Commented Jan 1, 2020 at 21:21
- Is there any way to get around it somehow? Different browser, settings or something like that? – Darknov Commented Jan 1, 2020 at 21:27
- Does this answer your question? DOMException on calling navigator.clipboard.readText() – AncientSwordRage Commented Nov 29, 2021 at 11:00
1 Answer
Reset to default 7This is not possible for security reasons.. It's hard to imagine anyone wanting this behavior...
This document has a lot of good info..
In Chrome, you can request clipboard-write
permissions to write to clipboard outside of a small user generated event, although it does not appear as though Chrome limits you to when you can write to clipboard.. According to the article below, you can write to the clipboard in Chrome from the background, etc.. See the note at the bottom of this section for more info.
If Chrome does allow you to write to clipboard from the background or if the window is not selected, you could possibly use the Page Visiblity API to kick off the copy event when "that" specific window is not visible.
You could possibly even use the window.addEventListener('blur', function(){...})
handler to test, etc...
All in all, this MAY be possible in Chrome, but it is definitely not supported in Firefox.
You can check out the differences between browsers and how they handle clipboard related events/permissions/etc, here..
本文标签: javascriptCopy to clipboard from website when browser is not focusedStack Overflow
版权声明:本文标题:javascript - Copy to clipboard from website when browser is not focused - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741321085a2372208.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论