Search code examples
jqueryajaximagegoogle-chrome-extensionblock

Do not load images in jQuery ajax request?


Well I'm making a chrome extension that parses some webpage, and that's working right, but I would like to be able to block images, meaning, not to load them, since all the parsing is made in background and it would be faster without the images... is there a way to do so?

$("#data").load(url, function(responseText, textStatus, XMLHttpRequest){ 
    $('img').attr('src','');
    if(XMLHttpRequest.status==200)
        parseLinks();
    else if(XMLHttpRequest.status==301)
        Text="Please login.";
    else
        Text = "Error.";
    console.log(textStatus);
});

function parseLinks(){
  Text = [];
  $("#data .plain").each(function(i){
    var tempArray;// = [];
    var parent = "#"+$(this).parent().attr("id");
    tempArray['text']   = $(parent+' a').text();
    tempArray['link']   = $(parent+' a').attr('href');
    tempArray['ad']     = $(parent+' font[color="#808080"]').text();
    tempArray['value']  = $(parent+' td[width="100"] font').text();
    Text[i] = tempArray;
      });
  if(Text.lenght)
    console.log("ads loaded!");
  else
    Text = "No new ads.";
  console.log(Text);
 }

Also, the more time you keep running the extension the more ram an vram the extension will be using, since it accumulates the images in each of the ajax request...


Solution

  • Solved it, I had to change from .load() to $.get() to be able to remove images via .replace() before the html requested is inserted in the page, here's the code:

    $.get(url, function(responseText, textStatus, XMLHttpRequest){ 
        console.log(XMLHttpRequest.status);
        if(XMLHttpRequest.status==200) {
            var loggedin = responseText.search('<div class=header>Account Login</div>');
            if(loggedin == -1){
                responseText = responseText.replace(/<img/gi, '<noload');
                parseLinks(responseText);
            } else {
                Text="Please login.";
                chrome.browserAction.setBadgeText({text:'L'});
                chrome.browserAction.setBadgeBackgroundColor({color:[180,180,180,255]});
            }
        } else {
            Text = "Error.";
            chrome.browserAction.setBadgeText({text:'\u00D7'});
            chrome.browserAction.setBadgeBackgroundColor({color:[255,0,0,255]});
        }
    });