0

I want to develop a chrome extension that replace some text on facebook but once the user scroll the ajax appear, so the newly ajax loaded text doesn't get change. I tried on() to bind but failed. So I'm thinking every time if there is ajax I fire my event again.

But I can't find any method to detect an ajax happened in a page using jquery :(

I tried

$('body').on(function(){
    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        /* special ajax here */
        alert('test');
    }
});

not working

DevlshOne
  • 8,357
  • 1
  • 29
  • 37
user3522749
  • 309
  • 3
  • 10
  • jQuery has global ajax handlers, such as `$.ajaxStart`, but I doubt facebook is using jQuery for their ajax calls. – adeneo Sep 07 '14 at 15:24
  • @adeneo if it's not jquery then it's ajax, jquery can't catch an ajax? I doubt they didn't use ajax though.. – user3522749 Sep 07 '14 at 15:26
  • Huh? I'm not saying facebook isn't using ajax, just that they aren't using jQuery, and that would mean XMLHttpRequest. Of course, there's also sockets and server sent events. – adeneo Sep 07 '14 at 15:29
  • 3
    So now that you've added code, you're using `on` without an event, and what the heck is that PHP doing in a Chrome extension ? – adeneo Sep 07 '14 at 15:33
  • @adeneo even I put that outside the on(), it detect nothing, that's why I debug with an on().. – user3522749 Sep 07 '14 at 15:41
  • You can't put that on the outside or the inside, its PHP code, not javascript code, you cannot mix and match the two – Patrick Evans Sep 07 '14 at 15:43
  • And you can't really put serverside code like PHP in a browser extension at all ? – adeneo Sep 07 '14 at 15:45
  • @PatrickEvans LOL it's PHP! I just realised fuhhhhh funny – user3522749 Sep 07 '14 at 15:45

2 Answers2

1

to detect AJAX calls on a webpage you have to inject the code directly in that page and then call the .ajaxStart or .ajaxSuccess

Example:

// To Successfully Intercept AJAX calls, we had to embed the script directly in the Notifications page
var injectedCode = '(' + function() {
    $('body').ajaxSuccess(function(evt, request, settings) {
        if (evt.delegateTarget.baseURI == 'URL to check against if you want') {
            // do your stuff
        }
    });
} + ')();';
// Inserting the script into the page
var script = document.createElement('script');
script.textContent = injectedCode;
(document.head || document.documentElement).appendChild(script);
script.parentNode.removeChild(script);
AhmadAssaf
  • 3,556
  • 5
  • 31
  • 42
0

To answer your question, you can detect any AJAX calls with the following jQuery event handler which only works on the document element.

$(document).ajaxStart(function() {
    alert('I'm making an AJAX call!!!');
});

You could also watch the 'Network' tab in developer tools.

DevlshOne
  • 8,357
  • 1
  • 29
  • 37
  • I tried use this before, and when I scrolled the feed, doesn't appear in facebook.. – user3522749 Sep 07 '14 at 15:38
  • Only would work if jQuery was the one actually making the ajax requests, facebook doesn't use jquery – Patrick Evans Sep 07 '14 at 15:38
  • Just answering the question, guys. One would have to assume that if you're going to use jQuery to detect the AJAX call, that jQuery would be the one performing the AJAX call.. ya know? Otherwise, this question and it's title make no sense whatsoever. – DevlshOne Sep 07 '14 at 15:40
  • @PatrickEvans so how could I catch the ajax? I checked the network tab, it fall under XHR, does that mean pure ajax? – user3522749 Sep 07 '14 at 15:40
  • @user3522749 Yes, XHR is an AJAX call. – DevlshOne Sep 07 '14 at 15:41
  • Well, the question states that the OP is trying to catch ajax calls made by Facebook in an extension, so it's actually pretty much given that jQuery **is not** the one making the ajax calls, hence why I posted it as a comment under the question. – adeneo Sep 07 '14 at 15:44
  • @user3522749,you would have to deminify their scripts and find which one does the xhr requests and find a way to hook into their api – Patrick Evans Sep 07 '14 at 15:44
  • hook into? how? I thought it would work with a normal js detection? I also tried this http://stackoverflow.com/questions/10783463/javascript-detect-ajax-requests but nothing happend – user3522749 Sep 07 '14 at 15:50