working? new word tracker with json output duplicated words and no punctuation
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 Results 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 = [];
let pastedWords = [];
// Function to index every word uniquely in the source text
function indexWords(text) {
wordIndex = []; // Reset index
const words = text.match(/\b\w+\b/g) || []; // Match all words, ignoring punctuation
words.forEach((word, i) => {
const cleanWord = word.toLowerCase().replace(/'/g, ''); // Remove apostrophes and convert to lowercase
wordIndex.push({ word: cleanWord, index: i + 1 }); // Assign a unique index to each word occurrence
});
logMessage('Document indexed with ' + words.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 = [];
words.forEach(word => {
const cleanWord = word.toLowerCase().replace(/'/g, ''); // Remove apostrophes
const occurrences = wordIndex.filter(entry => entry.word === cleanWord);
occurrences.forEach(entry => {
trackedInfo.push({ word: word, index: entry.index });
});
});
pastedWords = trackedInfo; // Save results for export
if (trackedInfo.length) {
const logText = trackedInfo
.map(info => `${info.word} (Index: ${info.index})`)
...