In AnkiMobile, navigator.clipboard.writeText successfully allows me to write a string into the clipboard, but it does not work on Anki Desktop for Linux. How can I write to clipboard on Anki Desktop? There is a difference in how the code works on both platforms.
Probably some difference between Chromium and Safari.
Ah gotcha! Thanks. I wasn’t aware of the different browsers for both. Gonna try the chromium version to see if it works and let you know.
So, I tested it on AnkiWeb using chromium and it worked just fine, but still refuses to work in Anki Desktop app. Works on all browsers with AnkiWeb and iOS app. If it works on AnkiWeb using chromium does that mean it should also work on desktop app using chromium? Or are they not the same thing?
The code I am trying to make work is this on card template:
<button href="#" onclick="navigator.clipboard.writeText('hello');return false;">Copy</button>
should copy hello to clipboard. Works everywhere except desktop app.
Edit:
Found something! I ran the app from console to see what it logs to stderr and I found this line when I press the copy button:
JS error /_anki/legacyPageData Uncaught (in promise) NotAllowedError: Write permission denied.
Any ideas on how I can circumvent this?
You should probably do something like that, according to mozilla’s documentation:
navigator.permissions.query({name: "clipboard-write"}).then(result => {
if (result.state == "granted" || result.state == "prompt") {
/* write to the clipboard now */
}
});
However, I fear that Qt will just deny that without even asking.
So I just tried that and now it doesn’t work on any platform, but the error message about permissions disappeared. I then added an else {console.log(result.state)}
and got JS info /_anki/legacyPageData: denied
. So I suppose that means that as you said Qt will just deny permission without asking. How can I work with permissions on Anki Desktop they will just be blanket denied?
Is this perhaps something that needs to be enabled in the source code? Found the following during online research:
Yes, indeed, what Qt actually does is that it has a hook for these permission requests do let the programmer handle them, but by default it will just deny in block.
Oh. In which case, what do you guys think about enabling clipboard-write permissions in the source? It’s already enabled on iOS/AnkiWeb, so enabling it for desktop would make it consistent for all Anki platforms.
On the one hand, I’d say there is a pretty good reason for which it’s disabled by default: you usually don’t want JS to be able to access your clipboard, for obvious security issues.
On the other hand, Anki already allows downloading add-ons, which can do much more than simply accessing the clipboard, so one might argue it has already committed to granting this kind of access. But still, while anyone will understand that downloading an add-on is not a danger free action, maybe it’s a bit more counterintuitive to think the same way of a deck.
Well, from what I read online research and from experimentation, the different browsers have a different stance on clipboard. Chrome is the strictest for clipboard actions. But Firefox and Safari only restrict clipboard-read. They allow clipboard-write without any special permissions. That would also explain why it is working on iOS but not desktop. I personally agree that while clipboard-read could be misused, clipboard-write should be safe. Plus, it would make the platforms consistent.
If Qt exposes a way to allow clipboard write without allowing clipboard read, I would be happy to accept a PR.