Error creating file in firefox extension - javascript

I am trying to create a file in the extension using this code:
Components.utils.import("resource://gre/modules/FileUtils.jsm");
var file = new FileUtils.File("C:\\Windows\\hello.txt");
But nothing happens.The file doesn't appear
Any ideas?

Your file var is an object that represents a file at the location you specified. Creating this file object does not create a file directly (you might instead choose to read from the file, for example).
You can now use the nsIFile API to manipulate the file object. For example, you can create a file at that location:
file.create(file.NORMAL_FILE_TYPE, parseInt("0600", 8));
Note that Windows UAC can cause file access to fail. You might want to try:
file.isWriteable();
but ultimately you might find that it's not possible to write to directories that UAC is protecting so you can instead choose a non-protected location, perhaps using the special directory definitions explained on this useful MDN page: https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O

Related

Turn CSV file into array using Vanilla Javascript without input element

I am trying to read turn a CSV file, the file is on local, could be same folder with the script file. Since I am writing JSX for photoshop, I couldn't use any other library. And there are a lot of tutorial out there using input element which is not what I need. The path of the file could be hard coded. What I am trying to do is read the CSV, and take out some data. Please advise!
Let me explain it clearly!
I am writing JSX for photoshop script which has no browser element - input tag something like that. And it must be pure Javascript no library such as jQuery. I did a lot of google search what they do is taking the input tag from browser let user select the CSV file, I just want the file path is hard code, it is a fixed path and filename. And I don't see any tutorial for read CSV file and turn into array via vanilla javascript.
You can use the File class. How this works is explained in the ExtendScript toolkit docs which are installed on your computer alongside Creative Cloud. An online version can also be found here. (The scripting guide references this under the File object on page 110, referring to a section about JavaScript on different platforms on page 32, which then refers to the ExtendScript docs.)
Example:
const file = new File("/c/Users/user/Desktop/text.csv");
file.encoding = 'UTF-8';
file.open("r");
const contents = file.read();
file.close();
alert(contents);

Read local .json from local .js without repos

I'm trying to write a chrome-extension that closes tabs when they're loaded and their links contain specific keywords, which I've saved in a .json file. Because the content.js has no access on the browser peripherals, I had to use the background.js script to close the tab. So far the extension does all that except loading the data from the .json, which means that I had to write the json (just for testing) into the background.js. Because I want to ban a lot of links this is'nt an option for me. So I decided on storing a list with the links in a .json file, which is stored in the same folder as the background.js, which means that it's on the localhost and not on any kink of webserver. That means that it should be possible to access this file, because after my research, I came to the assumption that the background.js runs on the localhost as well. So there should'nt occur any file accessing limitation issues right?
Unfortuantely I've got no idea how to write this in pure .js, because all the tutorials or posts here are either accessing the file from or in a webserver or using some kind of fancy library. This should be possible without one right?
chrome.webNavigation.onCompleted.addListener(closeTab, {
url: [
{urlPrefix: 'https://www.google.de/'},
{urlPrefix: 'https://sghm.eu/iserv/login'},
]
});
function closeTab(e) {
if (!e.frameId) {
chrome.tabs.remove(e.tabId);
}
}
this is how my code looks now, I want to have some kind of loadData('data.json') function that returns the .jsons content, so that I can delete this whole .json data strucure within the js.
It should be possible to access the javascript object notation language via javascript.
I personally think you can do this far easier and faster with using Chrome.storage. The data is saved as a json object and easy to reference and it sounds like exactly what you need as you're just referencing key/pair values anyway.

Linking a FileAttachment Annotation to the File

As the title suggests I'm trying to understand how to link a FileAttachment annotation with the actual file in javasacript. There are no properties that do this while I'm performing the addAnnot function. So after I've used addAnnot to create the annotation how would I link it to open an actual file?
Appreciate the help
Something like this but with a few caveats. First, you need to use a device independent path to the PDF, you can find out more in the Acrobat JS reference. Also, if you want to attach the file without user interaction, the script must be in a privileged context. Again, more on that in the JS reference. If you want the user to be able to select the file, leave cAttachmentPath empty and they'll get a file selection dialog. Note: Many file types like executables, .js, etc. can be attached but then not extracted by Acrobat or Reader for security reasons.
var annot = this.addAnnot({
page: 0,
type: "FileAttachment",
point: [0,0],
cAttachmentPath: "/c/temp/foo.pdf"
});

Can you locally map a dynamically loaded JS file (using sourceURL)?

I understand this might just be impossible but when you're making JS available for easier debugging in devtools via the helpful //# sourceURL comment, I'd also like to map it to its respective local file, for easy editing.
Clarification on #// sourceURL=dynamicScript.js:
Note: Notice the "//# sourceURL=dynamicScript.js" line at the end of dynamicScript.js file. This technique gives a name to a script created with eval, and will be discussed in more detail in the Source Maps section. Breakpoints can be set in dynamic JavaScript only if it has a user supplied name.
The sourced file now exists in Sources under "no-domain", and is unable to map to my workspace's dynamicScript.js file.
You can map your local web app directory to a server path, so that you can live edit the JS file that evaluates some code, but there is no way to map the dynamically generated named script to a file on the system as far as I'm aware.
If you use eval to execute code from a string, adding //# sourceURL=dynamicScript.js' simply tells Chrome to simulate that script being an actual file, so that you can debug etc. The file doesn't actually exist, it's in memory. The dynamic 'file' cannot appear as part of a local workspace because it simply doesn't exist on the system.

Including a text file in Chrome extension and reading it with Javascript

I want to create a Chrome extension that contains a text file with static data (a dictionary of English words) and I want the extension to be able to parse that file. I've only managed to find FileReader class, but it looks like it's made for reading user-selected files, while in my case I always want to read the same exact file included in extension's package. As a workaround, I can convert the file to a Javascript array of strings declared in some .js file included in the manifest, but in that case the whole contents would be loaded into memory at once, while what I need is to read the data line by line. Is there any way to do this?
You can go the FileReader route, since you can obtain the Entry of your package directory with chrome.runtime.getPackageDirectoryEntry().
However, an easier way is to just make a XHR to your file using chrome.runtime.getURL() with a relative path. The first way is useful when you want to list files, though.

Categories