google extension to extract words from every websites everytime I click in it

hi, I want to make a google extension to extract words from every websites everytime I click in a certain word inside the page. And I want it to popup it as a list of words. But I don't know what i wrong with my code so I don't achieve what I want. popup.js
document.addEventListener('DOMContentLoaded', () => {
const wordList = document.getElementById('word-list');

chrome.runtime.sendMessage({ action: 'getWords' }, (response) => {
const { words } = response;
words.forEach((word) => {
const listItem = document.createElement('li');
listItem.textContent = word;
wordList.appendChild(listItem);
});
});
});
document.addEventListener('DOMContentLoaded', () => {
const wordList = document.getElementById('word-list');

chrome.runtime.sendMessage({ action: 'getWords' }, (response) => {
const { words } = response;
words.forEach((word) => {
const listItem = document.createElement('li');
listItem.textContent = word;
wordList.appendChild(listItem);
});
});
});
content.js
function extractWords() {
const textContent = document.body.innerText;
const words = textContent.split(/\s+/).filter((word) => /^[a-zA-Z]+$/.test(word));
return words;
}

const words = extractWords();
chrome.runtime.sendMessage({ action: 'updateWords', words });
function extractWords() {
const textContent = document.body.innerText;
const words = textContent.split(/\s+/).filter((word) => /^[a-zA-Z]+$/.test(word));
return words;
}

const words = extractWords();
chrome.runtime.sendMessage({ action: 'updateWords', words });
background.js file
let extractedWords = [];

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'updateWords') {
extractedWords = extractedWords.concat(message.words);
chrome.storage.local.set({ words: extractedWords });
}

if (message.action === 'getWords') {
sendResponse({ words: extractedWords });
}
});
let extractedWords = [];

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'updateWords') {
extractedWords = extractedWords.concat(message.words);
chrome.storage.local.set({ words: extractedWords });
}

if (message.action === 'getWords') {
sendResponse({ words: extractedWords });
}
});
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server