Choose file window in extension firefox - javascript

Iam building an extension and i want to take the path of a file from a window popup like windows does . Is something like that to use in an extension in firefox?

You should check out this page on MDN
Creating a file picker
To begin, you need to create a file picker component and initialize it.
var nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp = Components.classes["#mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
fp.init(window, "Select a File", nsIFilePicker.modeOpen);
First, a new file picker object is created and stored in the variable
'fp'. The 'init' function is used to initialize the file picker. This
function takes three arguments, the window that is opening the dialog,
the title of the dialog and the mode. The mode here is modeOpen which
is used for an Open dialog. You can also use modeGetFolder and
modeSave for the other two modes. These modes are constants of the
nsIFilePicker interface.
Getting the selected file
Finally, you can show the dialog by calling the show() function. It
takes no arguments but returns a status code that indicates what the
user selected. Note that the function does not return until the user
has selected a file. The function returns one of three constants:
returnOK - the user selected a file and pressed OK. The file the user
selected will be stored in the file picker's file property.
returnCancel - the user pressed Cancel.
returnReplace - in the save
mode, this return value identifies that the user selected a file to be
replaced. (returnOK will be returned when the user entered the name of
a new file.) You should check the return value and then get the file
object from the file picker using the file property.
var res = fp.show();
if (res != nsIFilePicker.returnCancel){
var thefile = fp.file;
// --- do something with the file here ---
}

Related

A user-input from an HTML box being used as a variable in a function that already has parameters

This is inside of the google suite of products, so I am using app script for all of my coding. The process of what I want to happen is as follows:
User wants to convert their document or whatever it is to a PDF
They press the drop down menu, which gives them options for PDF conversion of their document (currently there's only one option)
When pressing one of the options to run, a dialog box is created, asking for user input for which folder they want to put their PDF
The user then specifies which folder they want their newly made PDF to go to based on the UID of the folder
Because I can create a function within a function, I thought I'd be able to set a variable to the result of the function that was created WITHIN another function, but when trying to select the 'Export PDF' option, I get an unexpected error while getting the method getFolderById on object DriveApp. Below is the relevant code
JavaScript/App Script:
function exportAsPDF(){
let ss = SpreadsheetApp.getActiveSpreadsheet()
let blob = _getAsBlob(ss.getUrl())
_exportBlob(blob, ss.getName())
}
function _exportBlob(blob, fileName){
let folderUID = function getFolderUID(data){
return data
}
blob = blob.setName(fileName)
let folder = DriveApp.getFolderById(folderUID)
let pdfFile = folder.createFile(blob)
const htmlOutput = HtmlService
.createHtmlOutput('<p>Click to open "' + fileName + '</p>').setWidth(375).setHeight(200)
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Export Succesful')
}
HTML:
function getFolderUID(){
let controls = {}
validateInput(controls) ? google.script.run.withSuccessHandler(google.script.host.close).getFolderUID(controls) : window.alert('Please Enter a Folder UID')
}
function validateInput(controls){
return controls["UID"].value != ''
}

Custom the downloaded file name and prevent number adding in js

I have following simple code for downloading file.
let text = 'download'
function make(){
const data = new Blob([text],{ type: 'type: "text/plain"' })
document.querySelector('a').download = 'untitle'
document.querySelector('a').href = window.URL.createObjectURL(data);
}
make()
<a download>Download</a>
Broswer (in chrome not sure about other broswer) will automatically specify the number for the second or more time of downloading a same name file.
Example:
first time download: untitle.text
second time download: untitle(1).text
......
Is it possible to prevent the browswer of this behavior from happening ?
Thanks
You could try in https://jsfiddle.net/yapb2xus/1/
You just can't, that's the default behavior for most of the browsers and almost all OSes because you can't have two files with the same name.
Since chrome (and firefix I think) automatically save the file in a default path, in the first time the file untitle.text will not exist and will be saved with original name. At second tryz the file will exist on default download path, so the browser automatically add a sufix to it.
What you can try is to create your own sequence, adding a timestamp or something else to the name at every click on the link.

add the current date to a pdf (when opened in browser)

I currently have a javaScript in a pdf that adds the current date once the document is opened in adobe.
function dateToday()
{
var date = new Date();
field = this.getField("todayDate");
field.value = date;
}
dateToday();
Now I want to upload this documents on a Sharepoint. But logically when I open the docs in the browser the javaScript is not triggered and the date is not updated (also when printing).
Is there any way I can change the script so it triggers when the pdf is opened in a browser?
Or how can I change the script so that it instead prints an error message when downloaded and opened in Adobe?
Thanks!!
We use Muhimbi PDF Converter for SharePoint On-Open functionality to add Dynamic Content(Current Date, User name, etc). You can find more details at https://blog.muhimbi.com/2011/04/applying-user-specific-watermarks-when.html
They also provide various watermarking facilities for details see http://support.muhimbi.com/entries/23730002-What-are-the-watermarking-facilities-of-the-PDF-Converter-for-SharePoint-

Auto Save a file in Firefox

I am trying to find a way where by we can auto save a file in Firefox using JS. The way I have done till yet using FireShot on a Windows Desktop:
var element = content.document.createElement("FireShotDataElement");
element.setAttribute("Entire", EntirePage);
element.setAttribute("Action", Action);
element.setAttribute("Key", Key);
element.setAttribute("BASE64Content", "");
element.setAttribute("Data", Data);
element.setAttribute("Document", content.document);
if (typeof(CapturedFrameId) != "undefined")
element.setAttribute("CapturedFrameId", CapturedFrameId);
content.document.documentElement.appendChild(element);
var evt = content.document.createEvent("Events");
evt.initEvent("capturePageEvt", true, false);
element.dispatchEvent(evt);
But the issue is that it opens a dialog box to confirm the local drive location details. Is there a way I can hard code the local drive storage location and auto save the file?
If you are creating a Firefox add-on then FileUtils and NetUtil.asyncCopy are your friends:
Components.utils.import("resource://gre/modules/FileUtils.jsm");
Components.utils.import("resource://gre/modules/NetUtil.jsm");
var TEST_DATA = "this is a test string";
var source = Components.classes["#mozilla.org/io/string-input-stream;1"].
createInstance(Components.interfaces.nsIStringInputStream);
source.setData(TEST_DATA, TEST_DATA.length);
var file = new FileUtils.File("c:\\foo\\bar.txt");
var sink = file.openSafeFileOutputStream(file, FileUtils.MODE_WRONLY |
FileUtils.MODE_CREATE);
NetUtil.asyncCopy(source, sink);
This will asynchronously write the string this is a test string into the file c:\foo\bar.txt. Note that NetUtil.asyncCopy closes both streams automatically, you don't need to do it. However, you might want to pass a function as third parameter to this method - it will be called when the write operation is finished.
See also: Code snippets, writing to a file
Every computer has a different file structure. But still, there is a way. You can save it to cookie / session, depends on how "permanent" your data wants to be.
Do not consider writing a physical file as it requires extra permission.

Integrating CKFinder with InnovaStudio WYSIWYG Editor

I use InnovaStudio WYSIWYG Editor, and I am trying to replace InnovaStudio's Asset Manager with CKFinder. There's a line in the editor configuration for what URL to use for the asset manager. I have pointed it at CKFinder. The part I can't get to work is getting the field to populate with the double-clicked file's path from CKFinder.
It appears to use the 'func' parameter to specify the callback function. The URL I'm calling is: /common/ckfinder/ckfinder.html?action=js&func=setAssetValue
The InnovaStudio WYSIWYG Editor provides the setAssetValue(v) callback function for setting the field value. The v parameter should hold the URL.
CKFinder pops up as expected when it's invoked, but neither double-clicking the thumbnails nor using the "select" option in the context menu works. The normal/expected behavior is that CKFinder closes and the target field is populated with the URL for the selected asset.
Additional Info: The InnovaStudio WYSIWYG Editor has a "popup" for adding an image or flash file to the content. This pop-up is in an iframe. When it calls CKFinder (or it's own asset manager), that is also in an iframe. It appears that CKFinder is looking in the scope of the main window rather than the image/flash iframe that actually contains the field that needs to be populated.
(Sort of) Solution
I discovered, by digging through the DOM with Firebug, that InnovaStudio creates an ISWindow object where it places references to the windows that it spawns. I modified my callback function to loop over that object and call the setAssetValue() function for the appropriate iframe. This worked, but CKEditor still did not close itself. I assume that's because it didn't "know" how to close the iframe that it was inside. Is there a way to tell CKFinder how to close the window it's inside of? I can envision other cases where using an iframe would be useful.
I would prefer to have CKFinder to use the iframe display, but I finally got things working using the standard CKFinder popup.
Editor config line: oEdit1.cmdAssetManager = "parent.BrowseServerIS();";
Supporting functions:
// InnovaStudio WYSIWYG Editor version
function BrowseServerIS()
{
// You can use the "CKFinder" class to render CKFinder in a page:
var finder = new CKFinder();
// The path for the installation of CKFinder (default = "/ckfinder/").
finder.BasePath = '/common/ckfinder/';
// Name of a function which is called when a file is selected in CKFinder.
finder.SelectFunction = SetFileFieldIS;
// Launch CKFinder
finder.Popup();
}
// InnovaStudio WYSIWYG Editor version
function SetFileFieldIS(fileUrl, data)
{
for (var i in ISWindow.objs) {
if ((null != ISWindow.objs[i].rt.frm.contentWindow)
&& ('function' == typeof ISWindow.objs[i].rt.frm.contentWindow.setAssetValue)) {
ISWindow.objs[i].rt.frm.contentWindow.setAssetValue(fileUrl);
}
}
}

Categories

Resources