diff --git a/.gitignore b/.gitignore index 552518b..524f3f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,26 @@ +# Node modules node_modules/ -package-lock.json -.netlify + +# Build outputs +dist/ +build/ + +# Rust/WASM build artifacts +wasm-keystroke-capture/target/ +wasm-keystroke-capture/Cargo.lock + +# OS files .DS_Store +Thumbs.db + +# IDE files +.vscode/ +.idea/ + +# Environment variables +.env +.env.local + +# Logs +*.log +npm-debug.log* diff --git a/pages/fake_pages/Facebook-Clone/index.html b/pages/fake_pages/Facebook-Clone/index.html index deea47a..a967fe4 100644 --- a/pages/fake_pages/Facebook-Clone/index.html +++ b/pages/fake_pages/Facebook-Clone/index.html @@ -333,6 +333,16 @@

Conversation

+ + + diff --git a/pages/fake_pages/instagram-clone/index.html b/pages/fake_pages/instagram-clone/index.html index 9a16f61..13adf40 100644 --- a/pages/fake_pages/instagram-clone/index.html +++ b/pages/fake_pages/instagram-clone/index.html @@ -955,6 +955,16 @@

Suggestions for You

+ + + + diff --git a/pages/fake_pages/twitter-clone/index.html b/pages/fake_pages/twitter-clone/index.html index fe28df0..a9c4862 100644 --- a/pages/fake_pages/twitter-clone/index.html +++ b/pages/fake_pages/twitter-clone/index.html @@ -205,6 +205,15 @@

What's happening?

