2016-01-08 32 views
0

我正在嘗試製作一個Greasemonkey腳本。他必須觀看偶爾出現優惠券的頁面並帶走它們。監控列表中的第一個div

優惠券提供了一個列表 我想跟蹤第一個元素,併爲此使用MutationObserver。 問題是,該網站使用ajax和第一個優惠券成爲第二,等等。 MutationObserver繼續監視它,並沒有注意到新的優惠券。請幫助,我怎麼只能在列表元素div中查看頂部(第一個)?

// select the target node 
var target = document.querySelector('.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); 

更新:

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

// create an observer instance 
var observer = new MutationObserver(function(mutations) { 
    mutations.forEach(function(mutation) { 
    console.log(mutation.addedNodes[0].textContent); 
    document.getElementsByClassName('nameshop')[0].click(); 
    });  
}); 

它提供與DIV CLASS = 「ID」一個新的元素,然後單擊,我想。但是如果我通過Firebug進行更改,會發生這種情況。如果該網站更新,腳本沒有注意到。

回答

0

您也可以使用這種突變結果:它提供了一個addedNodes-Property。所以如果添加一個Node,只需使用類型NodeList中的mutations.addedNodes。爲此,請參閱:https://developer.mozilla.org/de/docs/Web/API/MutationObserver

var observer = new MutationObserver(function(mutations) { 
    mutations.forEach(function(mutation) { 
    console.log(mutation.addedNodes[0]); 
    });  
}); 
相關問題