CodeHSRuse

1/25/2024, 9:16:47 AM || this allows you to copy and paste in CodeHS / working as of Feb 1, 24'

  1. // ==UserScript==
  2. // @name CodeHSRuse
  3. // @namespace Violentmonkey Scripts
  4. // @match *://codehs.com/*/assignment/*
  5. // @grant none
  6. // @version 1.1
  7. // @author klrin
  8. // @license MIT
  9. // @description 1/25/2024, 9:16:47 AM || this allows you to copy and paste in CodeHS / working as of Feb 1, 24'
  10. // ==/UserScript==
  11.  
  12.  
  13. function handlePaste(e, editor) {
  14. var clipboardData, pastedData;
  15.  
  16. // Stop data actually being pasted into div
  17. e.stopPropagation();
  18. e.preventDefault();
  19.  
  20. // Get pasted data via clipboard API
  21. clipboardData = e.clipboardData || window.clipboardData;
  22. pastedData = clipboardData.getData('Text');
  23.  
  24. // Do whatever with pasteddata
  25. editor.insert(pastedData);
  26. }
  27.  
  28. function fallbackCopyTextToClipboard(text) {
  29. var textArea = document.createElement("textarea");
  30. textArea.value = text;
  31.  
  32. // Avoid scrolling to bottom
  33. textArea.style.top = "0";
  34. textArea.style.left = "0";
  35. textArea.style.position = "fixed";
  36.  
  37. document.body.appendChild(textArea);
  38. textArea.focus();
  39. textArea.select();
  40.  
  41. try {
  42. var successful = document.execCommand('copy');
  43. var msg = successful ? 'successful' : 'unsuccessful';
  44. console.log('Fallback: Copying text command was ' + msg);
  45. } catch (err) {
  46. console.error('Fallback: Oops, unable to copy', err);
  47. }
  48.  
  49. document.body.removeChild(textArea);
  50. }
  51. function copyTextToClipboard(text) {
  52. if (!navigator.clipboard) {
  53. fallbackCopyTextToClipboard(text);
  54. return;
  55. }
  56. navigator.clipboard.writeText(text).then(function() {
  57. console.log('Async: Copying to clipboard was successful!');
  58. }, function(err) {
  59. console.error('Async: Could not copy text: ', err);
  60. });
  61. }
  62.  
  63. function copyText(editor) {
  64. copyTextToClipboard(editor.getSelectedText())
  65. console.log("Copying "+editor.getSelectedText())
  66. }
  67.  
  68.  
  69. window.addEventListener("load", (e) => {
  70. let editorel = document.getElementById("ace-editor")
  71. console.log("Starting the changer.")
  72. let editor = ace.edit("ace-editor");
  73. editor.setTheme("ace/theme/twilight");
  74. document.addEventListener("copy", (e) => {
  75. console.log("RUNNING")
  76. copyText(editor)
  77. }, true)
  78. editorel.addEventListener("copy", (e) => {
  79. console.log("RUNNING")
  80. copyText(editor)
  81. }, true)
  82. window.addEventListener("copy", (e) => {
  83. console.log("RUNNING")
  84. copyText(editor)
  85. }, true)
  86.  
  87.  
  88. document.addEventListener("paste", (e) => {handlePaste(e, editor)}, true)
  89. editorel.addEventListener("paste", (e) => {handlePaste(e, editor)}, true)
  90. window.addEventListener("paste", (e) => {handlePaste(e, editor)}, true)
  91.  
  92. })
  93.  
  94.  
  95.  
  96.