I apologise for asking multiple questions all at once.
- I am trying to manipulate the content of a page using a chrome extension using this code:
let container = document.querySelector('p');
let newtext = container.innerHTML.split('').map(
m => Math.random() > .49 ? <strong>${m}</strong> : m
);
container.innerHTML = newtext.join('')
The first problem I encounter is that the code only manipulates the first p
of the page. I have tried using querySelectorAll()
, but it does not work. The second is the code does not work on all the pages I load. Here is the code for my manifest.json
file:
{
"manifest_version": 3,
"version": "1.0",
"name": "name",
"description": "description",
"action": {
"default_title": "title",
"default_popup": "popup.html",
"default_icon": "icon.png"
},
"content_scripts": [
{
"js": ["content.js"],
"matches": ["<all_urls>"],
"run_at": "document_start"
}
]
}
- I am experiencing a new problem with my code (which I think might be a page-specific issue). Whenever I reload the page it displays the HTML
tags
that exist on the page, like this:
strong>Question</strong> <span>How does the efficacy of cefepime/enmetazobactam compare with piperacillin/tazobactam for the treatment of complicated urinary tract infection (UTI) or acute pyelonephritis?</span>
.
Any advice would be appreciated.
`) but the example you provided only included `
text
`, which is why it appeared to work as expected on SO.com, but broke in your real test. This is the benefit of a [minimal, reproducible example](https://stackoverflow.com/help/how-to-ask) to narrow down the scope of where things are going wrong. This has been updated in my answer to account for nested elements (specifically, only editing a container's `.innerText`, not `.innerHTML`). – WOUNDEDStevenJones Oct 14 '22 at 15:36