0

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.

Community
  • 1
  • 1
Camillo
  • 544
  • 1
  • 6
  • 24
  • Is `jQuery` defined within extension ? – guest271314 Mar 05 '15 at 15:57
  • Hi there and thanks for your comment. Yes, it is defined and it works. If I move `handleDownloadFile()` to be within the anonymous function (after `foo()` for example), it will work. But because I plan to use chrome APIs (for example `chrome.extension.getBackgroundPage()`) it must be outside. Hope it's clear, if not I'd be happy to provide more details! – Camillo Mar 05 '15 at 16:03
  • 2
    `handleDownloadFile(url)` will be called immediately when `foo` is called. The return value of `handleDownloadFile` will be assigned to the click handler. if you want `handleDownloadFile` to be called on click you will have to wrap it in a function `$("#downloadPdfButton").click(function(){handleDownloadFile(url)});` –  Mar 05 '15 at 16:03
  • Thanks a lot Austin, I'll try that immediately and let you know. – Camillo Mar 05 '15 at 16:04
  • I just tried this, and also `window.handleDownloadFile(url)`. Both did not work, unfortunately. Thanks a lot for your help and your time. – Camillo Mar 05 '15 at 16:14
  • Tried substituting `chrome.tabs.executeScript(null, {file: "doStuff.js"})` https://developer.chrome.com/extensions/tabs#method-executeScript for `injectScript` ? – guest271314 Mar 05 '15 at 16:35
  • As this whole code is in a content script, this won't work. "chrome.tabs" is only available in background scripts. Also, I tried setting that from background script, but with no luck. – Camillo Mar 05 '15 at 16:57
  • Uh, that function in the content script *is not global*? If it was, your code would work. – Bergi Mar 05 '15 at 17:16

1 Answers1

-1

While declaring the handleDownloadFile can you try this -

window.handleDownloadFile = function(urlVal){
    console.log("handleDownloadFile called");
}
sushant
  • 49
  • 5