111 lines
3.9 KiB
JavaScript
111 lines
3.9 KiB
JavaScript
document.addEventListener('DOMContentLoaded', function() {
|
|
// References to DOM elements
|
|
const wordPairsContainer = document.getElementById('wordPairsContainer');
|
|
const addPairButton = document.getElementById('addPair');
|
|
const saveChangesButton = document.getElementById('saveChanges');
|
|
const messageElement = document.getElementById('message');
|
|
|
|
// Load existing replacements
|
|
loadReplacements();
|
|
|
|
// Event listeners
|
|
addPairButton.addEventListener('click', addNewPair);
|
|
saveChangesButton.addEventListener('click', saveChanges);
|
|
|
|
// Function to load existing replacements
|
|
function loadReplacements() {
|
|
chrome.storage.local.get('wordReplacements', function(data) {
|
|
const replacements = data.wordReplacements || {};
|
|
|
|
// Clear any existing pairs shown
|
|
wordPairsContainer.innerHTML = '';
|
|
|
|
// Add existing pairs to the interface
|
|
for (const oldWord in replacements) {
|
|
addPairToUI(oldWord, replacements[oldWord]);
|
|
}
|
|
|
|
// If there are no replacements, add at least one empty pair
|
|
if (Object.keys(replacements).length === 0) {
|
|
addNewPair();
|
|
}
|
|
});
|
|
}
|
|
|
|
// Function to add a new pair to the interface
|
|
function addNewPair() {
|
|
addPairToUI('', '');
|
|
}
|
|
|
|
// Function to add a specific pair to the interface
|
|
function addPairToUI(oldWord, newWord) {
|
|
const pairDiv = document.createElement('div');
|
|
pairDiv.className = 'word-pair';
|
|
|
|
const oldWordInput = document.createElement('input');
|
|
oldWordInput.type = 'text';
|
|
oldWordInput.placeholder = 'Word to replace';
|
|
oldWordInput.value = oldWord;
|
|
oldWordInput.className = 'old-word';
|
|
|
|
const newWordInput = document.createElement('input');
|
|
newWordInput.type = 'text';
|
|
newWordInput.placeholder = 'New word';
|
|
newWordInput.value = newWord;
|
|
newWordInput.className = 'new-word';
|
|
|
|
const removeButton = document.createElement('button');
|
|
removeButton.textContent = 'X';
|
|
removeButton.title = 'Remove';
|
|
removeButton.addEventListener('click', function() {
|
|
pairDiv.remove();
|
|
});
|
|
|
|
pairDiv.appendChild(oldWordInput);
|
|
pairDiv.appendChild(newWordInput);
|
|
pairDiv.appendChild(removeButton);
|
|
|
|
wordPairsContainer.appendChild(pairDiv);
|
|
}
|
|
|
|
// Function to save changes
|
|
function saveChanges() {
|
|
const pairDivs = wordPairsContainer.querySelectorAll('.word-pair');
|
|
const replacements = {};
|
|
|
|
// Collect all valid pairs
|
|
pairDivs.forEach(function(pairDiv) {
|
|
const oldWord = pairDiv.querySelector('.old-word').value.trim();
|
|
const newWord = pairDiv.querySelector('.new-word').value.trim();
|
|
|
|
if (oldWord && newWord) {
|
|
replacements[oldWord] = newWord;
|
|
}
|
|
});
|
|
|
|
// Save to chrome.storage
|
|
chrome.storage.local.set({ wordReplacements: replacements }, function() {
|
|
showMessage('Changes saved successfully!', 'success');
|
|
|
|
// Notify open pages of the change
|
|
chrome.tabs.query({}, function(tabs) {
|
|
for (let tab of tabs) {
|
|
chrome.tabs.sendMessage(tab.id, {
|
|
action: 'replacementsUpdated'
|
|
}).catch(err => console.log('Error sending message to tab:', err));
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
// Function to show messages
|
|
function showMessage(text, type) {
|
|
messageElement.textContent = text;
|
|
messageElement.className = 'message ' + type;
|
|
messageElement.style.display = 'block';
|
|
|
|
setTimeout(function() {
|
|
messageElement.style.display = 'none';
|
|
}, 3000);
|
|
}
|
|
}); |