javascript - How to load a greasemonkey script after AJAX-Request - Stack Overflow

I have a simple greasemonkey script that makes some simple dom manipulation. The greasemonkey script is

I have a simple greasemonkey script that makes some simple dom manipulation. The greasemonkey script is loaded after the DOM is loaded, that's fine so fare and it does work for the initial Page. But on this site (it's twitter ;-) ) parts of the page get loaded after a click by xmlhttprequest and this part did not get manipulated by my greasemonkeyscript.

Is there a simple possibility to run the script again after the xmlhttprequest is loaded or after the DOM is changed by the request?

I have a simple greasemonkey script that makes some simple dom manipulation. The greasemonkey script is loaded after the DOM is loaded, that's fine so fare and it does work for the initial Page. But on this site (it's twitter ;-) ) parts of the page get loaded after a click by xmlhttprequest and this part did not get manipulated by my greasemonkeyscript.

Is there a simple possibility to run the script again after the xmlhttprequest is loaded or after the DOM is changed by the request?

Share edited May 20, 2013 at 2:02 Lee Taylor 7,98416 gold badges37 silver badges53 bronze badges asked Jul 6, 2009 at 10:46 leoleo 3,7497 gold badges36 silver badges46 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

A powerful technique when using Greasemonkey is to 'hijack' existing JavaScript functions. If the page you are altering has a function called processAjaxResponse which is called when to process the XmlHttpRequest response, the you can do the following in your Greasemonkey script:

var originalProcessAjaxResponse;

function myNewProcessAjaxResponse() {
  /* Manipulate DOM if you need to, and then... */
  originalProcessAjaxResponse();
}

function hijack() {
  var originalProcessAjaxResponse = processAjaxResponse;
  processAjaxResponse = myNewProcessAjaxResponse();
}

This allows you to inject your own functionality when the AJAX response event occurs.

Thanks Howard. It was the way in the right direction.

It's quite hard to find the right entry point to hijack a function on a minifyied script. But I found that there is a huck in the twitter script. It does call window.onPageChange after the Ajax request. (I wonder if this is a mon best practice in javascript and if other do this as well?) I did found some code on http://userscripts/scripts/review/47998 wich does use this posibility to attach events.

        if (typeof unsafeWindow.onPageChange === 'function') {
        var _onPageChange = unsafeWindow.onPageChange;
        unsafeWindow.onPageChange = function(){
            _onPageChange();
            filter();
        };
    } else {
        unsafeWindow.onPageChange = filter;
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744856847a4597457.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信