mynew word tracker
by Julian
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Word Index Tracker</title>
<style>
#source, #target {
width: 45%;
height: 200px;
margin: 10px;
border: 1px solid #ccc;
padding: 10px;
font-family: Arial, sans-serif;
font-size: 14px;
}
#log {
margin: 10px;
padding: 10px;
border: 1px solid #ccc;
font-family: Arial, sans-serif;
font-size: 14px;
height: 150px;
overflow-y: auto;
background-color: #f9f9f9;
}
#saveButton {
margin: 10px;
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
}
#saveButton:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h2>Word Index Tracker</h2>
<div>
<textarea id="source" placeholder="Paste or type your document here..."></textarea>
<textarea id="target" placeholder="Paste text here..."></textarea>
</div>
<div>
<h3>Activity Log:</h3>
<div id="log"></div>
</div>
<div>
<button id="saveButton">Save Pasted Words to JSON</button>
</div>
</body>
</html>
JavaScript
const source = document.getElementById('source');
const target = document.getElementById('target');
const log = document.getElementById('log');
const saveButton = document.getElementById('saveButton');
let wordIndex = []; // To store indexed words and their unique IDs
let pastedWords = []; // To accumulate pasted words and their matched indexes
// Function to index every word uniquely by its position in the source text
function indexWords(text) {
wordIndex = []; // Reset index
const words = text.match(/\b\w+\b/g) || []; // Match all words, ignoring punctuation
let currentPosition = 0; // Track the position in the text
words.forEach((word) => {
const start = text.indexOf(word, currentPosition); // Get the starting position of the word
wordIndex.push({
word: word.toLowerCase(), // Normalize to lowercase
index: wordIndex.length + 1, // Assign a unique sequential index
position: start // Store the actual position in the text
});
currentPosition = start + word.length; // Update the current position
});
logMessage(`Document indexed with ${wordIndex.length} word occurrences.`);
}
// Function to log messages to the activity log
function logMessage(message) {
const entry = document.createElement('div');
entry.textContent = message;
log.appendChild(entry);
log.scrollTop = log.scrollHeight;
}
// Event listener to index the document whenever it's updated
source.addEventListener('input', () => {
indexWords(source.value);
});
// Event listener to track copy-paste activity
target.addEventListener('paste', (event) => {
const pastedText = event.clipboardData.getData('text'); // Get pasted text
const words = pastedText.match(/\b\w+\b/g) || []; // Extract words, ignoring punctuation
const trackedInfo = [];
const matchedIndexes = new Set(); // Keep track of matched indexes to avoid duplication within a single paste event
...