gpt4 book ai didi

javascript - 在 Firefox Addon 中显示目录浏览对话框

转载 作者:行者123 更新时间:2023-11-29 18:12:14 24 4
gpt4 key购买 nike

我想让我的用户通过显示一个对话框来选择一个文件夹。

这在 Firefox 插件中可以通过 JavaScript 实现吗?

enter image description here

最佳答案

是的,有一种简单的方法可以做到这一点。正常的方法是使用 nsIFilePicker .

与该页面上示例的主要区别在于,在传递给 init() 方法的参数中,您将 nsIFilePicker.modeGetFolder 指定为 模式。此外,鉴于您正在寻找一个目录,您希望只包含 nsIFilePicker.filterAll 过滤器,而不是针对特定扩展类型的过滤器。

来自 MDN 页面的示例代码,针对选择文件夹进行了修改(并给出了描述性变量名称):

if (window === null || typeof window !== "object") {
//If you do not already have a window reference, you need to obtain one:
// Add a "/" to un-comment the version appropriate for your environment.

/* Add-on SDK environment:
var window = require('sdk/window/utils').getMostRecentBrowserWindow();
//*/

/* Overlay and bootstrap environments (from almost any context/scope):
var window = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator)
.getMostRecentWindow("navigator:browser");
//*/
}


const nsIFilePicker = Components.interfaces.nsIFilePicker;

var filePicker = Components.classes["@mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
filePicker.init(window, "Dialog Title", nsIFilePicker.modeGetFolder);
filePicker.appendFilters(nsIFilePicker.filterAll );

var pickerStatus = filePicker.show();
if (pickerStatus == nsIFilePicker.returnOK
|| pickerStatus == nsIFilePicker.returnReplace
) {
var file = filePicker.file;
// Get the path as string. Note that you usually won't
// need to work with the string paths.
var path = filePicker.file.path;
// work with returned nsILocalFile...
}

关于javascript - 在 Firefox Addon 中显示目录浏览对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26301351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com