问题描述
如何使用更改下载的文件名如果文件有JPG或PNG的扩展名?
How can I use chrome.downloads.onDeterminingFilename to change downloaded file names if the files have extension of JPG or PNG?
我在这里查看示例:
chrome.downloads.onDeterminingFilename.addListener(function(item, __suggest) {
function suggest(filename, conflictAction) {
__suggest({filename: filename,
conflictAction: conflictAction,
conflict_action: conflictAction});
}
var rules = localStorage.rules;
try {
rules = JSON.parse(rules);
} catch (e) {
localStorage.rules = JSON.stringify([]);
}
for (var index = 0; index < rules.length; ++index) {
var rule = rules[index];
if (rule.enabled && matches(rule, item)) {
if (rule.action == 'overwrite') {
suggest(item.filename, 'overwrite');
} else if (rule.action == 'prompt') {
suggest(item.filename, 'prompt');
} else if (rule.action == 'js') {
eval(rule.action_js);
}
break;
}
}
});
这令人困惑。如何从上面检测文件的名称?一旦检测到,它是如何更改文件的?有人可以分解这些代码的含义吗?
This is confusing. How does chrome.downloads.onDeterminingFilename from above detect the name of the file? And once it detects, how did it change the file? Can anyone break down what these codes mean above?
参考:
推荐答案
-
一旦确定了文件名
onDeterminingFilename
事件
被触发,调用回调函数并且需要2
参数As soon as the filename is determined
onDeterminingFilename
eventis triggered upon which a callback function is called and it takes 2parameters-
item
包含下载ID,url,文件名,引用等数据的对象。 (请参阅
) - __建议必须同步或异步传递建议。
item
object which contains data such as download id, url, filename, referrer etc. (seehttps://developer.chrome.com/extensions/downloads#type-DownloadItem)- __suggest which must be called to pass suggestion either synchronously or asynchronously.
-
- 定义了一个建议函数,该函数将用于根据特定规则调用
__ suggest
- 全部该规则从本地内存访问,
for
循环运行
迭代它们。 - 对于每次迭代,基于
规则中的
使用特定的操作
数据文件名调用
和
suggest函数
conflictAction
。
- A suggest function is defined which will be used to call
__suggest
based on the specific rule - All the rules are accessed from the local memory and a
for
loop runsto iterate across them. - For each iteration based on the
action
data in therule
thesuggest function is called with specificfilename
andconflictAction
.
基本上,文件名是使用 item.filename
进行的,并且通过调用 __ suggest
建议新的文件名,其中键的值 filename
包含新文件名。
Basically the filename is aceessed using item.filename
and a new filename is suggested by calling __suggest
where the value for the key filename
contains the new filename.
这篇关于如果文件的扩展名为jpg或png,我如何使用chrome.downloads.onDeterminingFilename更改下载的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!