i am trying to clear a txt file in javascript - javascript

I have a text file that has some strings in it. I am trying to clear it to put something else in it. What is the right code?
I have tried File.Clear() but it keeps throwing errors.

Assuming you're using nodejs and not a browser (in a browser you have other means of persistence like localstorage, but not files), check this and remember, you use an in memory representation of the file, if you want an empty one you can create one anew, if you want to clear the contents on the disk you need to write an empty (or whatever content you need to write) on the disk.

Related

How to link to text file in HTML for script use

What I'm after is this idea: The document contains some link to a text file (for load order reasons, I have several excessively bulky script and image files as well as a huge wall of HTML and want to get this operation done within 5 seconds even on 5kb/s) and then a script is able to reference this text file (to avoid messy code), a bit like:
textFile = document.getElementById ("textFileLink");
someText = textFile.read ();
doSomething (someText);
Some ideas I have tried:
Use the link toString method mentioned in passing in the living standard, this merely returns the url itself.
Instead have a script which exists solely to dump a 10k character string into a global variable (definitely bad)
As above but into a display:none HTML element (maybe not quite as bad?)
As above but LocalStorage?
is this possible, or do I have to do some kind of server-side black magic?
Try using the fetch API:
fetch('path/to/demo.txt').then((res) => res.text()).then((data) => {
// code
});
Fetch should be relatively quick... tell me if it works or if there are errors

Can you print/write the result of screen.debug() to a file?

In one of my tests I am expecting some kind of DOM change. However, the page it's on is quite long.
So what I usually do for smaller components is to use screen.debug() method. But since the file is quite long, I started to also run the test task with DEBUG_PRINT_LIMIT=50000. Now that eventually got the result I got.
But that made me wonder, is it perhaps possible to save the output in a file?
According to the docs, screen.debug is essentially a shortcut for console.log(prettyDOM()).
So you could just use prettyDOM() directly, and do whatever with the result.
I would do copy(prettyDOM()) to put it on the clipboard and then paste it in a text file manually (in Chrome) or save it into a file (in node).

How to attach large amounts of data with tampermonkey script?

My script adds some annotations to each page on a site, and it needs a few MBs of static JSON data to know what kind of annotations to put where.
Right now I'm including it with just var data = { ... } as part of the script but that's really awkward to maintain and edit.
Are there any better ways to do it?
I can only think of two choices:
Keep it embedded in your script, but to keep maintainable(few megabytes means your editor might not like it much), you put it in another file. And add a compilation step to your workflow to concatenate it. Since you are adding a compilation you can also uglify your script so it might be slightly faster to download for the first time.
Get it dynamically using jsonp. Put it on your webserver, amazon s3 or even better, a CDN. Make sure it will be server cachable and gzipped so it won't slow down the client network by getting downloaded on every page! This solution will work better if you want to update your data regularly, but not your script(I think tampermonkey doesn't support auto updates).
My bet would would definetly be to use special storage functions provided by tampermonkey: GM_getValue, GM_setValue, GM_deleteValue. You can store your objects there as long as needed.
Just download the data from your server once at the first run. If its just for your own use - you can even simply insert all the data directly to a variable from console or use temporary textarea, and have script save that value by GM_setValue.
This way you can even optimize the speed of your script by having unrelated objects stored in different GM variables.

Read/Write file causing errors. (javaScript & C++)

I have a piece of javaScript that works (alongside html) to produce the GUI for a program written in C++. The program has to run for a long time (Sometimes 14/15 days, without monitoring).
The C++ and javaScript communicate by writing to/reading from a XML file.
After running the program for over 24 hours at a time, I've noticed the occasional javaScript error appearing 'someArray[...].name' is null or not an object.
Now: These are all arrays that are filled with information taken from the XML file, written by the C++. The contents of these arrays are refreshed every few seconds (To update information in the GUI 'live').
Question is: Could these errors be caused by an access/timer problem as in --> The javaScript starts reading a line from the XML just as the C++ swoops in and rewrites that line. Therefore information is parsed into the javaScript arrays with some illegal characters (etc) which when accessed throws the errors?
Hope that all makes sense. Thanks.
Your suggestion seems to provide a plausible explanation of what's happening.
You're probably seeing a race condition.
To fix it, you could implement a synchronization mechanism between C++ and JS.
The simplest form of sync I can think of is creating a second file each time C++ writes to your main XML file (this file acts as a lock). JS waits for the lock file to disappear before reading the XML. The same is done on the C++ side.
Sample code:
C++:
while(programRunning) {
do stuff;
// Now it's time to write XML
while("lockCpp.txt" exists)
; // Do nothing, JS is reading
create file "lockJS.txt";
write to xml;
delete file "lockJS.txt";
}
JavaScript:
while(programRunning) {
do stuff;
// Now it's time to read XML
while("lockJS.txt" exists)
; // Do nothing
create file "lockCpp.txt";
read xml;
delete file "lockCpp.txt";
}
This should in practice eliminate race conditions (though some are theoretically, possible, but unlikely).
Should JS not be allowed to write to the file system, then you could remove one of the lock files (lockCpp.txt) and, if the reading on the JS side is normally faster then the writing, it should still eliminate most conflicts.
EDIT after comment:
If you only have access to JS, you could check that the XML document is complete when reading, e.g. the root element is correcly matched by a </rootElementName> at the end.
That will ensure the file write is complete provided C++ doesn't do writes at random locations, but always rewrites the whole document.
Another route would be checking the file is not changing over time. If C++ only sporadically writes to XML, you can read it a few times over a few, say, seconds, and, if unchanged, use the read value. If changed, keep waiting.
HTH

Using scripting.filesystemobject in javascript and checking for locked files

I have some code that reads from an ini file in Javascript, using activex filesystem objects.
This isn't particularly efficient but does the job, reading the whole file into an array, appending any changes and writing back.
The problem i'm having is that another process, a C# XBAP application is reading from this ini file (using getprivateprofilestring) at the same time as I could potentially be trying to write to it in the JS.
The javascript fails as the file is locked, or part of it, and the file ends up getting corrupted or even totally cleared - as I am trying to write back the whole file each time.
Preferably, what I need is a way to determine if a file is locked in javascript, as the writes are not urgent and I want to let any reads finish first.
Just can't seem to find anyway of syncing these two completely seperate ways of accessing the file.
May be you could use try/catch. If you open the file for appending (OpenTextFile([filename],8)) it should raise an exception. Same should be true for writing/saving the file (if the file is locked try raises an exception).

Categories

Resources