83 lines
2.5 KiB
Markdown
83 lines
2.5 KiB
Markdown
|
|
# JavaScript String Escaping Bug - FIXED
|
||
|
|
|
||
|
|
## Issue
|
||
|
|
|
||
|
|
When editing memories that contained quotes, newlines, or other special characters, the Edit button would throw:
|
||
|
|
```
|
||
|
|
Uncaught SyntaxError: "" literal not terminated before end of script
|
||
|
|
```
|
||
|
|
|
||
|
|
## Root Cause
|
||
|
|
|
||
|
|
The original code passed JSON data directly into the HTML onclick attribute:
|
||
|
|
```javascript
|
||
|
|
const factData = JSON.stringify(fact).replace(/'/g, "\\'");
|
||
|
|
html += `<button onclick='showEditMemoryModal("declarative", "${fact.id}", ${factData})'>✏️</button>`;
|
||
|
|
```
|
||
|
|
|
||
|
|
**Problem:** When the fact content contained characters like:
|
||
|
|
- Double quotes: `"My favorite song is "Monitoring""`
|
||
|
|
- Newlines: `"Line 1\nLine 2"`
|
||
|
|
- Backslashes: `"Path: C:\Users\Name"`
|
||
|
|
|
||
|
|
The escaping with `.replace(/'/g, "\\'")` only handled single quotes, causing syntax errors in the generated HTML.
|
||
|
|
|
||
|
|
## Solution
|
||
|
|
|
||
|
|
**Encode the data using `encodeURIComponent`:**
|
||
|
|
```javascript
|
||
|
|
const factDataEncoded = encodeURIComponent(JSON.stringify(fact));
|
||
|
|
html += `<button onclick='showEditMemoryModal("declarative", "${fact.id}", "${factDataEncoded}")'>✏️</button>`;
|
||
|
|
```
|
||
|
|
|
||
|
|
**Decode in the modal function:**
|
||
|
|
```javascript
|
||
|
|
function showEditMemoryModal(collection, pointId, memoryData) {
|
||
|
|
const decodedData = typeof memoryData === 'string' ? decodeURIComponent(memoryData) : memoryData;
|
||
|
|
const memory = typeof decodedData === 'string' ? JSON.parse(decodedData) : decodedData;
|
||
|
|
// ... rest of function
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
## Why This Works
|
||
|
|
|
||
|
|
- `encodeURIComponent()` converts all special characters to URL-safe percent-encoded format
|
||
|
|
- The encoded string is safe to use in HTML attributes
|
||
|
|
- `decodeURIComponent()` converts it back to the original JSON string
|
||
|
|
- `JSON.parse()` then converts it to a JavaScript object
|
||
|
|
|
||
|
|
## Files Modified
|
||
|
|
|
||
|
|
- `/bot/static/index.html`:
|
||
|
|
- Line ~5356: `loadFacts()` function - changed factData to factDataEncoded
|
||
|
|
- Line ~5400: `loadEpisodicMemories()` function - changed memData to memDataEncoded
|
||
|
|
- Line ~5535: `showEditMemoryModal()` function - added decodeURIComponent step
|
||
|
|
|
||
|
|
## Testing
|
||
|
|
|
||
|
|
**Before Fix:**
|
||
|
|
```javascript
|
||
|
|
// Memory with quotes
|
||
|
|
Content: 'My favorite song is "Monitoring"'
|
||
|
|
Result: ❌ Syntax error when clicking Edit button
|
||
|
|
```
|
||
|
|
|
||
|
|
**After Fix:**
|
||
|
|
```javascript
|
||
|
|
// Memory with quotes
|
||
|
|
Content: 'My favorite song is "Monitoring"'
|
||
|
|
Encoded: 'My%20favorite%20song%20is%20%22Monitoring%22'
|
||
|
|
Result: ✅ Modal opens correctly with content displayed
|
||
|
|
```
|
||
|
|
|
||
|
|
## Status
|
||
|
|
|
||
|
|
✅ **FIXED** - Encoding/decoding properly handles all special characters
|
||
|
|
|
||
|
|
## Restart Required
|
||
|
|
|
||
|
|
After making these changes, restart the container:
|
||
|
|
```bash
|
||
|
|
docker restart miku-bot
|
||
|
|
```
|