The elements on a website are objects, i. e. they have a unique identity
to them. Replacing the whole document tree with an (code-wise) identical
one still means it's a new tree, which may result in broken code.
Especially a lot of modern web pages use a technique called "lazy
loading", where the initially loaded page is quite empty except for some
generic content and some scripts that control which other content is
loaded from the server. The results are then placed onto the web page.
Unfortunately, your code threw away the "target elements" for those
results, and created new empty ones, which are however to the target for
any loading action, so they remain empty.
Since it's a bit difficult for non-programmers to wrap their head around
object oriented programming, I'll use an example that's not quite
correct but I hope you will get what's going on:
Imagine your webbrowser like a file converter that is the default
program for some weird file format. You double click a file in that
format, and the converter opens and starts to convert it. Then you
delete the input- and the output file, and thereby cause the converter
to crash because suddenly the file it used to read from isn't there any
more, and neither is the file it used to write to. Then you proceed to
create a new input file with the same name and content like the old one.
However, the conversion doesn't restart or continue, because although
everything is back to what it used to be like, the converter has stopped
working because all the things it was working on were replaced.
The example is not 100% right for actual object oriented programming,
but it gives you a feeling of what happens when you replace the source
code of some element or the whole page.
Am 21.02.2019 um 01:49 schrieb Cerberus:
I don't know, but I suspect it might help if you operated on the DOM
instead of on the HTML directly. So if you selected the .innerText
property of the text nodes that you wanted to change words in, rather
than the HTML of the whole document.
On Sunday, 11 November 2018 22:17:57 UTC+1, Anton wrote:
I need to replace all matches of one word by another.
Here is how I did it:
|
document.body.innerHTML
=document.body.innerHTML.replace('/hello/gi','world');
|
When I enable my greaseMonkey scipt this page doesn't load
correctly (it appears empty):
https://translate.yandex.ru/?text=hello&lang=en-ru
<https://translate.yandex.ru/?text=hello&lang=en-ru>
When I disable my script the page loads correctly.
What's wrong with my code?
--
You received this message because you are subscribed to the Google
Groups "greasemonkey-users" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected]
<mailto:[email protected]>.
To post to this group, send email to
[email protected]
<mailto:[email protected]>.
Visit this group at https://groups.google.com/group/greasemonkey-users.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"greasemonkey-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/greasemonkey-users.
For more options, visit https://groups.google.com/d/optout.