Page 1 of 1

SetClipboardText()

Posted: Sat Jul 28, 2018 6:34 am
by ols3
Need the same functionality as SetClipboardText() in Purebasic.
Thank you.

Re: SetClipboardText()

Posted: Sun Jul 29, 2018 9:57 pm
by poshu
Ask and you shall receive!

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'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

ಠ_ಠ

Re: SetClipboardText()

Posted: Sat Aug 04, 2018 2:27 am
by ols3
poshu wrote:Ask and you shall receive!
WOW!
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()

Posted: Sat Aug 04, 2018 2:05 pm
by poshu
Oh, I did not try it with an apk. I'll test and fix it.

Re: SetClipboardText()

Posted: Tue Sep 11, 2018 11:34 am
by Dirk Geppert
Thx poshu for that example. Unfortunately it doesn't work with my Firefox browser, although the code seems similar to the following (working) code:

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>
What could be the reason?

Re: SetClipboardText()

Posted: Tue Sep 11, 2018 12:18 pm
by Peter
Dirk Geppert wrote:Unfortunately it doesn't work with my Firefox browser
I can't confirm that.

Poshus code works for me with Ff V61.0.2 (and also with V62.0)

Greetings ... Peter

Re: SetClipboardText()

Posted: Tue Sep 11, 2018 2:38 pm
by Dirk Geppert
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.

Re: SetClipboardText()

Posted: Thu Feb 14, 2019 3:54 pm
by poshu
ols3 wrote:But, create app --> Android, not OK.
SetClipboardText() doesn't work in android apk.
I DEFINITELY did not forget about this. It was a genuinely hyper complex problem that took me 6 months of work to fix!

... 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