且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果文件的扩展名为jpg或png,我如何使用chrome.downloads.onDeterminingFilename更改下载的文件名?

更新时间:2022-11-28 16:14:53


  • 一旦确定了文件名 onDeterminingFilename 事件
    被触发,调用回调函数并且需要2
    参数

    • As soon as the filename is determined onDeterminingFilename event is triggered upon which a callback function is called and it takes 2 parameters


      1. item 包含下载ID,url,文件名,引用等数据的对象。 (请参阅
        https://developer.chrome.com/extensions/downloads#type -DownloadItem

      2. __建议必须同步或异步传递建议。

      1. item object which contains data such as download id, url, filename, referrer etc. (see https://developer.chrome.com/extensions/downloads#type-DownloadItem)
      2. __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 runs to iterate across them.
  • For each iteration based on the action data in the rule the suggest function is called with specific filename and conflictAction.
  • 基本上,文件名是使用 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.