Got an idea for enhancing SpiderBasic? New command(s) you'd like to see?
ols3
 
Posts: 2
Joined: Sat Jul 28, 2018 6:26 am

SetClipboardText()

by ols3 Sat Jul 28, 2018 6:34 am

Need the same functionality as SetClipboardText() in Purebasic.
Thank you.
poshu
 
Posts: 73
Joined: Mon Feb 24, 2014 11:46 pm

Re: SetClipboardText()

by poshu Sun Jul 29, 2018 9:57 pm

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

ಠ_ಠ
ols3
 
Posts: 2
Joined: Sat Jul 28, 2018 6:26 am

Re: SetClipboardText()

by ols3 Sat Aug 04, 2018 2:27 am

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.
poshu
 
Posts: 73
Joined: Mon Feb 24, 2014 11:46 pm

Re: SetClipboardText()

by poshu Sat Aug 04, 2018 2:05 pm

Oh, I did not try it with an apk. I'll test and fix it.
Dirk Geppert
 
Posts: 88
Joined: Fri Sep 22, 2017 7:02 am

Re: SetClipboardText()

by Dirk Geppert Tue Sep 11, 2018 11:34 am

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?
User avatar
Peter
 
Posts: 553
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1

Re: SetClipboardText()

by Peter Tue Sep 11, 2018 12:18 pm

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
Dirk Geppert
 
Posts: 88
Joined: Fri Sep 22, 2017 7:02 am

Re: SetClipboardText()

by Dirk Geppert Tue Sep 11, 2018 2:38 pm

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.
Return to Feature Requests and Wishlists

Who is online

Users browsing this forum: No registered users and 3 guests