Trouble with Mutation Observer



Hello. This is my first post. I am trying to observe changes made to my 'ItemList' using Mutation Observer. I'm injecting my code in Custom Code section of VEC. The observers are getting inititalized and my code works during edit Activity. But in the Activity preview it doesn't work and I am confused about this behavior. If you need any logs to further examine this, feel free to ask.

Here is my custom code:

    var id;
    window.MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
    function events(){
      var content, bookBtns;    
      if((content = document.querySelector('.booking-wrapper')) !== undefined){ = "0";
        //console.debug("set opacity to 0");
      if(document.querySelector('.book-room-btn') !== undefined){
        bookBtns = document.getElementsByClassName('book-room-btn');
        //console.debug("changing text");
        var i;
          var bookBtn = bookBtns[i];
          bookBtn.textContent = "Book";
        if(i == bookBtns.length){
   = "1";
            //console.debug("set opacity to 1");
            //console.debug("cleaned up");
    //id1 = window.setInterval(events, 50);
    window.addEventListener('hashchange', function(event){
      console.debug("hash changed!!!");
      if(window.location.hash.substr(1).split('&')[0] == "/step2"){
          document.querySelector('.booking-wrapper').style.opacity = "0";
              id = window.setInterval(events, 100);
        }, 1500);
    document.addEventListener('DOMContentLoaded', function(event){
      var target = document.querySelector('#room-choices'),
      observer = new MutationObserver(function(mutations){
        console.debug("mutation detected");
        document.querySelector('.booking-wrapper').style.opacity = "0";
      config = {childList:true, subtree:true};
          observer.observe(target, config);
      //Mutation Observer Polyfill for IE 10
        target.addEventListener('DOMNodeInserted', function(){
          console.debug("Node mutation detected");
          document.querySelector('.booking-wrapper').style.opacity = "0";
        target.addEventListener('DOMSubtreeModified', function(){
          console.debug("subtree mutation detected");
          document.querySelector('.booking-wrapper').style.opacity = "0";

Accepted Solutions (1)

Accepted Solutions (1)




Hi Barath,

I am not able to do a code review, but one thing to check is the timing of the code delivery.  When delivered to the page through the mbox, the code is probably loading at the top of the page and depending on which library you are using it might be before or after DOMContentLoaded.  And in production, it might execute slightly differently from within the VEC.  So, i would use additional console.log statements to see if things are actually firing when you think.  For example, i see you are using event listeners for DOMContentLoaded which might not be behaving the way you think in the context of a normal page load with an mbox call.  Let us know if this helps.