• Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!

HTML5 Copying text to clipboard for SMS Share Gamemaker Studio 2

dhouseley

Member
Hello all,

I've been building a game that is playable on both mobile and desktop browsers, the issue now is I am trying to create share functionality on the games end screen. I have found useful extensions where I can push users to a social media URL, so that's not a problem.

My main issue is I'm trying to create a button that copies a URL to the user's clipboard so they can share via SMS or however they want. However, gamemaker does not seem to let me do this. Here is my javascript that runs on button click. It is logging the console but not copying text to the clipboard.

var shareLink = document.createElement('textarea');
shareLink.style.visibility = 'fixed';
shareLink.style.top = 0;
shareLink.style.left = 0;
shareLink.style.width = '2em';
shareLink.style.height = '2em';
shareLink.style.padding = 0;
shareLink.style.border = 'none';
shareLink.style.outline = 'none';
shareLink.style.boxShadow = 'none';
shareLink.style.background = 'transparent';
shareLink.value = 'https://www.example.com';
document.body.appendChild(shareLink);
shareLink.select();
document.execCommand('copy');
document.body.removeChild(shareLink);

Let me know if anyone has any solutions or has encountered the same problem.
 

dhouseley

Member
Hi,

I've tested your code on a blank page and it did work. So I assume you're trying to use it in a click event inside GameMaker? If so, that might be the problem, something similar than using a url_open function with "_blank" target (which would trigger a popup issue).

I think doing something like this extension by @YellowAfterlife could meet your needs:
https://yal.cc/gamemaker-opening-links-in-new-tab-on-html5/
Thank you so much,

I put my javascript copy to clipboard right in there and it worked like a charm on desktop,

The only issue I'm having now is that the button doesn't register the event on a mobile device.

with (obj_sms_button) if (position_meeting(mouse_x, mouse_y, id)) {
copylink();
}

This is whats happening in my click event now but I don't know if that will work with mobile.
 

chmod777

Member
Try to replace:
Code:
window.addEventListener("click", window.gml_Script_gmcallback_***);
to:
Code:
window.addEventListener("mouseup", window.gml_Script_gmcallback_***); // desktops
window.addEventListener("touchend", window.gml_Script_gmcallback_***); // touchscreen devices
("click" event should work on mobiles but it adds a delay, so maybe changing this this will solve your issue)
 

donsoyer

Member
Thank you so much,

I put my javascript copy to clipboard right in there and it worked like a charm on desktop,

The only issue I'm having now is that the button doesn't register the event on a mobile device.

with (obj_sms_button) if (position_meeting(mouse_x, mouse_y, id)) {
copylink();
}

This is whats happening in my click event now but I don't know if that will work with mobile.

can I ask You What did You put in the code in GMS to copy the content to clipboard in HTML project?? :/
 
Top