Created
February 23, 2025 02:43
-
-
Save LukeNewNew/a6abe65f12020b3cedeff83710c5d18d to your computer and use it in GitHub Desktop.
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
| const http = require('http'); | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| // Global variable to store clipboard content | |
| let clipboardContent = ""; | |
| const server = http.createServer((req, res) => { | |
| console.log(req.socket.remoteAddress, req.method, req.url); | |
| // Handle main routes | |
| if (req.method === 'GET') { | |
| if (req.url === '/') { | |
| // Serve the HTML file | |
| fs.readFile(path.join(__dirname, 'index.html'), (err, content) => { | |
| if (err) { | |
| res.writeHead(500); | |
| res.end('Error loading index.html'); | |
| return; | |
| } | |
| res.writeHead(200, { | |
| 'Content-Type': 'text/html' | |
| }); | |
| res.end(content); | |
| }); | |
| } | |
| else if (req.url === '/api/clipboard') { | |
| // Return clipboard content | |
| res.writeHead(200, { | |
| 'Content-Type': 'application/json' | |
| }); | |
| res.end(JSON.stringify({ content: clipboardContent })); | |
| } | |
| else { | |
| // Handle 404 | |
| res.writeHead(404); | |
| res.end('Not Found'); | |
| } | |
| } | |
| else if (req.method === 'POST' && req.url === '/api/clipboard') { | |
| // Handle POST requests to update clipboard | |
| let body = ''; | |
| req.on('data', chunk => { | |
| body += chunk.toString(); | |
| }); | |
| req.on('end', () => { | |
| try { | |
| const postData = JSON.parse(body); | |
| clipboardContent = postData.content || ''; | |
| res.writeHead(200, { | |
| 'Content-Type': 'application/json' | |
| }); | |
| res.end(JSON.stringify({ | |
| status: 'success', | |
| message: 'Clipboard content updated' | |
| })); | |
| } catch (error) { | |
| res.writeHead(400, { | |
| 'Content-Type': 'application/json' | |
| }); | |
| res.end(JSON.stringify({ | |
| status: 'error', | |
| message: 'Invalid JSON data' | |
| })); | |
| } | |
| }); | |
| } | |
| else { | |
| // Handle unsupported methods | |
| res.writeHead(405); | |
| res.end('Method Not Allowed'); | |
| } | |
| }); | |
| const PORT = 8000; | |
| server.listen(PORT, () => { | |
| console.log(`Server running on port ${PORT}`); | |
| }); |
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
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Cross-Device Clipboard</title> | |
| <style> | |
| body { | |
| font-family: Arial, sans-serif; | |
| max-width: 600px; | |
| margin: 0 auto; | |
| padding: 20px; | |
| line-height: 1.6; | |
| } | |
| textarea { | |
| width: 100%; | |
| height: 100px; | |
| margin-bottom: 10px; | |
| } | |
| button { | |
| padding: 10px; | |
| margin-right: 10px; | |
| cursor: pointer; | |
| } | |
| #status { | |
| margin-top: 20px; | |
| font-weight: bold; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <h1>Cross-Device Clipboard</h1> | |
| <textarea id="clipboardText" placeholder="Enter text to save to clipboard"></textarea> | |
| <div> | |
| <button onclick="saveToClipboard()">Save to Clipboard</button> | |
| <button onclick="retrieveFromClipboard()">Retrieve from Clipboard</button> | |
| </div> | |
| <div id="status"></div> | |
| <script> | |
| function saveToClipboard() { | |
| const text = document.getElementById('clipboardText').value; | |
| if (text) { | |
| fetch('/api/clipboard', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify({ content: text }), | |
| }) | |
| .then(response => response.json()) | |
| .then(data => updateStatus(data.message)) | |
| .catch(error => updateStatus('Error: ' + error)); | |
| } else { | |
| updateStatus('Please enter text to save'); | |
| } | |
| } | |
| function retrieveFromClipboard() { | |
| fetch('/api/clipboard') | |
| .then(response => response.json()) | |
| .then(data => { | |
| document.getElementById('clipboardText').value = data.content; | |
| updateStatus('Text retrieved from clipboard'); | |
| }) | |
| .catch(error => updateStatus('Error: ' + error)); | |
| } | |
| function updateStatus(message) { | |
| document.getElementById('status').textContent = message; | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment