2014-07-24 18 views
-5

我正在嘗試改變使用擴展名擴展名下載的文件的名稱。我所做的是我解析的網頁(使用jQuery)從中給出文件的名稱。現在這是我的代碼的結構:在Chrome擴展中運行befor jquery的問題

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

正在發生的事情是,文件被下載了jQuery可以完成解析和更改文件名的代碼運行之前的jQuery之前可以完成執行這反過來又導致錯誤的文件name.I不要」瞭解什麼是問題以及如何糾正它。我認爲完整的代碼將不會被要求,所以我不會發布那個。是否有任何方法限制文件名的更改,直到jquery完成? 編輯 代碼:

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; 
    } 

的問題是,如果該功能,改變了名稱的jQuery之前運行...

+0

也許有人甚至可以告訴投票下來的原因! – user3868494

+0

我很樂意。您應該閱讀[如何提問](http://stackoverflow.com/help/how-to-ask)指導並相應地編輯您的問題。你應該確實包括相關的代碼。 – Xan

+0

@Xan請看代碼.. – user3868494

回答

0

大家好消息,你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 
} 

的關鍵在這裏:你$.get回調函數被執行之前chrome.downloads.onDeterminingFilename處理程序將退出。默認情況下,Chrome在這種情況下不會等待您的建議 - 但您可以通過返回true表示「稍後會通知您,請稍候」。這全部寫在文檔中。

請閱讀一些關於異步函數的教程。例如,this answer

0

我並不完全相信你的問題是什麼,但也許

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

可能會幫助你。

如果您正在使用下載文件的函數有callback,那麼您可以檢查jQuery文檔。那麼你可以做一些沿線

somefiledownload.ready(function(){ 
    // some code that gets executed when the download finished 
}); 
+0

我認爲他的問題可以描述如下:在收集數據之前調用你的概念'somefiledownload.ready',並且它必須**同步返回一個建議的文件名。 – Xan

+0

我發佈的代碼..pls看看它.. – user3868494