How to replace the name of the string, for example, there is a span with the class new-message and in this span is the string "hello". How to do a search for the class and replace the contents of the call with another word, I load the page and if there is a new-message class and the word "hello" in it replace it with another word, of course, so that the page would be output with another word, like DOM search and substitution do, in general, I do not know how to do it.
|
2 answers
if you use jquery
$(".new-message").text('другое слово') - No, I use the tempermonkey extension for chrome, it allows you to load custom javascript, and the task is to check the contents of the class, because there are a lot of such calses, and if one of them has the word hello, replace it with another. - Anton Essential
|
Break into subtasks, and solve in turn.
Find all span'y with class "new-message" getElementsByClassName()
var all = document.getElementsByClassName('new-message'); var spans = Array.prototype.filter.call( all, function(el){ return el.nodeName === 'SPAN'; }); Replace the string substring .replace() :
("Привет, как дела?").replace(/привет/ig, 'Здравствуйте'); // Здравствуйте, как дела? Reading and writing the text content of an element is its .innerText property:
<div id="test">Пустота</div> <script> document.getElementById('test').innerText = "Чапаев"; </script> Will you be able to combine everything into a working solution by yourself?
Working example with a delay of 2 seconds .:
function izmena() { var all = document.getElementsByClassName('new-message') ,spans = Array.prototype.filter.call( all, function(el){ return el.nodeName === 'SPAN'; } ) ,i ; for( i=0; i<spans.length;i++) { spans[i].innerText = spans[i] .innerText .replace(/привет/ig, 'Здравствуйте') ; } } setTimeout( izmena, 2000); body{font-family:Helvetica,sans-serif} .new-message{display:block;width:200px;border-radius:10px;border:1px solid #EEE;padding:8px;margin:0 0 10px;background-color:#FAE6C9} <span class="new-message">Привет, как дела?</span> <span class="new-message">Привет!</span> <span class="new-message">Привет..</span> <span class="new-message">Привет</span> <span class="new-message">Пока не родила</span> <span class="new-message">Что делаешь?</span> <span class="new-message">Всем привет!</span> - It seems to be what you need, there are many spans with this class and in one of them there can be a word that needs to be changed, i.e. what the user would have loaded the village already with the substitution, in general, a crutch. Combine with difficulty, I will try, if you will not be difficult I will be grateful. - Anton Essential
- Keep in mind that now many pages are formed dynamically - the same VKontakte or Facebook is loaded into the message page only when it opens. section. Therefore, it is necessary to run your script at the right time - not immediately after the page loads, but after filling it with content. If you want to fake something imperceptibly for the user, failure is waiting for you) - Sergiks
- well, this is not really a fake, no, it's not about social networks) this is still a crutch for the log, there is a log that is stored and when it is requested it loads the canvas, and at this stage some logs need to be changed - Anton Essential
|