![](/img/trans.png)
[英]How can I use a callback function as a parameter, in which has its own independent parameters?
[英]How can I reference a download item from a callback function that has its id as parameter?
場景:我執行chrome.downloads API函數之一,並且在其回調函數中,我需要獲取有關downloadItem
引用的downloadId
信息(該項目的id
除外)。 除了使用chrome.downloads.search()
或構建自己的數組以“記住” chrome.downloads.onCreated.addListener()
的項目外,我該怎么做-因為除后者外,幾乎所有回調函數都在那里只接受id
而不接受項目本身作為參數?
我嘗試過(並需要類似chrome.downloads.downloadItems[id]
來引用它)。
用例場景:如果在除chrome.downloads.onCreated.addListener()
(例如chrome.downloads.open()
或其他任何函數)之外的其他函數的回調函數中需要downloadItem
的url
或filename
,例如大多數人通過其id
引用項目)。
與此相關的一個附帶問題:考慮到測試中的執行順序,如何檢查代碼中的某個功能是否觸發了下載?
chrome.downloads.onCreated
偵聽器(我可以在其中引用實際項目,請參見上文) chrome.downloads.download()
(在我的代碼中我可以知道下載請求的來源,但是我不能引用實際的項目) chrome.downloads.onChanged
偵聽器(在檢查下載是否完成時)。 換句話說,我需要知道在特定項目中從代碼中調用下載的位置,同時我可以獲取有關下載項目的其他信息(只能在chrome.downloads.onCreated()
目前,因此我的問題在這里)。
我希望我清楚地描述了我的問題-否則,請隨時在評論中提問,或檢查下面的相關代碼( 僅當下載源自queryselected()
,我才希望將下載信息添加到數組中,而丟棄其他在此期間或之后可能發生的下載):
function queryselected()
{
var selection = window.getSelection(),
elements = document.querySelectorAll("[id^='element']");
for (var i = 0; i < somearray.length; i++)
{
if (selection.containsNode(elements[i], true))
{
chrome.downloads.download
(
{url: somearray[i].url, filename: "element" + i + ".el", conflictAction: "uniquify"},
function(downloadid)
{
console.log("downloads.download: ", downloadid);
// I can know where the download was triggered here, but
// I can't access the download item (only its id), so I do in the listener below
itemstoquery.push({id: downloadid, url: undefined, filename: undefined});
}
);
}
}
}
chrome.downloads.onCreated.addListener
(
function(downloaditem)
{
if (downloaditem)
{
console.log("downloads.onCreated: ", downloaditem.url);
for (var i = 0; i < itemstoquery.length; i++)
{
// I can access the download item here, but
// I can't know if its download was triggered by queryselected() above,
// since this executes BEFORE chrome.downloads.download()'s callback function
// As a result, the if below is never true, so I can't fill the array properly.
if (downloaditem.id === itemstoquery[i].id)
{
itemstoquery[i].url = downloaditem.url;
itemstoquery[i].filename = downloaditem.filename;
console.log("Started downloading: ", itemstoquery[i]);
}
}
}
}
);
chrome.downloads.onChanged.addListener
(
function(downloaddelta)
{
if ((downloaddelta.state) && (downloaddelta.state.current === "complete"))
{
console.log("downloads.onChanged: ", downloaddelta.id);
var rf = chrome.downloads.removeFile(downloaddelta.id);
var eh = chrome.downloads.erase({id: downloaddelta.id});
}
}
);
注意:我說“不是chrome.downloads.search()
”是因為對我來說“搜索”下載項有點多余,因為您已經有了它的ID,並且基本上可以使用它了。 我想在下載項目時參考下載項目, 而不是之后(在搜索可能具有其作用的地方)。
我說“不是chrome.downloads.search()”,因為在我看來“搜索”一個下載項有點多余,
他們明確表示您應該這樣做。 這是從id獲取DownloadItem的唯一方法。
https://developer.chrome.com/extensions/downloads#method-search
要獲取特定的DownloadItem,請僅設置id字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.