Advertisement
MatiGe

Untitled

May 13th, 2025
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. console.trace();
  2. import { createRequire } from './module';
  3. const require = createRequire(import.meta.url);
  4. var express = require('express');
  5. var cors = require('cors');
  6. var app = express();
  7. document.addEventListener('DOMContentLoaded', function () {
  8.     const codeDisplay = document.getElementById('codeDisplay');
  9.     const generateButton = document.getElementById('generateButton');
  10.     const storageKey = 'generatedCode'; // Key for localStorage
  11.  
  12.  
  13.     app.use(cors({ origin: true, credentials: true }));
  14.     // Function to generate a 6-digit number code
  15.     function generateCode() {
  16.         let code = '';
  17.         for (let i = 0; i < 6; i++) {
  18.             code += Math.floor(Math.random() * 10); // Generates a random digit (0-9)
  19.         }
  20.         return code;
  21.     }
  22.  
  23.     // Function to display the code
  24.     function displayCode(code) {
  25.         codeDisplay.textContent = 'Your Code: ' + code;
  26.     }
  27.  
  28.     function saveCode(code) {
  29.         fetch('save_code.php', {
  30.             method: 'POST',
  31.             headers: {
  32.                 'Content-Type': 'application/x-www-form-urlencoded',
  33.             },
  34.             mode: 'cors',
  35.             body: 'code=' + encodeURIComponent(code),
  36.         })
  37.             .then(response => response.text())
  38.             .then(data => {
  39.                 console.log('Server response:', data);
  40.             })
  41.             .catch(error => {
  42.                 console.error('Error saving code:', error);
  43.             });
  44.     }
  45.  
  46.     // Modified loadCode function to fetch data from the server
  47.     function loadCode() {
  48.         fetch('get_code.php')
  49.             .then(response => response.json())
  50.             .then(data => {
  51.                 if (data && data.code) {
  52.                     displayCode(data.code);
  53.                     console.log('Code loaded from server:', data.code);
  54.                 } else {
  55.                     codeDisplay.textContent = 'No code generated yet.';
  56.                 }
  57.             })
  58.             .catch(error => {
  59.                 console.error('Error loading code:', error);
  60.             });
  61.     }
  62.     // Event listener for the button click
  63.     generateButton.addEventListener('click', function () {
  64.         const newCode = generateCode();
  65.         displayCode(newCode);
  66.         saveCode(newCode); // In a real scenario, this would trigger an AJAX call to your server.
  67.     });
  68.  
  69.     // Load any previously saved code when the page loads
  70.     loadCode();
  71. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement