Leveraging nsIFilePicker.modeGetFolder in addon sdk

My extension creates a Panel that displays the currently selected preferences for the extension.
I want to populate the Panel with a button, where clicking on it triggers a dialog allowing the user to choose a directory to save files in.
This functionality was possible with XUL based extensions, but it doesn't work with SDK based extensions as the window object is not accessible.
Is there a workaround for this issue?

In the extension code, I create the Panel using:

var confirmDialog = require("sdk/panel").Panel({
    width: 250,
    height: 250,
    contentURL: data.url("confirmDialog.html"),
});

confirmDialog.port.on("selectDir", function () {
    chromeManager.selectDir();
});

The content of confirmDialog.html specifies the Panel's content:

<div id="pathToFile"></div>
<button onclick="Panel.selectDir();">
</button>

The confirmDialog.js file sends a message to the extension code:

var Panel = {

    selectDir: function() {
        addon.port.emit("selectDir", '');
    }
};

chromeManager.js is part of the extension code, so it has access to the SDK API.

exports.selectDir = function() {

    var nsIFilePicker = Ci.nsIFilePicker, 
    fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);

    fp.init(window, nsIFilePicker.modeGetFolder);

    var ret = fp.show();

    if (ret == nsIFilePicker.returnOK || ret == nsIFilePicker.returnReplace) {
        document.getElementById('pathToFile').value = fp.file.path;
    }
}

However, when running this code, I encounter

ReferenceError: window is not defined
. Is there a way to make the window object accessible here or an alternative approach to achieve this functionality?

Answer №1

If you require access to the current window for interactions with the nsIFilePicker API, you can utilize window/utils and specifically the function getMostRecentBrowserWindow().

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the importance of typescript requiring the use of generics?

const typeFunction = <T>(a:string, {c,d} = {c: T[], D:T} = {})=>{} In what way does TypeScript enforce the usage of generics? I am looking to create a function that requires a specific type (T) to be passed in when it is used. typeFunction<st ...

Displaying JavaScript validation errors on a Bootstrap 4 .btn-group-toggle

I have created a star rating user interface using the Bootstrap 4 .btn-group.btn-group-toggle as shown in the code snippet below: <div class="form-group"> <div class="btn-group btn-group-toggle" data-toggle="buttons" aria-label="Rate this"> ...

Utilizing JSONLoader to load several models simultaneously

Seeking advice on the most effective method for loading multiple models exported from Blender to JSON format. In the provided example below, I am utilizing the static_objects array containing predefined object data and model URLs. These URLs are then iter ...

What could be causing checked="checked" to fail validation in JavaScript?

I'm feeling a bit puzzled about this issue. Here's the HTML code I have - <input type="radio" id="20577091" name="q_engine" value="20577091" style="position: absolute; opacity:0;" checked="checked" /> and here is the corresponding JavaScr ...

Issue: Unable to retrieve the property "div" as the animated object is not defined

Recently, I decided to enhance my JavaScript skills by following a tutorial on creating a dating site using the react-tinder-card component. However, during implementation, I encountered an error message: Uncaught runtime errors: ERROR can't access pr ...

Refreshing the settimeout function through a click event

I'm working on a feature where I use setTimeout to dynamically change the font size of paragraphs. My goal is to have the font size reset when the user clicks on the "reset" button or initiates the timer again. However, I've encountered an i ...

Having trouble retrieving list data in JSON response while utilizing the ASP.NET Web API in MVC

In my Project, this is the JavaScript code: function RetrieveData() { debugger; var url = 'http://localhost:50951/api/Home'; $.ajax({ type: "GET", url: url, ...

Updating a connected model in Sequelize using another model

Seeking guidance on updating a model with new associations in Sequelize. The model involves a many-to-many relationship with a join table. Attempted this code snippet: app.patch('/api/team/:id/newplayers', function(request, response){ const pl ...

Mastering the art of ajax and j Query integration with Spring

I'm fresh to working with Spring and AJAX. My task involves checking whether an email is already registered or not when the user clicks on "check availability." I can successfully make calls to the controller class, but the response is being displaye ...

Saving information to a local file using JavaScript or AngularJS

As a novice in JS development, I am eager to learn how to write data to a file stored in local storage. Within my application, I collect raw data and store it in MongoDB as key-value pairs. Simultaneously, I also wish to save this data to a file in local ...

Troubleshooting module not being found with rekuire, requirish, or rfr in resolving relative require problem in nodejs

Looking to steer clear of the complicated relative path problem detailed here by opting for one of the suggested solutions. I've found three similar libraries that could help: rekuire node-rfr aka Require from Root requirish I've experimented ...

When using jQuery's ajax() function, it is possible to pass HTML as data and change the ampersands in URLs to &amp;

I have encountered an issue where I am capturing the HTML content of a page and attempting to send it as a string to a PHP web service using jQuery's ajax() function. However, when I receive the parameter on the PHP side, the &s in the URLs present wi ...

Exploring the method to find all corresponding keys within deeply nested objects

Within my 'const', I have an array filled with objects. Each object contains a key called 'image' which holds the value for 'url' as illustrated below. const images =[ { "image": { "url& ...

The process of loading the Facebook like script using $.getScript is causing an issue where the

How can I make the Facebook like button display properly on my HTML page? I have successfully loaded scripts and divs for Twitter, Google +1 buttons, but the Facebook like button script is not displaying the button. The alert shows that the script is exec ...

Nesting maps in JavaScript is a powerful way to transform

I'm in the process of developing a budgeting app using React and JavaScript. At the moment, I have successfully generated a table displaying various costs. Name Budget Used $ Used % Available Food 300 300 100 0 Streaming services 600 600 100 ...

Utilizing Gigya API integration with node.js

Looking to incorporate Gigya as my social network connection provider for a project and developing the app with Node.js. Wondering if anyone has experience with this combination? Gigya offers a JavaScript API specifically designed for client-side use. Th ...

Unable to run EJS script inside ajax 'success' function

I am facing an issue with my EJS page that utilizes AJAX to retrieve data. After loading the data, I intend to add specific content to a div. Within the added content, there are checkboxes controlled by the following script: <script> $('#select ...

Prevent user input in HTML

Currently, I am working on creating the 8 puzzle box game using JavaScript and jQuery Mobile. The boxes have been set up with <input readonly></input> tags and placed within a 9x9 table. However, an issue arises when attempting to move a box ...

The checkbox fails to display as selected in the user interface despite setting the checked property to true in JavaScript

In the table generated dynamically using an ng-repeat, I have checkboxes. The checkboxes are created as shown below: <input type="checkbox" id="chkView{{::myObj.TeamId}}" ng-disabled="disableView(myObj)" ng-click="setViewSelectio ...

Angular service that iterates through each $q.promise

Here's the function I am working with: this.getBenchmarkData = function () { var benchmarkData = []; var d = $q.defer(); users.forEach(function(user){ var dataArray = []; modules.forEach(function (module) { ...