diff --git a/README.md b/README.md index 86e6629..87d7354 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,13 @@ Programmatically open a native "Folder select" dialog in NW.js. Similar to `` but with just JavaScript. -Compatible with all versions of NW.js (even all the way back to 0.12.3). +Compatible with all versions of NW.js. + +**Version** | **Tested** +:--: | :--: +v0.12.3 | :heavy_check_mark: +v0.14.7 | :heavy_check_mark: +v0.40.0 | :heavy_check_mark: ## Installation @@ -20,7 +26,7 @@ npm install --save nw-programmatic-folder-select ```js const openFolderExplorer = require('nw-programmatic-folder-select'); -openFolderExplorer((selection) => { console.log(selection); }); +openFolderExplorer(window, (selection) => { console.log(selection); }); ``` @@ -46,6 +52,7 @@ const callback = function (selection) { } } -// All arguments are optional -openFolderExplorer(options, callback); +// Window is required to have access to the browser context +// All other arguments are optional +openFolderExplorer(window, options, callback); ``` diff --git a/index.js b/index.js index 93b9442..cb61dc3 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,16 @@ /** * Opens the native OS's folder selection dialog + * @param {object} window The "window" object from the browser context. Required. * @param {object} options Optional object for setting the title of the window and the default working directory to start in * @param {Function} callback This is called when the user selects a folder or cancels the window. it will retun the path to the folder or undefined */ -function openFolderExplorer (options, callback) { +function openFolderExplorer (window, options, callback) { // Argument validation var error = false; + if (!window || typeof(window) !== 'object') { + console.log('You must pass in the window object for this script to have access to the browser context.'); + error = true; + } if (typeof(options) === 'function') { callback = options; options = null; @@ -34,8 +39,8 @@ function openFolderExplorer (options, callback) { } // If element does not exist, create it and append to DOM - if (!document.getElementById('nwdirectory')) { - var inputElement = document.createElement('input'); + if (!window.document.getElementById('nwdirectory')) { + var inputElement = window.document.createElement('input'); inputElement.setAttribute('type', 'file'); inputElement.setAttribute('id', 'nwdirectory'); inputElement.setAttribute('nwdirectory', ''); @@ -45,11 +50,11 @@ function openFolderExplorer (options, callback) { callback(evt.target.value); } }); - document.body.append(inputElement); + window.document.body.appendChild(inputElement); } // Modify element based on options - var element = document.getElementById('nwdirectory'); + var element = window.document.getElementById('nwdirectory'); if (options && options.directory) { element.setAttribute('nwworkingdir', options.directory); } else { diff --git a/package.json b/package.json index ded272d..686bcb5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "nw-programmatic-folder-select", - "version": "1.0.1", - "description": "Programmatically open a native \"Folder select\" dialog in NW.js", + "version": "1.0.2", + "description": "Programmatically open a native folder select dialog in NW.js", "main": "index.js", "scripts": { "lint": "eslint --config=.eslintrc.js index.js",