-5

I am trying to change the name of the file downloaded using chrome extension.What I do is I parse the webpage(using jQuery) from which I give the name of the file.Now this is the structure of my code:

download event listener{
javascript code
jquery code to parse webpage
change file name 
}

What is happening is that the file gets downloaded before the jquery can finish parsing and change filename code runs before jquery can finish execution which in turn lead to wrong file name.I don't understand what is the problem and how to correct it.I thought full code will not be required so I am not posting that.Is there any way that I restrict the filename change until jquery finishes? EDIT code:

chrome.downloads.onDeterminingFilename.addListener( 
        function (downloadItem, suggest) 
        {   window.alert(downloadItem.url);
            window.alert("inside downloads");
            if (/^https?:\/\/ieeexplore\.ieee\.org.*/.test(downloadItem.referrer)) 
            {   
                x=downloadItem.url;
                window.alert("match done")
                folder="newww";
                window.alert(String(downloadItem.referrer));
                z=downloadItem.referrer;
                var res = z.split("arnumber=");
                var res1 = res[1].split("&");
                alert(res1[0]);
                console.log(res1[0]);
                u="http://ieeexplore.ieee.org/xpl/articleDetails.jsp?tp=&arnumber="+res1[0];
                console.log(u);
                $(document).ready(function(){
                $.get(u, function(data) 
                {//parse webpage here
                 //set the value of name here
                });
                 });
            if(isPDF(downloadItem))
            {   alert("lame");
                suggest({filename: folder + "/" + name});
            }
            else suggest();
            }


        });

    function isPDF(item)
    {
      if(item.mime === "application/pdf") return true;
      else if (item.filename.match(/\.pdf$/i)) return true;
      else return false;
    }

The problem is that the if function which changes the name runs before the jquery...

  • maybe someone could even tell the reason of voting down!! – user3868494 Jul 24 '14 at 13:05
  • I gladly will. You should read [How to Ask](http://stackoverflow.com/help/how-to-ask) guide and edit your question accordingly. And you should indeed include the relevant code. – Xan Jul 24 '14 at 13:12
  • @Xan please look at the code.. – user3868494 Jul 24 '14 at 13:19
  • 2
    My downvote stays, it's still a horribly stated question. – Xan Jul 24 '14 at 13:35
  • Next time I will try to post a much better question :) – user3868494 Jul 24 '14 at 13:37
  • I would write a comment, but unfortunately I don't have enough reputation to do so. Anyway. First, I think you're getting downvoted because your question is very unclear. Your description is very vague, you don't explain in enough detail what you are _actually_ doing and how. Probably posting the code would help. To your actual question. Either you can try to pass a callback function to your download function as suggested in the other answer and/or have a look at $(document).ready. Or even simpler: why don't you simply change the order? Parse webpage, download file, rename. Or do you mean by t – dingalapadum Jul 24 '14 at 13:16

2 Answers2

0

I'm not entirely sure what your question is, but maybe

$(document).ready(function(){
    // some code that gets executed when the page finished loading
});

might help you.

You could check the jQuery docs if there's a callback for the function you're using to download the file. Then you could do something along the lines of

somefiledownload.ready(function(){
    // some code that gets executed when the download finished
});
  • I think his problem can be described as follows: your conceptual `somefiledownload.ready` is called before data is collected, and it **must** return a suggested filename synchronously. – Xan Jul 24 '14 at 13:13
  • I am posting the code ..pls take a look at it.. – user3868494 Jul 24 '14 at 13:14
0

Good news everyone, you can call suggest asynchronously!

if (/^https?:\/\/ieeexplore\.ieee\.org.*/.test(downloadItem.referrer)) 
{   
  /* ... */
  $.get(u, function(data) 
  {
    //parse webpage here
    //set the value of name here
    suggest({filename: result}); // Called asynchronously
  });
  return true; // Indicate that we will call suggest() later
}

The key point here: chrome.downloads.onDeterminingFilename handler will exit before your $.get callback gets executed. Chrome will not, by default, wait for your suggestion in this case - but you can indicate "I will tell you later, please wait" by returning true. This is all written in the docs.

Please do read some tutorials on async functions. For example, this answer.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206