IngameDebugConsole.jslib 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. mergeInto( LibraryManager.library,
  2. {
  3. IngameDebugConsoleStartCopy: function( textToCopy )
  4. {
  5. var textToCopyJS = Pointer_stringify( textToCopy );
  6. // Delete if element exist
  7. var copyTextButton = document.getElementById( 'DebugConsoleCopyButtonGL' );
  8. if( !copyTextButton )
  9. {
  10. copyTextButton = document.createElement( 'button' );
  11. copyTextButton.setAttribute( 'id', 'DebugConsoleCopyButtonGL' );
  12. copyTextButton.setAttribute( 'style','display:none; visibility:hidden;' );
  13. }
  14. copyTextButton.onclick = function( event )
  15. {
  16. // Credit: https://stackoverflow.com/a/30810322/2373034
  17. if( navigator.clipboard )
  18. {
  19. navigator.clipboard.writeText( textToCopyJS ).then( function() { }, function( err )
  20. {
  21. console.error( "Couldn't copy text to clipboard using clipboard.writeText: ", err );
  22. } );
  23. }
  24. else
  25. {
  26. var textArea = document.createElement( 'textarea' );
  27. textArea.value = textToCopyJS;
  28. // Avoid scrolling to bottom
  29. textArea.style.top = "0";
  30. textArea.style.left = "0";
  31. textArea.style.position = "fixed";
  32. document.body.appendChild( textArea );
  33. textArea.focus();
  34. textArea.select();
  35. try
  36. {
  37. document.execCommand( 'copy' );
  38. }
  39. catch( err )
  40. {
  41. console.error( "Couldn't copy text to clipboard using document.execCommand", err );
  42. }
  43. document.body.removeChild( textArea );
  44. }
  45. };
  46. document.body.appendChild( copyTextButton );
  47. document.onmouseup = function()
  48. {
  49. document.onmouseup = null;
  50. copyTextButton.click();
  51. document.body.removeChild( copyTextButton );
  52. };
  53. },
  54. IngameDebugConsoleCancelCopy: function()
  55. {
  56. var copyTextButton = document.getElementById( 'DebugConsoleCopyButtonGL' );
  57. if( copyTextButton )
  58. document.body.removeChild( copyTextButton );
  59. document.onmouseup = null;
  60. }
  61. } );