Created
January 10, 2024 05:31
-
-
Save octiwhale/e6af390d90e3a2aaca0a9edf9cf0623a to your computer and use it in GitHub Desktop.
vCopywrite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Flag to check if shift key is pressed | |
| let vcr_shiftPressed = false; | |
| // Listen for keydown events on the window | |
| // Flag to check if Command (on MacOS) or Ctrl (on other OS) + 'J' is pressed | |
| let vcr_commandOrCtrlPressed = false; | |
| // Listen for keydown events on the window | |
| window.addEventListener('keydown', function(event) { | |
| // Check if the pressed key is 'J' and either Command (metaKey) or Ctrl is pressed | |
| if (event.key === 'j' && (event.metaKey || event.ctrlKey)) { | |
| // Prevent default action to avoid conflict with browser shortcuts | |
| event.preventDefault(); | |
| // Toggle the commandOrCtrlPressed flag | |
| vcr_commandOrCtrlPressed = !vcr_commandOrCtrlPressed; | |
| if (vcr_commandOrCtrlPressed) { | |
| alert('vCopyright is active! You can now edit the page.'); | |
| // Make the body content editable | |
| document.body.contentEditable = 'true'; | |
| } else { | |
| alert('Exporting page...'); | |
| vcr_exportPage(); | |
| } | |
| } | |
| }); | |
| // Function to export the current page | |
| function vcr_exportPage() { | |
| // Disable content-editable on body | |
| document.body.contentEditable = 'false'; | |
| // Clone the body to avoid modifying the original | |
| const vcr_bodyClone = document.body.cloneNode(true); | |
| const vcr_textContent = vcr_bodyClone.innerHTML; | |
| // Properly encode the text content | |
| const vcr_encodedContent = btoa(encodeURIComponent(vcr_textContent)); | |
| // Create a blob with the encoded content | |
| const vcr_blob = new Blob([vcr_encodedContent], { type: 'text/plain' }); | |
| const vcr_downloadLink = document.createElement('a'); | |
| vcr_downloadLink.href = URL.createObjectURL(vcr_blob); | |
| // Set the download file name | |
| const vcr_fileName = window.location.pathname.split('/').pop().replace('.html', '.txt') || Math.random().toString(36).substr(2, 5) + '.txt'; | |
| vcr_downloadLink.download = vcr_fileName; | |
| // Trigger the download | |
| document.body.appendChild(vcr_downloadLink); | |
| vcr_downloadLink.click(); | |
| document.body.removeChild(vcr_downloadLink); | |
| } | |
| // Function to decode and optionally export HTML content | |
| function vcr(base64Content, htmlExport) { | |
| // Decode the Base64 string | |
| const vcr_decodedContent = decodeURIComponent(atob(base64Content)); | |
| // Update the page content | |
| document.body.innerHTML = vcr_decodedContent; | |
| if (htmlExport) { | |
| // Create and download blob as HTML | |
| const vcr_blob = new Blob([vcr_decodedContent], { type: 'text/html' }); | |
| const vcr_downloadLink = document.createElement('a'); | |
| vcr_downloadLink.href = URL.createObjectURL(vcr_blob); | |
| vcr_downloadLink.download = window.location.pathname.split('/').pop() || 'vcopywrite.html'; | |
| document.body.appendChild(vcr_downloadLink); | |
| vcr_downloadLink.click(); | |
| document.body.removeChild(vcr_downloadLink); | |
| alert('Page updated + exported as HTML!'); | |
| } else { | |
| alert('Page updated!'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment