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...