Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen for creation of an element and fire an event when it appears on the page in Chrome Extension

Is it possible to have a Chrome extension listen for the appearance of a yet-to-be-created element?

Say the user clicks a button and the click event creates an element <div id='myDiv'>My Div</div> and adds it to the page/DOM. Is it possible to set a listener that will automatically fire an event when that element appears?

Or do I have to resort to polling the page and checking for this element every X amount of milliseconds?

jQuery and other libraries are not an option for me btw.

like image 503
Jim_CS Avatar asked Jun 20 '12 22:06

Jim_CS


1 Answers

You can use arrive.js, it wraps the Mutation Observers api. Usage:

document.arrive(".test-elem", function() {
    // 'this' refers to the newly created element
    var newElem = this;
});
like image 172
Uzair Farooq Avatar answered Oct 18 '22 22:10

Uzair Farooq