How to execute a script in chrome extension? when the content is changed?

I want to execute a chrome script,when the content of webpage is update (E.g like Facebook)

So what method I should use for that?

Answers


You can use

$("body").bind("DOMSubtreeModified", function() {
    alert("something changed");
});

Note: DOMSubtreeModified is deprecated


You should try it with chrome.* API:

chrome.tabs.onUpdated.addListener(
    function(integer tabId, object changeInfo, Tab tab) {
     //your code
});

See here for documentation:

http://code.google.com/chrome/extensions/tabs.html#event-onUpdated

It should work for updates like (AJAX) requests etc...


As sachleen said, you can use DOMSubtreeModified listener

document.addEventListener("DOMSubtreeModified", methodToRun);

Note that the Mutation Events are performance hogs which can't really be tamed well (they fire too often and slow down the page a lot). Therefore, they have been deprecated over a year ago and should be used only when really needed. However, they work.

If you want this for a Chrome extension, you could use the new and shiny Mutation Observers from DOM Level 4 (follow the links there, they explain a lot!). Where DOMSubtreeModified fired a thousand times, MutationObserver fires only once with all the modifications contained and accessible.

Works for (as of 2012/06):

  • Chrome 18+ (prefixed, window.WebKitMutationObserver)
  • Firefox 14+ (unprefixed)
  • WebKit nightlies

Need Your Help

Broadcasting a message to multiple workers spring rabbitmq

spring rabbitmq spring-batch

I am using RabbitMQ with Spring. I have multiple workers running on separate vm's that pick up messages in a round robin fashion. All is good.

select element in parent of parent

jquery jquery-mobile

I have a jQuery Mobile accordion menu set up like: