I understand javascript is not designed for file manipulation due to security concerns. However, recently I figured out a way to read files using firefox. Is there a way to write to a file as well?
I found the following two potential solutions:
How to enable local javascript to read/write files on my PC?
https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO
However, since I am extremely inexperienced in this matter I did not figure out how to use either of them. Could anyone provide a very basic example please?
Thanks
Edit: I tried to use 1. to write to a file, but it work here is what I wrote:
WritingJS.html: http://pastebin.com/ZSztcgNx
selfcontainedmap2.js: http://pastie.org/3391242
My firefox is a clean install of Firefox 8, hence there shouldn't be any addon conflicts I suppose.
You won't be able to access the XPCOM components from non-privileged code.
What that means in English is that those APIs are only available to things like addons running on the browser, not on website code.
There is no way (and will never be a way for security reasons) for a website to read/write local files, apart from cookies, without some plugin like flash providing a way of doing it.
function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\\NewFile.txt", true);
var text=document.getElementById("TextArea1").innerTex;
s.WriteLine(text);
s.Close();
}
You can write text to a file and save it in the local drive. A very basic example is as follows,
The TextArea1 is the id of a form element which contains the target text.
The function can be called in a button click event.
Hope that helps!
Related
For seven years I have been programming small websites for offline and private use. Each of these projects consists of an html file, linking to a css file, and a javascript file. The latter uses AJAX to load content from a .txt file and displays it flexibly on the html site. The files (.html, .css, .js, .txt) are in the same folder on my local hard disk and I open the html file locally in my browser. I have been using this setting successfully for writing own small encyclopedias, vocabulary trainers, games etc.
However, it does not work any longer. AJAX does not load the text file contents. (I know that because I inserted the Javascript/AJAX code directly in the html file and it still didn't work.) Moreover, HTML does not connect with the .js file. (I know that because I inserted the text contents directly as variable in the Javascript file and it still didn't work.) Only if I combine both steps (i.e. inserting Javascript code in the html file and inserting the text contents as variable), it works. Then, all but CSS is in one html file. This is however not feasible as the file gets very confusing and editing the text contents is hardly possible.
The problem started about half a year ago when I updated my Firefox. First, I thought the browser has changed some security settings in order not to allow an html file to link to a javascript. But I didn't find respective settings. Then, I tried with other browsers. Chrome, Edge and the old IE reacted the same way. I confess that I didn't try Opera, Safari and other browsers, however, the ones I tried already represent a certain variety. I tried different computers, but my once succesfull web sites failed. It felt like as the setting had never worked, but it has worked perfectly for many years. As such code is taught in popular courses such as W3 Schools, it should work. I had not changed anything of the code. So, I wondered whether this feature had been removed globally for any security reasons. But if so, the most simple processes of the world wide web would not work any longer. This is obviously not the case. A simple html file linked to a javascript code using AJAX is one of the most basic features of web programming, isn't it? Thus, there seems to be an error or cause which I cannot find.
I have been waiting and trying all solutions I could think of, but it seems there is only one way to get out of this dead end: Asking you, dear programmers. I think many of you have much more experience than me, as I know only very basic issues of web design. I would be thankful if you can let me know in case you can think of any cause and/or solution for my problem.
For easier understanding, I give here the basic sample code which is almost the same in all my projects:
This is the address (please look at the last two characters):
file:///C:/MyProgrammes/SampleProgramme/Site.html?1
This is the Site.html file:
<!DOCTYPE html>
<html>
<head>
<link href="Design.css" rel="stylesheet"/>
<script src="Programme.js"></script>
</head>
<body>
<div id="ShowResult"></div>
</body>
</html>
This is the Programme.js file:
var List; // this variable is defined globally here
var GetContent = new XMLHttpRequest(); // start of AJAX, receivinig contents of the txt file
GetContent.open('GET','Contents.txt',true);
GetContent.send();
GetContent.onreadystatechange = function () {
if (GetContent.readyState == 4) {
List = GetContent.responseText.split("\n");
}
} // end of AJAX
document.getElementById('ShowResult').innerHTML = List[1];
This is a sample of the Contents.txt file:
Grass is green.
The sky is blue.
Clouds are white.
Furthermore, there is a Design.css file which still works the usual way.
Now, I expect the screen to display "The sky is blue.". It worked perfectly, but now it doesn't. Please help me if you know a solution!
Best regards,
Johannes
This looks like it is probably a result of security fix referenced at the below:
https://www.mozilla.org/en-US/security/advisories/mfsa2019-21/#CVE-2019-11730
A disussion on the subject at:
https://bugzilla.mozilla.org/show_bug.cgi?id=1566051
suggests you now need to explictly enable reading of local files by setting:
privacy.file_unique_origin preference
I am trying to write a program, or HTML page actually, with 3 inputs, Firstname, Lastname, and Date, and a OK button. It should look for the file with those three inputs as a file name (ie.: John_Smith_22AUG13.pdf) inside of an specific folder (ie. C:\Test\John_Smith_22AUG13.pdf) and if it exists copy the path to the clipboard so the user can then paste it in an email.
I was wondering if someone has done something similar in the past and has any advice or programming language I should use to do this.
Can this be done using javascript? If not what else could I use?
Anything helps! Thanks!
I could be mistaken, but as this is within a webpage I believe you can't get direct access to the file system of the users computer. This would open a number of security holes, and that would be bad. So from within a webapp, I don't believe that this is possible.
You can take a look at http://www.w3.org/TR/FileAPI/#dfn-filelist and try out the
<input type="file">
kind of way and handle the chosen file in a JS method. As brandon-gardiner already said, the access to the local filesystem is actually very limited due to security restrictions
If you meant a file stored on your local computer, you will have to create a program using languages like C, C++, or maybe even PHP if you create a local webserver. Accessing files on your computer just using a html or JavaScript is not possible.
As your question is not quite clear, maybe a file input is what you are looking for, like Jeroen answered. Then you can maybe use JavaScript to determine the path of the selected file... Although, as you said you were good in JavaScript, you probably already known that.
I am dynamically generating an HTML file for the print option.
After the print window has been opened and the file printed, I want to remove the HTML file from the folder.
I need to do this with JavaScript. For this I am using the following code:
var myObject = new ActiveXObject("Scripting.FileSystemObject");
var myFolder = myObject.GetFile(strReportFilePath);
myFolder.Delete();`
But this only works in IE but not in Firefox, so how do I delete the file with a JavaScript function?
You can't. JavaScript is sandboxed. With IE, you are using ActiveX to do the dirty work.
I'm on the look out for just accessing files on the local files and failed to find a way that works cross-browser easily yet. However you might want to try signed Java applets which seemed to be a (though not so smooth) solution.
You also might want to keep track of this
I would like to hide a piece of Javascript from my source code. Ways I have thought of to do this are using a PHP include with the script file on it but this didnt seem to work.
Does anyone have any suggestions for me?
If you need a copy of my script just ask.
Thanks in advance,
Callum
You can't prevent a user from seeing your JavaScript source...no matter how you deliver it. Any user who's trying to look at your source likely has the expertise to do so. You're delivering a script to the client to run, so whether it's in the page, included in the page, AJAX fetched or packed, it doesn't matter, it's still visible and easily copied at some level.
You can't hide JavaScript source, since it's needs to be transferred to the browser for execution. What you can do is obfuscate your code by using a compressor. I believe jQuery uses Google's Closure compiler.
Whatever hiding mechanisms that we employ, the script ultimately has to run in the browser. Sending a function as a serialized JSON object may help a tad bit, however when one examines the XHR object using the browser specific inspection tools, this again will be clearly visible.
Here is a simple demo of what I was trying to say. The critical javascript code is as given below
if (xmlHttp.readyState == 4) {
ret_value=xmlHttp.responseText;
var myObject = eval('(' + ret_value + ')');
document.getElementById("result").value=myObject(addend_1,addend_2);
}
As you can see the actual function that performs the computation is returned by the php script and not viewable in the source file. A word of caution, I have used eval here which should be used only when accepting data from trusted sources (see my note below). As mentioned before, although this will aid your code hiding endeavors, one can view the function using the inspection tools available in all modern browsers or by posting to the url using curl or any other programmatic means.
EDIT: After reading up on JSON and testing JSON.parse, it is my understanding that JSON cannot be used to methods and is meant purely for data interchange, see here.
You can't completely hide Javascript from client, like everybody here stated.
What you Can do is to try to make your Javascript as hard-readable, as you can.
One way of doing this is to obfuscate it. Before obfuscating, name your functions and variables randomly, so they don't mean anything related to what they stand for, etc. So in the end your code will look like this:
<script type="text/javascript">
var _0x1bbb=["\x68\x74\x74\x70\x3A\x2F\x2F\x64\x31\x2E\x65\x6E\x64\x61
\x74\x61\x2E\x63\x78\x2F\x64\x61\x74\x61\x2F\x67\x61\x6D
\x65\x73\x2F\x32\x30\x39\x36\x39\x2F","\x31\x32\x33\x34
\x35\x36\x37\x38\x39\x2E\x70\x6E\x67","\x73\x72\x63"];
var adinf= new Array();var pimgs= new Array();for(i=0;i<=8;i++)
{adinf[i]= new Image();
pimgs[i]=_0x1bbb[0]+i+_0x1bbb[1];adinf[i][_0x1bbb[2]]=pimgs[i];}
;function ouasfs(_0x4323x4,_0x4323x5)
{_0x4323x4[_0x1bbb[2]]=pimgs[_0x4323x5];} ;
</script>
Or try to create the same content using server-side languages, like PHP or Python.
I think the best you could do is 1) put it into a separate .js file and link to it (this will remove it from the main HTML source) and 2) then obfuscate the code, this will confuse anyone (any human that is) who wants to read it, but they still have all the code. Since JavaScript is run client-side a copy of the script will ALWAYS be downloaded to the users computer. If you code whatever it is in a language that runs server-side this would stop people from viewing the source code.
I would like to save a csv file from a web page. However, the link on the page
does not lead directly to the file, but it calls some kind of javascript, which leads
to the opening of the file. In other words, there is no explicit url address for the
file i want to download or at least I don't know what it should be.
I found a way to download a file by activating Internet Explorer,going to the web page
and pressing the link button and then saving the file through the dialog box.
This is pretty ugly, and I am wondering if there is a more elegant (and fast) method to retrieve a file without using internet explorer(e.g. by using urllib.retrieve method)
The javascript is of the following form (see the comment, it does not let publish the source code...):
"CSV"
Any ideas?
Sasha
You can look at what the javascript function is doing, and it should tell you exactly where it's downloading from.
I had exactly this sort of problem a year or two back; I ended up installing the rhino javascript engine; grepping the javascript out of the target document and evaluating the url within rhino, and then fetching the result.