1

I am trying to build a Chrome extension that uses injected JS to insert HTML from my server into a Facebook page from my server. Then, I want to be able to call a Javascript function from within the HTML, and have it execute on the Facebook page. The problem is, JS functions that I put in the fhrun.js file don't run on the page when I call them from my HTML file. It may have something to do with the fact that none of the injected JS's functions can be executed from the JS console on Facebook.

Here is the Chrome script I run in the background.js file:

chrome.tabs.executeScript(null,
    {file: "fhrun.js", allFrames: false, runAt: "document_start"},
    function(){
        //JS inserted
    }
);

This is a simplified fhrun.js (it also contains jquery):

function testFunction(){
console.log("Test function is working.");
}

$("#facebookDiv").load("https://path.to.my.server/ajax.html"); // this works great btw

This is ajax.html:

<a href="#" onclick="testFunction();">Click Me</a> //link doesn't work

When I click "Click Me," it should call testFunction(); which is inside the JS that has been injected into the page. But it doesn't. It doesn't even throw an error. I also tried putting testFunction(); inside the background.js file, and that didn't work either.

Any suggestions?

Brian Mayer
  • 989
  • 3
  • 13
  • 23
  • `allFrames` and `runAt` should be `all_frames` and `run_at`. Your problem description is not sufficiently clear, can you create and share a minimal demo to show your problem (including `manifest.json`)? Did you know about the differences of the execution context (see [this answer](http://stackoverflow.com/questions/9915311/chrome-extension-code-vs-content-scripts-vs-injected-scripts/9916089#9916089))? – Rob W Nov 15 '12 at 21:39

1 Answers1

1

Inline Java Script will not work for JS Injection, use event bindings instead

Ex:

onload = function (){

document.getElementById("xx").click = fun;

};

Sudarshan
  • 18,140
  • 7
  • 53
  • 61