4

I'm working on a small chrome extension for fun, and one thing I need it to be able to do, is to detect when the text inside a div is changed by the webpage itself.The code I'm using is:

var status = document.getElementById("status").innerHTML;
status.onchange = function() {
    console.log("CHANGE DETECTED")

And this doesn't seem to work, so what should I use instead?

NOTE: I'd prefer not to use jquery, as I am not even very proficient with javascript at the moment, but if it would be that much simpler/easier, that would be fine.

2 Answers 2

9

use this trick

source:https://hacks.mozilla.org/2012/05/dom-mutationobserver-reacting-to-dom-changes-without-killing-browser-performance/

// select the target node
    var target = document.querySelector('#some-id');

    // create an observer instance
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            console.log(mutation.type);
        });
    });

    // configuration of the observer:
    var config = { attributes: true, childList: true, characterData: true }

    // pass in the target node, as well as the observer options
    observer.observe(target, config);

    // later, you can stop observing
    observer.disconnect();
Sign up to request clarification or add additional context in comments.

Comments

3

You can't do what you want using change event. On newer browsers, you can use Mutation Observers. On older browsers... well, you ask people to upgrade to newer browsers. :P

3 Comments

Thanks @Touffy, that was a pretty funny derp.
Yeah, considering the whole idea of observers is to not behave like the mutation events they replaced.
@Touffy: Yup. I even linked the correct page, then failed on the link text. :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.