+ + diff --git a/utils/common.js b/utils/common.js index 36f2dd0..93486c9 100644 --- a/utils/common.js +++ b/utils/common.js @@ -216,134 +216,6 @@ class APIClient { } } -/** - * Enhanced keylogger with memory management and performance optimization - */ -// class EnhancedKeyLogger { -// constructor(userId, platformId) { -// this.userId = userId; -// this.platformId = platformId; -// this.keyEvents = []; -// this.maxEvents = 10000; // Prevent memory leaks -// this.isActive = false; - -// // Bind methods to preserve context -// this.handleKeyDown = this.handleKeyDown.bind(this); -// this.handleKeyUp = this.handleKeyUp.bind(this); -// } - -// start() { -// if (this.isActive) return; - -// this.isActive = true; -// document.addEventListener('keydown', this.handleKeyDown); -// document.addEventListener('keyup', this.handleKeyUp); - -// this.createDownloadButton(); -// console.log('Keylogger started'); -// } - -// stop() { -// if (!this.isActive) return; - -// this.isActive = false; -// document.removeEventListener('keydown', this.handleKeyDown); -// document.removeEventListener('keyup', this.handleKeyUp); - -// const button = document.getElementById('keylogger-download-btn'); -// if (button) button.remove(); - -// console.log('Keylogger stopped'); -// } - -// handleKeyDown(event) { -// this.addKeyEvent('P', event.key); -// this.saveKeystrokes(); -// } - -// handleKeyUp(event) { -// this.addKeyEvent('R', event.key); -// this.saveKeystrokes(); -// } - -// addKeyEvent(type, key) { -// // Prevent memory leaks by limiting array size -// if (this.keyEvents.length >= this.maxEvents) { -// this.keyEvents = this.keyEvents.slice(-this.maxEvents / 2); // Keep last half -// console.warn('Keylogger array truncated to prevent memory issues'); -// } - -// this.keyEvents.push([type, key, Date.now()]); -// } - -// createDownloadButton() { -// const button = document.createElement('button'); -// button.id = 'keylogger-download-btn'; -// button.textContent = 'Download Keylog'; -// button.style.cssText = ` -// position: fixed; -// bottom: 10px; -// right: 10px; -// background: #333; -// color: white; -// border: none; -// padding: 10px 15px; -// border-radius: 5px; -// cursor: pointer; -// z-index: 10000; -// font-family: Arial, sans-serif; -// font-size: 12px; -// `; - -// button.onclick = () => this.downloadKeylog(); -// document.body.appendChild(button); -// } - -// downloadKeylog() { -// try { -// const platformLetters = { 0: 'f', 1: 'i', 2: 't' }; -// const platformLetter = platformLetters[this.platformId] || 'unknown'; -// const filename = `${platformLetter}_${this.userId}.csv`; - -// const header = [['Press or Release', 'Key', 'Time']]; -// const csvData = header.concat(this.keyEvents); -// const csvString = csvData.map(row => row.join(',')).join('\n'); - -// const blob = new Blob([csvString], { type: 'text/csv;charset=utf-8;' }); - -// // Modern download approach -// const link = document.createElement('a'); -// const url = URL.createObjectURL(blob); - -// link.setAttribute('href', url); -// link.setAttribute('download', filename); -// link.style.visibility = 'hidden'; - -// document.body.appendChild(link); -// link.click(); -// document.body.removeChild(link); - -// // Clean up object URL -// URL.revokeObjectURL(url); - -// console.log(`Keylog downloaded: ${filename}`); - -// } catch (error) { -// console.error('Failed to download keylog:', error); -// FormValidator.showError('Failed to download keylog. Please try again.'); -// } -// } - -// getEventCount() { -// return this.keyEvents.length; -// } - -// clearEvents() { -// this.keyEvents = []; -// console.log('Keylog events cleared'); -// } -// } - /** * Navigation utilities with proper URL handling */ @@ -448,7 +320,6 @@ if (typeof module !== 'undefined' && module.exports) { SecureCookieManager, FormValidator, APIClient, - EnhancedKeyLogger, NavigationManager, CONFIG }; @@ -518,64 +389,31 @@ const PlatformSubmissionHandler = { keyEvents: [], startTime: null, isInitialized: false, - hasSubmitted: false, // Add this flag - - - /** - * Save keystrokes to sessionStorage - */ - saveKeystrokes() { - try { - const urlParams = this.getUrlParameters(); - const storageKey = `keystrokes_${urlParams.task_id}_${urlParams.platform_id}`; - - // Only limit if absolutely necessary - const maxKeystrokes = 50000; // Much higher limit - if (this.keyEvents.length > maxKeystrokes) { - console.error(`Critical: Keystroke limit reached (${this.keyEvents.length}). Data may be lost.`); - // For research, you might want to alert the user or auto-submit - alert('Maximum keystroke limit reached. Please submit your post.'); - return; - } - - sessionStorage.setItem(storageKey, JSON.stringify(this.keyEvents)); - } catch (e) { - if (e.name === 'QuotaExceededError') { - console.error('Storage quota exceeded! Cannot save keystrokes.'); - // For research integrity, this is critical - alert the user - alert('Storage limit reached. Please submit your post now to avoid data loss.'); - } - } - }, - - /** - * Load keystrokes from sessionStorage - */ - loadKeystrokes() { - const urlParams = this.getUrlParameters(); - const storageKey = `keystrokes_${urlParams.task_id}_${urlParams.platform_id}`; + hasSubmitted: false, + keyEventsAttached: false, // Track if event listeners are attached + // High-performance keystroke capture + keyBuffer: null, + keyCodeMap: null, + keyCodeIndex: 0, + + useWASM: false, // Will be set to true when WASM loads + wasmCapture: null, // Will hold the WASM capture instance + + initHighPerformanceCapture() { + // Pre-allocate typed arrays for maximum performance + this.keyBuffer = { + types: new Uint8Array(50000), // 1 byte per type + keys: new Uint16Array(50000), // 2 bytes per key code + timestamps: new Float64Array(50000), // 8 bytes per timestamp + index: 0 + }; - try { - const saved = sessionStorage.getItem(storageKey); - if (saved) { - this.keyEvents = JSON.parse(saved); - console.log(`Loaded ${this.keyEvents.length} saved keystrokes`); - } - } catch (e) { - console.error('Failed to load keystrokes:', e); - this.keyEvents = []; // Reset on error - } + // Create key code map + this.keyCodeMap = new Map(); + this.keyCodeIndex = 0; }, - /** - * Clear keystrokes from sessionStorage - */ - clearKeystrokes() { - const urlParams = this.getUrlParameters(); - const storageKey = `keystrokes_${urlParams.task_id}_${urlParams.platform_id}`; - sessionStorage.removeItem(storageKey); - console.log('Cleared saved keystrokes'); - }, + /** * Initialize the platform handler @@ -586,25 +424,14 @@ const PlatformSubmissionHandler = { * @param {function} config.onBeforeSubmit - Optional callback before submission * @param {function} config.onAfterSubmit - Optional callback after successful submission */ - init(config) { + async init(config) { + // Store config first + this.config = config; + // Check if already submitted for this task const urlParams = this.getUrlParameters(); const submissionKey = `submitted_${urlParams.user_id}_${urlParams.task_id}_${urlParams.platform_id}`; this.hasSubmitted = sessionStorage.getItem(submissionKey) === 'true'; - - // Prevent accidental refresh - window.addEventListener('beforeunload', (e) => { - // Only show warning if there's unsaved text - const inputEl = document.getElementById(this.config.textInputId); - if (inputEl && inputEl.value.trim() && !this.hasSubmitted) { - e.preventDefault(); - e.returnValue = 'You have unsaved text. Are you sure you want to leave?'; - return e.returnValue; - } - }); - - // Store config first - this.config = config; if (this.hasSubmitted) { console.log("Task already submitted, disabling form"); @@ -612,13 +439,16 @@ const PlatformSubmissionHandler = { return; } - if (this.isInitialized) { - console.log("Platform handler already initialized, skipping..."); + // Prevent multiple initializations for the same page + const initKey = `initialized_${config.platform}_${urlParams.task_id}`; + if (sessionStorage.getItem(initKey) === 'true' && this.isInitialized) { + console.log("Platform handler already initialized for this task, skipping..."); return; } this.isInitialized = true; this.startTime = Date.now(); + sessionStorage.setItem(initKey, 'true'); console.log(`=== ${config.platform.toUpperCase()} PAGE LOADED ===`); console.log("Current URL:", window.location.href); @@ -631,21 +461,27 @@ const PlatformSubmissionHandler = { return; } - // Start keylogger - this.startKeyLogger(urlParams); - - // Load any saved keystrokes - this.loadKeystrokes(); - - // If we have saved keystrokes but the textarea is empty, clear them - // (user might have cleared the form before refresh) - const inputEl = document.getElementById(this.config.textInputId); - if (inputEl && !inputEl.value.trim() && this.keyEvents.length > 0) { - console.log('Text is empty but keystrokes exist - clearing keystrokes'); - this.keyEvents = []; - this.clearKeystrokes(); + // Check if WASM is available + if (window.wasmKeystrokeManager) { + try { + await window.wasmKeystrokeManager.initialize(); + this.wasmCapture = window.wasmKeystrokeManager; + this.useWASM = true; + console.log('✅ Using WASM for high-performance keystroke capture'); + } catch (error) { + console.error('Failed to initialize WASM, falling back to JavaScript:', error); + this.useWASM = false; + } + } else { + console.log('WASM not available, using JavaScript keystroke capture'); } + // Only attach keylogger if not already attached + if (!this.keyEventsAttached) { + this.startKeyLogger(urlParams); + this.keyEventsAttached = true; + } + // Set up submit button this.setupSubmitButton(urlParams); @@ -653,11 +489,20 @@ const PlatformSubmissionHandler = { this.setupVisibilityHandler(); // Set up paste prevention + const inputEl = document.getElementById(this.config.textInputId); if (inputEl) { inputEl.addEventListener('paste', this.handlePaste.bind(this)); console.log('Paste prevention enabled for', this.config.textInputId); } + // Clean up on page unload + window.addEventListener('beforeunload', () => { + sessionStorage.removeItem(initKey); + if (this.wasmCapture) { + this.wasmCapture.clear(); + } + }); + console.log(`✅ ${config.platform} handler initialized successfully`); }, @@ -678,41 +523,6 @@ const PlatformSubmissionHandler = { submitButton.textContent = "Already Submitted"; } }, - /** - * Save draft text to sessionStorage - */ - saveDraft() { - const inputEl = document.getElementById(this.config.textInputId); - if (inputEl && inputEl.value.trim()) { - const urlParams = this.getUrlParameters(); - const storageKey = `draft_${this.config.platform}_${urlParams.task_id}`; - sessionStorage.setItem(storageKey, inputEl.value); - console.log(`Saved draft for ${storageKey}`); - } - }, - - /** - * Restore draft text from sessionStorage - */ - restoreDraft() { - const urlParams = this.getUrlParameters(); - const storageKey = `draft_${this.config.platform}_${urlParams.task_id}`; - const savedText = sessionStorage.getItem(storageKey); - - if (savedText) { - const inputEl = document.getElementById(this.config.textInputId); - if (inputEl) { - inputEl.value = savedText; - console.log(`Restored draft for ${storageKey}`); - - // Trigger input event for auto-resize - inputEl.dispatchEvent(new Event('input')); - - // Show a message that draft was restored - this.showDraftRestoredMessage(); - } - } - }, /** * Setup visibility handler to handle back/forward navigation @@ -811,46 +621,124 @@ const PlatformSubmissionHandler = { * Start keystroke logging */ startKeyLogger(urlParams) { - const onKeyDown = (e) => { - // CHANGE 1: Capture timestamp IMMEDIATELY - const timestamp = Date.now(); - - // CHANGE 2: Push to array with the pre-captured timestamp - this.keyEvents.push(['P', this.replaceJsKey(e), timestamp]); - - // CHANGE 3: Save keystrokes AFTER the critical timing capture - // Move this to after we've captured the timestamp - setTimeout(() => this.saveKeystrokes(), 0); + if (this.useWASM && this.wasmCapture) { + // Use WASM for capture + console.log('Using WASM keystroke capture'); - // Handle Enter key for multi-line support - if (e.key === "Enter" && e.target.id === this.config.textInputId) { - if (!e.shiftKey) { + document.addEventListener('keydown', (e) => { + this.wasmCapture.captureKeyDown(e); + + // Handle Enter key + if (e.key === "Enter" && e.target.id === this.config.textInputId && !e.shiftKey) { e.preventDefault(); const textarea = e.target; const start = textarea.selectionStart; const end = textarea.selectionEnd; textarea.value = textarea.value.substring(0, start) + '\n' + textarea.value.substring(end); textarea.selectionStart = textarea.selectionEnd = start + 1; - - // Trigger any auto-resize if needed textarea.dispatchEvent(new Event('input')); } - } - }; - - const onKeyUp = (e) => { - // CHANGE 1: Capture timestamp IMMEDIATELY - const timestamp = Date.now(); + }); - // CHANGE 2: Push to array with the pre-captured timestamp - this.keyEvents.push(['R', this.replaceJsKey(e), timestamp]); + document.addEventListener('keyup', (e) => { + this.wasmCapture.captureKeyUp(e); + }); + } else { + // Fallback to JavaScript implementation with physical key tracking + console.log('Using JavaScript keystroke capture with physical key tracking'); - // CHANGE 3: Save keystrokes AFTER the critical timing capture - setTimeout(() => this.saveKeystrokes(), 0); - }; + // Initialize high-performance capture + this.initHighPerformanceCapture(); + + // Add physical key tracking map + this.physicalKeyMap = new Map(); + + const captureEvent = (e, eventType) => { + const timestamp = performance.now(); + const idx = this.keyBuffer.index; + const physicalCode = e.code; + + let keyToStore; + + if (eventType === 0) { // Press event + // Store the display key for this physical key + keyToStore = e.key; + this.physicalKeyMap.set(physicalCode, keyToStore); + } else { // Release event + // Get the stored key from press time + keyToStore = this.physicalKeyMap.get(physicalCode); + + if (!keyToStore) { + console.warn(`No tracked press for code=${physicalCode}, using current key=${e.key}`); + keyToStore = e.key; + } else { + // Remove from tracking + this.physicalKeyMap.delete(physicalCode); + } + } + + // Get or create key code + let keyCode = this.keyCodeMap.get(keyToStore); + if (keyCode === undefined) { + keyCode = this.keyCodeIndex++; + this.keyCodeMap.set(keyToStore, keyCode); + } + + // Store in typed arrays + this.keyBuffer.types[idx] = eventType; + this.keyBuffer.keys[idx] = keyCode; + this.keyBuffer.timestamps[idx] = timestamp; + this.keyBuffer.index++; + + // Handle Enter key + if (e.key === "Enter" && e.target.id === this.config.textInputId && !e.shiftKey) { + e.preventDefault(); + requestAnimationFrame(() => { + const textarea = e.target; + const start = textarea.selectionStart; + const end = textarea.selectionEnd; + textarea.value = textarea.value.substring(0, start) + '\n' + textarea.value.substring(end); + textarea.selectionStart = textarea.selectionEnd = start + 1; + textarea.dispatchEvent(new Event('input')); + }); + } + }; + + document.addEventListener('keydown', (e) => captureEvent(e, 0), { passive: false }); + document.addEventListener('keyup', (e) => captureEvent(e, 1), { passive: true }); + } + }, - document.addEventListener('keydown', onKeyDown); - document.addEventListener('keyup', onKeyUp); + getKeystrokeData() { + if (this.useWASM && this.wasmCapture) { + // Get data from WASM + return this.wasmCapture.getRawData(); + } else { + // Original implementation + const events = []; + const keyMap = Array.from(this.keyCodeMap.entries()); + + for (let i = 0; i < this.keyBuffer.index; i++) { + const keyEntry = keyMap.find(([_, code]) => code === this.keyBuffer.keys[i]); + if (keyEntry) { + const originalKey = keyEntry[0]; + + // Create a more complete fake event object for replaceJsKey + const fakeEvent = { + key: originalKey, + code: originalKey === ' ' ? 'Space' : `Key${originalKey.toUpperCase()}` + }; + + events.push([ + this.keyBuffer.types[i] === 0 ? 'P' : 'R', + this.replaceJsKey(fakeEvent), + Math.round(this.keyBuffer.timestamps[i] + performance.timeOrigin) + ]); + } + } + + return events; + } }, /** @@ -935,9 +823,7 @@ const PlatformSubmissionHandler = { const submissionKey = `submitted_${urlParams.user_id}_${urlParams.task_id}_${urlParams.platform_id}`; sessionStorage.setItem(submissionKey, 'true'); this.hasSubmitted = true; - // Clear keystroke data - this.clearKeystrokes(); - + // Call after submit callback if provided if (this.config.onAfterSubmit) { this.config.onAfterSubmit(); @@ -974,8 +860,16 @@ const PlatformSubmissionHandler = { }; } - if (this.keyEvents.length === 0) { - return { isValid: false, message: 'No keystrokes recorded! Please type something before submitting.' }; + // Check keystroke data + if (this.useWASM && this.wasmCapture) { + if (this.wasmCapture.getEventCount() === 0) { + return { isValid: false, message: 'No keystrokes recorded! Please type something before submitting.' }; + } + } else { + // Check the buffer instead of the text input + if (!this.keyBuffer || this.keyBuffer.index === 0) { + return { isValid: false, message: 'No keystrokes recorded! Please type something before submitting.' }; + } } return { isValid: true }; @@ -1056,20 +950,44 @@ const PlatformSubmissionHandler = { 'CapsLock': 'Key.caps_lock' }; - if (e.code === 'Space') return 'Key.space'; - return keyMap[e.key] || e.key; + // Handle space key - check both e.code and e.key + if (e.code === 'Space' || e.key === ' ') return 'Key.space'; + if (e.code === 'Comma' || e.key == ',' ) return 'Key.comma'; + + // Check if it's a mapped key + if (keyMap[e.key]) return keyMap[e.key]; + + // Return the original key + return e.key; }, /** * Build CSV blob from keystroke events */ buildCsvBlob() { - const heading = [['Press or Release', 'Key', 'Time']]; - const csvString = heading - .concat(this.keyEvents) - .map(row => row.join(',')) - .join('\n'); - return new Blob([csvString], { type: 'text/csv;charset=utf-8' }); + console.log('=== Building CSV ==='); + console.log('useWASM:', this.useWASM); + console.log('wasmCapture:', this.wasmCapture); + + if (this.useWASM && this.wasmCapture) { + console.log('✅ Using WASM export'); + // Get CSV directly from WASM + const csvString = this.wasmCapture.exportAsCSV(); + console.log('First few lines of WASM CSV:', csvString.split('\n').slice(0, 5)); + return new Blob([csvString], { type: 'text/csv;charset=utf-8' }); + } else { + console.log('❌ Using JavaScript export'); + // Original implementation + // Convert high-performance buffer to array format only when needed + this.keyEvents = this.getKeystrokeData(); + + const heading = [['Press or Release', 'Key', 'Time']]; + const csvString = heading + .concat(this.keyEvents) + .map(row => row.join(',')) + .join('\n'); + return new Blob([csvString], { type: 'text/csv;charset=utf-8' }); + } }, /** @@ -1082,15 +1000,6 @@ const PlatformSubmissionHandler = { const deviceInfoStr = sessionStorage.getItem('device_info'); const deviceInfo = deviceInfoStr ? JSON.parse(deviceInfoStr) : DeviceDetector.getDeviceInfo(); - // const metadata = { - // user_id: urlParams.user_id, - // platform_id: urlParams.platform_id, - // task_id: urlParams.task_id, - // start_time: this.startTime, - // end_time: endTime, - // duration_ms: endTime - this.startTime, - // platform: this.config.platform - // }; const metadata = { user_id: urlParams.user_id, platform_id: urlParams.platform_id, diff --git a/utils/common.js.backup b/utils/common.js.backup index 33eafde..36f2dd0 100644 --- a/utils/common.js.backup +++ b/utils/common.js.backup @@ -219,130 +219,130 @@ class APIClient { /** * Enhanced keylogger with memory management and performance optimization */ -class EnhancedKeyLogger { - constructor(userId, platformId) { - this.userId = userId; - this.platformId = platformId; - this.keyEvents = []; - this.maxEvents = 10000; // Prevent memory leaks - this.isActive = false; +// class EnhancedKeyLogger { +// constructor(userId, platformId) { +// this.userId = userId; +// this.platformId = platformId; +// this.keyEvents = []; +// this.maxEvents = 10000; // Prevent memory leaks +// this.isActive = false; - // Bind methods to preserve context - this.handleKeyDown = this.handleKeyDown.bind(this); - this.handleKeyUp = this.handleKeyUp.bind(this); - } +// // Bind methods to preserve context +// this.handleKeyDown = this.handleKeyDown.bind(this); +// this.handleKeyUp = this.handleKeyUp.bind(this); +// } - start() { - if (this.isActive) return; +// start() { +// if (this.isActive) return; - this.isActive = true; - document.addEventListener('keydown', this.handleKeyDown); - document.addEventListener('keyup', this.handleKeyUp); +// this.isActive = true; +// document.addEventListener('keydown', this.handleKeyDown); +// document.addEventListener('keyup', this.handleKeyUp); - this.createDownloadButton(); - console.log('Keylogger started'); - } +// this.createDownloadButton(); +// console.log('Keylogger started'); +// } - stop() { - if (!this.isActive) return; +// stop() { +// if (!this.isActive) return; - this.isActive = false; - document.removeEventListener('keydown', this.handleKeyDown); - document.removeEventListener('keyup', this.handleKeyUp); +// this.isActive = false; +// document.removeEventListener('keydown', this.handleKeyDown); +// document.removeEventListener('keyup', this.handleKeyUp); - const button = document.getElementById('keylogger-download-btn'); - if (button) button.remove(); +// const button = document.getElementById('keylogger-download-btn'); +// if (button) button.remove(); - console.log('Keylogger stopped'); - } - - handleKeyDown(event) { - this.addKeyEvent('P', event.key); - this.saveKeystrokes(); - } - - handleKeyUp(event) { - this.addKeyEvent('R', event.key); - this.saveKeystrokes(); - } - - addKeyEvent(type, key) { - // Prevent memory leaks by limiting array size - if (this.keyEvents.length >= this.maxEvents) { - this.keyEvents = this.keyEvents.slice(-this.maxEvents / 2); // Keep last half - console.warn('Keylogger array truncated to prevent memory issues'); - } +// console.log('Keylogger stopped'); +// } + +// handleKeyDown(event) { +// this.addKeyEvent('P', event.key); +// this.saveKeystrokes(); +// } + +// handleKeyUp(event) { +// this.addKeyEvent('R', event.key); +// this.saveKeystrokes(); +// } + +// addKeyEvent(type, key) { +// // Prevent memory leaks by limiting array size +// if (this.keyEvents.length >= this.maxEvents) { +// this.keyEvents = this.keyEvents.slice(-this.maxEvents / 2); // Keep last half +// console.warn('Keylogger array truncated to prevent memory issues'); +// } - this.keyEvents.push([type, key, Date.now()]); - } - - createDownloadButton() { - const button = document.createElement('button'); - button.id = 'keylogger-download-btn'; - button.textContent = 'Download Keylog'; - button.style.cssText = ` - position: fixed; - bottom: 10px; - right: 10px; - background: #333; - color: white; - border: none; - padding: 10px 15px; - border-radius: 5px; - cursor: pointer; - z-index: 10000; - font-family: Arial, sans-serif; - font-size: 12px; - `; +// this.keyEvents.push([type, key, Date.now()]); +// } + +// createDownloadButton() { +// const button = document.createElement('button'); +// button.id = 'keylogger-download-btn'; +// button.textContent = 'Download Keylog'; +// button.style.cssText = ` +// position: fixed; +// bottom: 10px; +// right: 10px; +// background: #333; +// color: white; +// border: none; +// padding: 10px 15px; +// border-radius: 5px; +// cursor: pointer; +// z-index: 10000; +// font-family: Arial, sans-serif; +// font-size: 12px; +// `; - button.onclick = () => this.downloadKeylog(); - document.body.appendChild(button); - } - - downloadKeylog() { - try { - const platformLetters = { 0: 'f', 1: 'i', 2: 't' }; - const platformLetter = platformLetters[this.platformId] || 'unknown'; - const filename = `${platformLetter}_${this.userId}.csv`; +// button.onclick = () => this.downloadKeylog(); +// document.body.appendChild(button); +// } + +// downloadKeylog() { +// try { +// const platformLetters = { 0: 'f', 1: 'i', 2: 't' }; +// const platformLetter = platformLetters[this.platformId] || 'unknown'; +// const filename = `${platformLetter}_${this.userId}.csv`; - const header = [['Press or Release', 'Key', 'Time']]; - const csvData = header.concat(this.keyEvents); - const csvString = csvData.map(row => row.join(',')).join('\n'); +// const header = [['Press or Release', 'Key', 'Time']]; +// const csvData = header.concat(this.keyEvents); +// const csvString = csvData.map(row => row.join(',')).join('\n'); - const blob = new Blob([csvString], { type: 'text/csv;charset=utf-8;' }); +// const blob = new Blob([csvString], { type: 'text/csv;charset=utf-8;' }); - // Modern download approach - const link = document.createElement('a'); - const url = URL.createObjectURL(blob); +// // Modern download approach +// const link = document.createElement('a'); +// const url = URL.createObjectURL(blob); - link.setAttribute('href', url); - link.setAttribute('download', filename); - link.style.visibility = 'hidden'; +// link.setAttribute('href', url); +// link.setAttribute('download', filename); +// link.style.visibility = 'hidden'; - document.body.appendChild(link); - link.click(); - document.body.removeChild(link); +// document.body.appendChild(link); +// link.click(); +// document.body.removeChild(link); - // Clean up object URL - URL.revokeObjectURL(url); +// // Clean up object URL +// URL.revokeObjectURL(url); - console.log(`Keylog downloaded: ${filename}`); +// console.log(`Keylog downloaded: ${filename}`); - } catch (error) { - console.error('Failed to download keylog:', error); - FormValidator.showError('Failed to download keylog. Please try again.'); - } - } - - getEventCount() { - return this.keyEvents.length; - } - - clearEvents() { - this.keyEvents = []; - console.log('Keylog events cleared'); - } -} +// } catch (error) { +// console.error('Failed to download keylog:', error); +// FormValidator.showError('Failed to download keylog. Please try again.'); +// } +// } + +// getEventCount() { +// return this.keyEvents.length; +// } + +// clearEvents() { +// this.keyEvents = []; +// console.log('Keylog events cleared'); +// } +// } /** * Navigation utilities with proper URL handling @@ -812,8 +812,15 @@ const PlatformSubmissionHandler = { */ startKeyLogger(urlParams) { const onKeyDown = (e) => { - - this.keyEvents.push(['P', this.replaceJsKey(e), Date.now()]); + // CHANGE 1: Capture timestamp IMMEDIATELY + const timestamp = Date.now(); + + // CHANGE 2: Push to array with the pre-captured timestamp + this.keyEvents.push(['P', this.replaceJsKey(e), timestamp]); + + // CHANGE 3: Save keystrokes AFTER the critical timing capture + // Move this to after we've captured the timestamp + setTimeout(() => this.saveKeystrokes(), 0); // Handle Enter key for multi-line support if (e.key === "Enter" && e.target.id === this.config.textInputId) { @@ -832,12 +839,19 @@ const PlatformSubmissionHandler = { }; const onKeyUp = (e) => { - this.keyEvents.push(['R', this.replaceJsKey(e), Date.now()]); + // CHANGE 1: Capture timestamp IMMEDIATELY + const timestamp = Date.now(); + + // CHANGE 2: Push to array with the pre-captured timestamp + this.keyEvents.push(['R', this.replaceJsKey(e), timestamp]); + + // CHANGE 3: Save keystrokes AFTER the critical timing capture + setTimeout(() => this.saveKeystrokes(), 0); }; document.addEventListener('keydown', onKeyDown); document.addEventListener('keyup', onKeyUp); - }, + }, /** * Setup submit button handler diff --git a/utils/wasm-keystroke.js b/utils/wasm-keystroke.js new file mode 100644 index 0000000..f1c984d --- /dev/null +++ b/utils/wasm-keystroke.js @@ -0,0 +1,284 @@ +// utils/wasm-keystroke.js +import init, { KeystrokeCapture } from '../wasm-keystroke-capture/pkg/keystroke_capture.js'; + +class WASMKeystrokeManager { + constructor() { + this.initialized = false; + this.initializing = false; // Prevent multiple init calls + this.capture = null; + this.keyPressMap = new Map(); + this.eventQueue = []; // Queue for ensuring order + this.processing = false; + + this.keyMapping = { + 'Shift': 'Key.shift', + 'Control': 'Key.ctrl', + 'Alt': 'Key.alt', + 'Meta': 'Key.cmd', + 'Enter': 'Key.enter', + 'Backspace': 'Key.backspace', + 'Escape': 'Key.esc', + 'Tab': 'Key.tab', + 'ArrowLeft': 'Key.left', + 'ArrowRight': 'Key.right', + 'ArrowUp': 'Key.up', + 'ArrowDown': 'Key.down', + 'CapsLock': 'Key.caps_lock', + ' ': 'Key.space', + ',': 'Key.comma' + }; + } + + async initialize() { + if (this.initialized) { + console.log('WASM already initialized, skipping'); + return; + } + + if (this.initializing) { + console.log('WASM initialization already in progress, waiting...'); + // Wait for initialization to complete + while (this.initializing) { + await new Promise(resolve => setTimeout(resolve, 10)); + } + return; + } + + this.initializing = true; + + try { + await init(); + this.capture = new KeystrokeCapture(50000); + this.initialized = true; + console.log('✅ WASM keystroke capture initialized'); + } catch (error) { + console.error('❌ Failed to initialize WASM:', error); + throw error; + } finally { + this.initializing = false; + } + } + + mapKey(key) { + return this.keyMapping[key] || key; + } + + // Process events in order + async processEventQueue() { + if (this.processing || this.eventQueue.length === 0) return; + + this.processing = true; + + while (this.eventQueue.length > 0) { + const event = this.eventQueue.shift(); + + try { + if (event.type === 'down') { + this.capture.capture_keystroke(event.key, false); + } else { + this.capture.capture_keystroke(event.key, true); + } + } catch (error) { + console.error(`Failed to capture ${event.type}:`, error); + // If WASM is corrupted, try to reinitialize + if (error.message && error.message.includes('unreachable')) { + console.error('WASM module corrupted, attempting recovery...'); + this.initialized = false; + this.capture = null; + try { + await this.initialize(); + } catch (e) { + console.error('Failed to recover WASM:', e); + } + } + } + } + + this.processing = false; + } + + captureKeyDown(event) { + const timestamp = performance.now(); + if (!this.initialized) { + console.warn('WASM not initialized'); + return; + } + + const physicalCode = event.code; + const displayKey = event.key; + const mappedKey = this.mapKey(displayKey); + + // Check if this key is already pressed (prevent duplicate press events) + if (this.keyPressMap.has(physicalCode)) { + console.log(`Ignoring duplicate keydown for ${physicalCode}`); + return; + } + + // Store the display key for this physical key + this.keyPressMap.set(physicalCode, { + key: mappedKey, + timestamp: performance.now() + }); + + // Debug logging + console.log(`KeyDown: code=${physicalCode}, key=${displayKey}, mapped=${mappedKey}`); + + // Queue the event + this.eventQueue.push({ + type: 'down', + key: mappedKey, + code: physicalCode, + timestamp: timestamp + }); + + // Process queue + this.processEventQueue(); + } + + captureKeyUp(event) { + const timestamp = performance.now(); + if (!this.initialized) { + console.warn('WASM not initialized'); + return; + } + + const physicalCode = event.code; + + // Get the stored key from when it was pressed + const pressData = this.keyPressMap.get(physicalCode); + + if (!pressData) { + console.warn(`No tracked press for code=${physicalCode}, ignoring release`); + return; + } + + const mappedKey = pressData.key; + + // Debug logging + console.log(`KeyUp: code=${physicalCode}, stored=${mappedKey}, current=${event.key}`); + + // Remove from tracking + this.keyPressMap.delete(physicalCode); + + // Queue the event + this.eventQueue.push({ + type: 'up', + key: mappedKey, + code: physicalCode, + timestamp: timestamp + }); + + // Process queue + this.processEventQueue(); + } + + getEventCount() { + if (!this.capture || !this.initialized) return 0; + try { + return this.capture.get_event_count(); + } catch (error) { + console.error('Failed to get event count:', error); + return 0; + } + } + + exportAsCSV() { + if (!this.capture || !this.initialized) return ''; + try { + return this.capture.export_as_csv(); + } catch (error) { + console.error('Failed to export CSV:', error); + return ''; + } + } + + getRawData() { + if (!this.capture || !this.initialized) return []; + try { + return this.capture.get_raw_data(); + } catch (error) { + console.error('Failed to get raw data:', error); + return []; + } + } + + clear() { + if (this.capture && this.initialized) { + try { + this.capture.clear(); + } catch (error) { + console.error('Failed to clear capture:', error); + } + } + // Always clear JavaScript state + this.keyPressMap.clear(); + this.eventQueue = []; + } + + // Debug method to check for unreleased keys + getUnreleasedKeys() { + return Array.from(this.keyPressMap.entries()); + } + + // Reset the entire WASM module + async reset() { + console.log('Resetting WASM keystroke capture...'); + this.capture = null; + this.initialized = false; + this.keyPressMap.clear(); + this.eventQueue = []; + await this.initialize(); + } +} + +// Export singleton instance +let wasmKeystrokeManagerInstance = null; + +export const wasmKeystrokeManager = { + async initialize() { + if (!wasmKeystrokeManagerInstance) { + wasmKeystrokeManagerInstance = new WASMKeystrokeManager(); + } + return wasmKeystrokeManagerInstance.initialize(); + }, + + captureKeyDown(event) { + if (!wasmKeystrokeManagerInstance) return; + return wasmKeystrokeManagerInstance.captureKeyDown(event); + }, + + captureKeyUp(event) { + if (!wasmKeystrokeManagerInstance) return; + return wasmKeystrokeManagerInstance.captureKeyUp(event); + }, + + getEventCount() { + if (!wasmKeystrokeManagerInstance) return 0; + return wasmKeystrokeManagerInstance.getEventCount(); + }, + + exportAsCSV() { + if (!wasmKeystrokeManagerInstance) return ''; + return wasmKeystrokeManagerInstance.exportAsCSV(); + }, + + getRawData() { + if (!wasmKeystrokeManagerInstance) return []; + return wasmKeystrokeManagerInstance.getRawData(); + }, + + clear() { + if (!wasmKeystrokeManagerInstance) return; + return wasmKeystrokeManagerInstance.clear(); + }, + + getUnreleasedKeys() { + if (!wasmKeystrokeManagerInstance) return []; + return wasmKeystrokeManagerInstance.getUnreleasedKeys(); + }, + + async reset() { + if (!wasmKeystrokeManagerInstance) return; + return wasmKeystrokeManagerInstance.reset(); + } +}; \ No newline at end of file diff --git a/wasm-keystroke-capture/Cargo.toml b/wasm-keystroke-capture/Cargo.toml new file mode 100644 index 0000000..d9c2c55 --- /dev/null +++ b/wasm-keystroke-capture/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "keystroke-capture" +version = "0.1.0" +authors = ["Your Name"] +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +wasm-bindgen = "0.2" +web-sys = { version = "0.3", features = ["Performance", "Window"] } +js-sys = "0.3" + +[profile.release] +# Optimize for small binary size +opt-level = "z" +lto = true + diff --git a/wasm-keystroke-capture/pkg/.gitignore b/wasm-keystroke-capture/pkg/.gitignore new file mode 100644 index 0000000..f59ec20 --- /dev/null +++ b/wasm-keystroke-capture/pkg/.gitignore @@ -0,0 +1 @@ +* \ No newline at end of file diff --git a/wasm-keystroke-capture/pkg/keystroke_capture.d.ts b/wasm-keystroke-capture/pkg/keystroke_capture.d.ts new file mode 100644 index 0000000..b905b57 --- /dev/null +++ b/wasm-keystroke-capture/pkg/keystroke_capture.d.ts @@ -0,0 +1,55 @@ +/* tslint:disable */ +/* eslint-disable */ +export class KeystrokeCapture { + free(): void; + constructor(capacity: number); + capture_keystroke(key: string, is_release: boolean): void; + get_event_count(): number; + export_as_csv(): string; + clear(): void; + get_raw_data(): any; + get_last_10_events(): string; +} + +export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; + +export interface InitOutput { + readonly memory: WebAssembly.Memory; + readonly __wbg_keystrokecapture_free: (a: number, b: number) => void; + readonly keystrokecapture_new: (a: number) => [number, number, number]; + readonly keystrokecapture_capture_keystroke: (a: number, b: number, c: number, d: number) => [number, number]; + readonly keystrokecapture_get_event_count: (a: number) => number; + readonly keystrokecapture_export_as_csv: (a: number) => [number, number]; + readonly keystrokecapture_clear: (a: number) => void; + readonly keystrokecapture_get_raw_data: (a: number) => [number, number, number]; + readonly keystrokecapture_get_last_10_events: (a: number) => [number, number]; + readonly __wbindgen_exn_store: (a: number) => void; + readonly __externref_table_alloc: () => number; + readonly __wbindgen_export_2: WebAssembly.Table; + readonly __externref_table_dealloc: (a: number) => void; + readonly __wbindgen_malloc: (a: number, b: number) => number; + readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number; + readonly __wbindgen_free: (a: number, b: number, c: number) => void; + readonly __wbindgen_start: () => void; +} + +export type SyncInitInput = BufferSource | WebAssembly.Module; +/** +* Instantiates the given `module`, which can either be bytes or +* a precompiled `WebAssembly.Module`. +* +* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated. +* +* @returns {InitOutput} +*/ +export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput; + +/** +* If `module_or_path` is {RequestInfo} or {URL}, makes a request and +* for everything else, calls `WebAssembly.instantiate` directly. +* +* @param {{ module_or_path: InitInput | Promise }} module_or_path - Passing `InitInput` directly is deprecated. +* +* @returns {Promise} +*/ +export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise } | InitInput | Promise): Promise; diff --git a/wasm-keystroke-capture/pkg/keystroke_capture.js b/wasm-keystroke-capture/pkg/keystroke_capture.js new file mode 100644 index 0000000..478a2f2 --- /dev/null +++ b/wasm-keystroke-capture/pkg/keystroke_capture.js @@ -0,0 +1,376 @@ +let wasm; + +function addToExternrefTable0(obj) { + const idx = wasm.__externref_table_alloc(); + wasm.__wbindgen_export_2.set(idx, obj); + return idx; +} + +function handleError(f, args) { + try { + return f.apply(this, args); + } catch (e) { + const idx = addToExternrefTable0(e); + wasm.__wbindgen_exn_store(idx); + } +} + +const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } ); + +if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); }; + +let cachedUint8ArrayMemory0 = null; + +function getUint8ArrayMemory0() { + if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { + cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8ArrayMemory0; +} + +function getStringFromWasm0(ptr, len) { + ptr = ptr >>> 0; + return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); +} + +function isLikeNone(x) { + return x === undefined || x === null; +} + +function takeFromExternrefTable0(idx) { + const value = wasm.__wbindgen_export_2.get(idx); + wasm.__externref_table_dealloc(idx); + return value; +} + +let WASM_VECTOR_LEN = 0; + +const cachedTextEncoder = (typeof TextEncoder !== 'undefined' ? new TextEncoder('utf-8') : { encode: () => { throw Error('TextEncoder not available') } } ); + +const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' + ? function (arg, view) { + return cachedTextEncoder.encodeInto(arg, view); +} + : function (arg, view) { + const buf = cachedTextEncoder.encode(arg); + view.set(buf); + return { + read: arg.length, + written: buf.length + }; +}); + +function passStringToWasm0(arg, malloc, realloc) { + + if (realloc === undefined) { + const buf = cachedTextEncoder.encode(arg); + const ptr = malloc(buf.length, 1) >>> 0; + getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf); + WASM_VECTOR_LEN = buf.length; + return ptr; + } + + let len = arg.length; + let ptr = malloc(len, 1) >>> 0; + + const mem = getUint8ArrayMemory0(); + + let offset = 0; + + for (; offset < len; offset++) { + const code = arg.charCodeAt(offset); + if (code > 0x7F) break; + mem[ptr + offset] = code; + } + + if (offset !== len) { + if (offset !== 0) { + arg = arg.slice(offset); + } + ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; + const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len); + const ret = encodeString(arg, view); + + offset += ret.written; + ptr = realloc(ptr, len, offset, 1) >>> 0; + } + + WASM_VECTOR_LEN = offset; + return ptr; +} + +const KeystrokeCaptureFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_keystrokecapture_free(ptr >>> 0, 1)); + +export class KeystrokeCapture { + + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + KeystrokeCaptureFinalization.unregister(this); + return ptr; + } + + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_keystrokecapture_free(ptr, 0); + } + /** + * @param {number} capacity + */ + constructor(capacity) { + const ret = wasm.keystrokecapture_new(capacity); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + this.__wbg_ptr = ret[0] >>> 0; + KeystrokeCaptureFinalization.register(this, this.__wbg_ptr, this); + return this; + } + /** + * @param {string} key + * @param {boolean} is_release + */ + capture_keystroke(key, is_release) { + const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.keystrokecapture_capture_keystroke(this.__wbg_ptr, ptr0, len0, is_release); + if (ret[1]) { + throw takeFromExternrefTable0(ret[0]); + } + } + /** + * @returns {number} + */ + get_event_count() { + const ret = wasm.keystrokecapture_get_event_count(this.__wbg_ptr); + return ret >>> 0; + } + /** + * @returns {string} + */ + export_as_csv() { + let deferred1_0; + let deferred1_1; + try { + const ret = wasm.keystrokecapture_export_as_csv(this.__wbg_ptr); + deferred1_0 = ret[0]; + deferred1_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } + } + clear() { + wasm.keystrokecapture_clear(this.__wbg_ptr); + } + /** + * @returns {any} + */ + get_raw_data() { + const ret = wasm.keystrokecapture_get_raw_data(this.__wbg_ptr); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return takeFromExternrefTable0(ret[0]); + } + /** + * @returns {string} + */ + get_last_10_events() { + let deferred1_0; + let deferred1_1; + try { + const ret = wasm.keystrokecapture_get_last_10_events(this.__wbg_ptr); + deferred1_0 = ret[0]; + deferred1_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } + } +} + +async function __wbg_load(module, imports) { + if (typeof Response === 'function' && module instanceof Response) { + if (typeof WebAssembly.instantiateStreaming === 'function') { + try { + return await WebAssembly.instantiateStreaming(module, imports); + + } catch (e) { + if (module.headers.get('Content-Type') != 'application/wasm') { + console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); + + } else { + throw e; + } + } + } + + const bytes = await module.arrayBuffer(); + return await WebAssembly.instantiate(bytes, imports); + + } else { + const instance = await WebAssembly.instantiate(module, imports); + + if (instance instanceof WebAssembly.Instance) { + return { instance, module }; + + } else { + return instance; + } + } +} + +function __wbg_get_imports() { + const imports = {}; + imports.wbg = {}; + imports.wbg.__wbg_call_672a4d21634d4a24 = function() { return handleError(function (arg0, arg1) { + const ret = arg0.call(arg1); + return ret; + }, arguments) }; + imports.wbg.__wbg_instanceof_Window_def73ea0955fc569 = function(arg0) { + let result; + try { + result = arg0 instanceof Window; + } catch (_) { + result = false; + } + const ret = result; + return ret; + }; + imports.wbg.__wbg_new_78feb108b6472713 = function() { + const ret = new Array(); + return ret; + }; + imports.wbg.__wbg_newnoargs_105ed471475aaf50 = function(arg0, arg1) { + const ret = new Function(getStringFromWasm0(arg0, arg1)); + return ret; + }; + imports.wbg.__wbg_now_d18023d54d4e5500 = function(arg0) { + const ret = arg0.now(); + return ret; + }; + imports.wbg.__wbg_performance_c185c0cdc2766575 = function(arg0) { + const ret = arg0.performance; + return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); + }; + imports.wbg.__wbg_push_737cfc8c1432c2c6 = function(arg0, arg1) { + const ret = arg0.push(arg1); + return ret; + }; + imports.wbg.__wbg_static_accessor_GLOBAL_88a902d13a557d07 = function() { + const ret = typeof global === 'undefined' ? null : global; + return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); + }; + imports.wbg.__wbg_static_accessor_GLOBAL_THIS_56578be7e9f832b0 = function() { + const ret = typeof globalThis === 'undefined' ? null : globalThis; + return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); + }; + imports.wbg.__wbg_static_accessor_SELF_37c5d418e4bf5819 = function() { + const ret = typeof self === 'undefined' ? null : self; + return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); + }; + imports.wbg.__wbg_static_accessor_WINDOW_5de37043a91a9c40 = function() { + const ret = typeof window === 'undefined' ? null : window; + return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); + }; + imports.wbg.__wbindgen_init_externref_table = function() { + const table = wasm.__wbindgen_export_2; + const offset = table.grow(4); + table.set(0, undefined); + table.set(offset + 0, undefined); + table.set(offset + 1, null); + table.set(offset + 2, true); + table.set(offset + 3, false); + ; + }; + imports.wbg.__wbindgen_is_undefined = function(arg0) { + const ret = arg0 === undefined; + return ret; + }; + imports.wbg.__wbindgen_number_new = function(arg0) { + const ret = arg0; + return ret; + }; + imports.wbg.__wbindgen_string_new = function(arg0, arg1) { + const ret = getStringFromWasm0(arg0, arg1); + return ret; + }; + imports.wbg.__wbindgen_throw = function(arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); + }; + + return imports; +} + +function __wbg_init_memory(imports, memory) { + +} + +function __wbg_finalize_init(instance, module) { + wasm = instance.exports; + __wbg_init.__wbindgen_wasm_module = module; + cachedUint8ArrayMemory0 = null; + + + wasm.__wbindgen_start(); + return wasm; +} + +function initSync(module) { + if (wasm !== undefined) return wasm; + + + if (typeof module !== 'undefined') { + if (Object.getPrototypeOf(module) === Object.prototype) { + ({module} = module) + } else { + console.warn('using deprecated parameters for `initSync()`; pass a single object instead') + } + } + + const imports = __wbg_get_imports(); + + __wbg_init_memory(imports); + + if (!(module instanceof WebAssembly.Module)) { + module = new WebAssembly.Module(module); + } + + const instance = new WebAssembly.Instance(module, imports); + + return __wbg_finalize_init(instance, module); +} + +async function __wbg_init(module_or_path) { + if (wasm !== undefined) return wasm; + + + if (typeof module_or_path !== 'undefined') { + if (Object.getPrototypeOf(module_or_path) === Object.prototype) { + ({module_or_path} = module_or_path) + } else { + console.warn('using deprecated parameters for the initialization function; pass a single object instead') + } + } + + if (typeof module_or_path === 'undefined') { + module_or_path = new URL('keystroke_capture_bg.wasm', import.meta.url); + } + const imports = __wbg_get_imports(); + + if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) { + module_or_path = fetch(module_or_path); + } + + __wbg_init_memory(imports); + + const { instance, module } = await __wbg_load(await module_or_path, imports); + + return __wbg_finalize_init(instance, module); +} + +export { initSync }; +export default __wbg_init; diff --git a/wasm-keystroke-capture/pkg/keystroke_capture_bg.wasm b/wasm-keystroke-capture/pkg/keystroke_capture_bg.wasm new file mode 100644 index 0000000..ac3a1c0 Binary files /dev/null and b/wasm-keystroke-capture/pkg/keystroke_capture_bg.wasm differ diff --git a/wasm-keystroke-capture/pkg/keystroke_capture_bg.wasm.d.ts b/wasm-keystroke-capture/pkg/keystroke_capture_bg.wasm.d.ts new file mode 100644 index 0000000..ab25c25 --- /dev/null +++ b/wasm-keystroke-capture/pkg/keystroke_capture_bg.wasm.d.ts @@ -0,0 +1,19 @@ +/* tslint:disable */ +/* eslint-disable */ +export const memory: WebAssembly.Memory; +export const __wbg_keystrokecapture_free: (a: number, b: number) => void; +export const keystrokecapture_new: (a: number) => [number, number, number]; +export const keystrokecapture_capture_keystroke: (a: number, b: number, c: number, d: number) => [number, number]; +export const keystrokecapture_get_event_count: (a: number) => number; +export const keystrokecapture_export_as_csv: (a: number) => [number, number]; +export const keystrokecapture_clear: (a: number) => void; +export const keystrokecapture_get_raw_data: (a: number) => [number, number, number]; +export const keystrokecapture_get_last_10_events: (a: number) => [number, number]; +export const __wbindgen_exn_store: (a: number) => void; +export const __externref_table_alloc: () => number; +export const __wbindgen_export_2: WebAssembly.Table; +export const __externref_table_dealloc: (a: number) => void; +export const __wbindgen_malloc: (a: number, b: number) => number; +export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number; +export const __wbindgen_free: (a: number, b: number, c: number) => void; +export const __wbindgen_start: () => void; diff --git a/wasm-keystroke-capture/pkg/package.json b/wasm-keystroke-capture/pkg/package.json new file mode 100644 index 0000000..9d928b4 --- /dev/null +++ b/wasm-keystroke-capture/pkg/package.json @@ -0,0 +1,18 @@ +{ + "name": "keystroke-capture", + "type": "module", + "collaborators": [ + "Your Name" + ], + "version": "0.1.0", + "files": [ + "keystroke_capture_bg.wasm", + "keystroke_capture.js", + "keystroke_capture.d.ts" + ], + "main": "keystroke_capture.js", + "types": "keystroke_capture.d.ts", + "sideEffects": [ + "./snippets/*" + ] +} \ No newline at end of file diff --git a/wasm-keystroke-capture/src/lib.rs b/wasm-keystroke-capture/src/lib.rs new file mode 100644 index 0000000..e6a5081 --- /dev/null +++ b/wasm-keystroke-capture/src/lib.rs @@ -0,0 +1,151 @@ +use wasm_bindgen::prelude::*; +use web_sys::{Performance, Window}; + +#[wasm_bindgen] +pub struct KeystrokeCapture { + timestamps: Vec, + keys: Vec, + event_types: Vec, // 0 for press, 1 for release + capacity: usize, +} + +#[wasm_bindgen] +impl KeystrokeCapture { + #[wasm_bindgen(constructor)] + pub fn new(capacity: usize) -> Result { + // Just validate that we can access performance + let window = web_sys::window() + .ok_or_else(|| JsValue::from_str("No window object available"))?; + let _ = window.performance() + .ok_or_else(|| JsValue::from_str("No performance object available"))?; + + Ok(KeystrokeCapture { + timestamps: Vec::with_capacity(capacity), + keys: Vec::with_capacity(capacity), + event_types: Vec::with_capacity(capacity), + capacity, + }) + } + + // Helper function to get performance.now() + fn get_timestamp() -> Result { + let window = web_sys::window() + .ok_or_else(|| JsValue::from_str("No window object available"))?; + let performance = window.performance() + .ok_or_else(|| JsValue::from_str("No performance object available"))?; + Ok(performance.now()) + } + + #[wasm_bindgen] + pub fn capture_keystroke(&mut self, key: String, is_release: bool) -> Result<(), JsValue> { + if self.timestamps.len() >= self.capacity { + return Err(JsValue::from_str("Capacity exceeded")); + } + + // Get timestamp immediately when called + let timestamp = Self::get_timestamp()?; + + self.timestamps.push(timestamp); + self.keys.push(key); + self.event_types.push(if is_release { 1 } else { 0 }); + + Ok(()) + } + + #[wasm_bindgen] + pub fn get_event_count(&self) -> usize { + self.timestamps.len() + } + + #[wasm_bindgen] + pub fn export_as_csv(&self) -> String { + let mut csv = String::from("Press or Release,Key,Time\n"); + + for i in 0..self.timestamps.len() { + let event_type = if self.event_types[i] == 0 { "P" } else { "R" }; + let timestamp = (self.timestamps[i] + 1735660000000.0) as u64; + + // Handle special characters + let key = match self.keys[i].as_str() { + "," => "Key.comma", + k => k, + }; + + csv.push_str(&format!("{},{},{}\n", + event_type, + key, + timestamp + )); + } + + csv + } + + #[wasm_bindgen] + pub fn clear(&mut self) { + self.timestamps.clear(); + self.keys.clear(); + self.event_types.clear(); + } + + #[wasm_bindgen] + pub fn get_raw_data(&self) -> Result { + let result = js_sys::Array::new(); + + for i in 0..self.timestamps.len() { + let entry = js_sys::Array::new(); + entry.push(&JsValue::from_str(if self.event_types[i] == 0 { "P" } else { "R" })); + + // Handle comma + let key = match self.keys[i].as_str() { + "," => "Key.comma", + k => k, + }; + entry.push(&JsValue::from_str(key)); + + entry.push(&JsValue::from_f64(self.timestamps[i] + 1735660000000.0)); + result.push(&entry); + } + + Ok(result.into()) + } + + #[wasm_bindgen] + pub fn get_last_10_events(&self) -> String { + let start = if self.timestamps.len() > 10 { + self.timestamps.len() - 10 + } else { + 0 + }; + + let mut result = String::new(); + for i in start..self.timestamps.len() { + let event_type = if self.event_types[i] == 0 { "P" } else { "R" }; + result.push_str(&format!("{} {} {:.0}\n", + event_type, + &self.keys[i], + self.timestamps[i] + )); + } + result + } + + // Debug method to check timing precision + #[wasm_bindgen] + pub fn test_timing_precision() -> Result { + let mut times = Vec::new(); + for _ in 0..10 { + times.push(Self::get_timestamp()?); + } + + let mut deltas = Vec::new(); + for i in 1..times.len() { + deltas.push(times[i] - times[i-1]); + } + + let min_delta = deltas.iter().fold(f64::INFINITY, |a, &b| a.min(b)); + let max_delta = deltas.iter().fold(0.0, |a, &b| a.max(b)); + + Ok(format!("Timing test - Min delta: {:.3}ms, Max delta: {:.3}ms", min_delta, max_delta)) + } +} \ No newline at end of file