3

I am creating a chrome extension for facebook. I need to execute the same content script on every page. It works well during the first load but it doesn't work when I go to a facebook page thanks to a link (as the profile name). I read this was because not the entire page was loaded except the first time. So the script is not executed again. However I have no idea how to solve this problem.

Here is my manifest.json :

{
    "name" : "name",
    "version" : "1.0",
    "manifest_version":1,
    "description" : " description ",
    "content_scripts" : [
            {
              "matches" : ["http://www.facebook.com/*"],
              "js" : ["test.js"]
            }   
    ] ,
    "all_frames":"true"  
}

I hope someone will have the answer because I really need the script (test.js, which doesn't change the html or the css) to be executed on every page every time!

Thanks for your future answers.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Facebook heavily relies on AJAX, iirc. The page itself is not reloaded, but the contents are replaced. Because the problem is Facebook-specific, have a look at http://facebook.stackoverlow.com/ – Rob W Jul 26 '12 at 15:16
  • possible duplicate of [Chrome Extension which is supposed to run on all Facebook pages only runs when I hit refresh](http://stackoverflow.com/questions/9982429/chrome-extension-which-is-supposed-to-run-on-all-facebook-pages-only-runs-when-i) – Rob W Jul 26 '12 at 15:18
  • You could try to implement a handler for the hashchange event in your script, and have it run itself again when this event occurs … – CBroe Jul 26 '12 at 16:11
  • This is not related to your problem, but your matches should rather be: `"matches" : ["*://www.facebook.com/*"]` (Some people always load Facebook in https) – Maxime Kjaer Jul 27 '12 at 11:51

1 Answers1

4

I personally solved this by using Chrome's webRequest API. You'll want to add a listener that tracks AJAX-generated HTTP calls. Here's essentially the code I used:

chrome.webRequest.onCompleted.addListener(function(details) {
    var url = document.createElement('a');
    url.href = details.url;
    if (url.search && url.search.indexOf('ajaxpipe=1') !== -1) {
        console.log('New page via AJAX.');
        chrome.tabs.executeScript({'file' : 'test.js'});
    }
}, {urls : ["*://*.facebook.com/*"]});
Max Scheiber
  • 173
  • 1
  • 9