Need the same functionality as SetClipboardText() in Purebasic.
Thank you.
SetClipboardText()
Re: SetClipboardText()
Ask and you shall receive!
It's using the cliboard API which is currently experimental (though it is implemented in chrome since march!), so I added a fallback solution for good measure!
It was tested on up-to-date Firefox, Chrome and Edge.
Also added it to JSTools : viewtopic.php?f=9&t=1530
ಠ_ಠ
Code: Select all
Procedure _SetClipboardText(text.s)
! var textArea = document.createElement("textarea");
! textArea.value = v_text;
! document.body.appendChild(textArea);
! textArea.focus();
! textArea.select();
!
! try {
! document.execCommand('copy');
! } catch (err) {
! console.error('Fallback failed', err);
! }
! document.body.removeChild(textArea);
EndProcedure
Procedure SetClipboardText(text.s)
! if (!navigator.clipboard) {
! f__setclipboardtext(v_text);
! return;
! }
! navigator.clipboard.writeText(v_text);
EndProcedure
;- Demo:
Procedure Demo()
SetClipboardText(GetGadgetText(0))
EndProcedure
OpenWindow(#PB_Any, 10,10, 200,100, "SetClipboardText demo",#PB_Window_SystemMenu)
StringGadget(0,10,10,180,20,"")
ButtonGadget(1,10,40,180,20,"Copy to clipboard!")
BindGadgetEvent(1,@Demo())
It was tested on up-to-date Firefox, Chrome and Edge.
Also added it to JSTools : viewtopic.php?f=9&t=1530
ಠ_ಠ
Re: SetClipboardText()
WOW!poshu wrote:Ask and you shall receive!
Thank you very much!
Test results:
It works! ( create app --> Web OK)
But, create app --> Android, not OK.
SetClipboardText() doesn't work in android apk.
Re: SetClipboardText()
Oh, I did not try it with an apk. I'll test and fix it.
-
- Posts: 284
- Joined: Fri Sep 22, 2017 7:02 am
Re: SetClipboardText()
Thx poshu for that example. Unfortunately it doesn't work with my Firefox browser, although the code seems similar to the following (working) code:
What could be the reason?
Code: Select all
<html>
<body>
<textarea id="input">Copy me</textarea>
<button id="copy-button">Copy</button>
<script>
var input = document.getElementById("input");
var button = document.getElementById("copy-button");
button.addEventListener("click", function (event) {
event.preventDefault();
input.select();
document.execCommand("copy");
});
</script>
</body>
</html>
Re: SetClipboardText()
I can't confirm that.Dirk Geppert wrote:Unfortunately it doesn't work with my Firefox browser
Poshus code works for me with Ff V61.0.2 (and also with V62.0)
Greetings ... Peter
-
- Posts: 284
- Joined: Fri Sep 22, 2017 7:02 am
Re: SetClipboardText()
Args, you're right. It only does not work with my ff62.0. May be a problem of one add on..
I apologize for any inconvenience.
I apologize for any inconvenience.
Re: SetClipboardText()
I DEFINITELY did not forget about this. It was a genuinely hyper complex problem that took me 6 months of work to fix!ols3 wrote:But, create app --> Android, not OK.
SetClipboardText() doesn't work in android apk.
... Ok, it's a 3 lines piece of code I forgot to post... Ahem... Here it is, better late than never, right? ;D : viewtopic.php?f=9&t=1530
It's using https://github.com/ihadeed/cordova-clipboard, which is under MIT License, a very permissive license, just read about it there : https://en.wikipedia.org/wiki/MIT_License