I am trying to create a chrome extension. I have the following function in my content script to handle downloads of files:
function handleDownloadFile(urlVal){
console.log("handleDownloadFile called");
}
Then I have the following code to inject a function into the DOM (taken from here: https://stackoverflow.com/a/11811558/3778854):
function injectScript(source) {
var elem = document.createElement("script"); //Create a new script element
elem.type = "text/javascript"; //It's javascript
elem.innerHTML = source; //Assign the source
document.documentElement.appendChild(elem); //Inject it into the DOM
}
injectScript("("+(function(window) {
// functions here
console.log("successfully injected content script"); // works
function foo(url){
$("#downloadPdfButton").click(handleDownloadFile(url));
}
foo("http://url.com/doc.pdf"); // breaks here
}).toString()+")(window)");
upon calling foo("http://url.com/doc.pdf")
, it will break saying that handleDownloadFile is undefined. As you can see, I even tried passing window
, and then setting window.handleDownloadFile(url)
as the click event handler, with no success.
I hope someone can give me some pointers, if this is even possible.