Write file to dropbox via cronjob.de - javascript

I need to run code in javascript at specific time of a day. And every time when this script is triggered i need to save a file with some information.
So i figured out i'm gona use dropbox API to do that.
I have created new app in my dropbox account and generated token for it.
For test MY_SCRIPT.js looks like this:
var client = new Dropbox.Client({ token: "GENERATED_TOKEN" });
client.writeFile("FILE_NAME","DATA");
I have html file on the server (which i don't have access) with a structure like this:
<!DOCTYPE html>
<html>
<head>
<script language="javascript" src="//cdnjs.cloudflare.com/ajax/libs/dropbox.js/0.10.2/dropbox.min.js"></script>
</head>
<body>
<script language="javascript" src="//PATH/MY_SCRIPT.js"> </script>
</body>
</html>
When i run this html file on my web browser it works fine (new file in dropbox app folder is being created).
But when i put this html address in new cronjob (cronjob.de) new files are not being created. Why? I assume the problem is access to the dropbox API from cronjob server. But how i can handle this?

The problem is that cronjob.deuses an browser / command that won't evaluate javascript. (such as curl)
So your javascript code will never be executed.
You should rather use an backend such as phpto run tasks of this kind.

Related

Why won't my .html file connect to .js file?

I want to connect my .html to .js. I'm trying to run this simple program but it's not working. Below is the screenshot of my file path and files I'm working with.
Here's map.html:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
</body>
<script type="javascript" src="map.js"></script>
</html>
Here's map.js:
document.write("testing");
The problem is below. How can I render the .js file along with the .html file?
Here's views.py:
def map(request):
return render(request, 'personal/map.html')
A common CMS convention is to save JavaScript files in a static folder.
You save anything that you don't want your template engine messing with there: images, javascript, css, etc.
It looks like you may need to save map.js at this path:
mysite/personal/static/personal/js/map.js
After that, you'll need to update you script link in your HTML to something like:
<script src="static/js/map.js">
The src path here isn't relative to where you store the file on your computer, but to the URI that your web server associates with it.
Depending on how you've set things up, you'll need some portion of the new path.
Django has a few ways of linking to static resources, but I'm not familiar enough with the platform to tell you which option you should use.

Include socket.io in the script tag using html

While using html for long time, we used to include the script tags calling for some file within the same folder of the html page, or any other folder but we have to include the exact source of it, something like that:
<script src = "the source of the file and its name"></script>
Using socket.io website, there is index.html file, and the script tag is like that:
<script src="/socket.io/socket.io.js"></script>
but in fact the real source of socket.io.js file is in the node modules, even-though the website is working well, if I include the real source of this file, the website will crash.
I am really curious about the reason of this strange situation, someone explain it to me, please!
The website crashes rightly because, your Socket.IO server will handle serving the correct version of the Socket.IO client library; you should not be using one from anywhere else.
How it works?
you wrap your HTTP server in Socket.IO like this:
var io = require('socket.io')(http);
and it intercepts requests for "/socket.io/socket.io.js" and sends the appropriate response automatically. That is why <script src="/socket.io/socket.io.js"></script> works and the others don't.
Meaning if the server is running, socket.io.js should be readily available.

Saving .json file using javascript

I want to save json file to my application's directory using javascript. how to do it? my code only opens a new tab when you click download button and display the content of my json file. I am allowed to use client-side scripting only. I've tried this code but not working.
NOT WORKING CODE:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("location", true);
s.WriteLine(json);
s.Close();
Here's my code..
HTML CODE:
<!doctype html>
<html manifest="survey.manifest">
<head>
<title>Offline Survey Application</title>
<link rel="stylesheet" href="styles/survey.css" type="text/css">
<script type="text/javascript" src="scripts/survey.js"></script>
</head>
<body>
</body>
</html>
Javascript code
window.onload=function myFunction()
{
var btn=document.createElement("BUTTON");
btn.setAttribute('id', 'dButton');
btn.setAttribute('value', 'download');
var t=document.createTextNode("DOWNLOAD");
btn.appendChild(t);
document.body.appendChild(btn);
btn.onclick=function clickedFunction(){
var url = "http://localhost/JSONFiles/survey.json";
window.open(url, 'download');
}
};
thanks in advance friends! :)
What you are trying to do would kill a lot of kittens.
For safety reasons, a web page cannot use activeX objects on a whim, nor access the local computer resources like file system in any way except what is allowed by standard (basically the cookies and local storage files).
That would allow any Russian hacker and his rabid dog to wreak havoc on computers all over the planet.
You can, however, try to use local storage if your json files are not too bulky and you don't expect to reuse them from another application.
If you plan on downloading json files, just point to them (i.e. put a link to whatever .json file somewhere on your page) and when the user clicks on such a link the browser will pop a save requester to let the user free to save the files in a location of his/her choosing.
If all you're trying to do is get a file on your server to download automatically instead of be viewed in the browser, you just need to set the Content-Disposition header the file is sent to the client with to "attachment". The easiest way to do that is to modify the config files for whatever server your using to add a rule for files of type .json. The exact methodology will differ depending on your server, though.

Load data from text file with ajax/javascript into html problems

I want to load data from this text file that is in the same folder as the html file on my computer but it won't work. In process of learning Ajax and put together this little test for myself test.html and the text file test.txt. Any advice please, would be greatly appreciated.
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script>
function loadData()
{
var test;
test=new XMLHttpRequest();
test.onreadystatechange=function()
{
if (test.readyState==4 && test.status==200)
{
document.getElementById("test").innerHTML=test.responseText;
}
}
test.open("GET","test.txt",true);
test.send();
}
</script>
</head>
<body>
<div id="test"></div>
<button type="button" onclick="loadData()">Get data</button>
</body>
</html>
When I press the button, nothing happens. On the site where I saw a similar example the data from the text file is displayed above the button.
The problem is likely to be that you're accessing the files directly on your local system; web browsers have been designed not to allow this in order to prevent saved web pages loading personal files from your disks and uploading them to remote servers. In order to make it work, you'll need to run a web server locally and use that to view the files. I recommend the Apache web server, which is flexible and can be used on Windows, Linux or OSX.

running .bat file with javascript on html

I need to run a .bat file when the user clicks a link on the webpage. The .bat file basically runs another executable file and saves its output in a text file. So what I want is to create that text file once the user clicks the link to the .bat file on the webpage. Now, my .bat file is working perfectly when I execute it separately it creates the text file with contents, but somehow when I click the link it creates an empty text file. I looked at all the paths, they are all good;
I am using
Batch File ,
I have also tried
function runApp(which)
{ WshShell = new ActiveXObject("WScript.Shell");
WshShell.Run (which,1,true);
}
But both of them just create the text file, and not put the contents
Does any one has any idea about this, also is there any other way to do this, like running the original .bat file and then getting its output in a text file directly with html/javascript?
Appriciate any help
Thanks
You don't say anything about what environment you are working with and I would guess you're not working with a server-side environment. JavaScript normally works in a browser to respond to the user's clicks and mouse moves etc but strictly within the confines of the browser. What you are trying to do is perform I/O operations on the underlying OS that the browser is running in (if you are running locally) or on the server-side OS in a normal webpage environment. It's not just a security issue - JavaScript simply doesn't have any direct connection to the client's OS or the server-side OS for that matter.
What you need is a web server environment like Apache or IIS etc, probably running an environment like ASP.NET, JSP, PHP(with a nice framework like CodeIgniter), or, rather you than me, CGI.
The user clicks a link or a submit button, and sends a request to the server. The relevant server-side program processes the request, runs the I/O operation you talk about and responds with the text. JavaScript is irrelevant in most of that process. It only comes into its own again when you are trying to figure out how to display the response in some fancy dynamic way.
There are millions of tutorials out there:
Tomcat (Java) http://wiki.apache.org/tomcat/GettingStarted
.NET(C# or VB) http://www.asp.net/get-started
Codeigniter (PHP) http://codeigniter.com/
CGI (not for the faint-hearted) http://www.cgi101.com/book/ch1/text.html
Having said all that, there is a server-side JavaScript environment (http://nodejs.org/) but the point is you will always be restricted by the limitations of the http protocol which means that you send a request to a server, the server processes your request depending on your privileges as a client, performing an I/O operation if appropriate, and responds with a stream of HTML. It does not allow direct operations on the server.
None of this is easy. Expect steep learning curves.
Displaying the text file contents
Here's a sample JSP page which will read the contents of a text file then display it on the webpage. I haven't added any buttons or anything - it just demonstrates how to read a file on the server:
<%#page contentType="text/html" pageEncoding="UTF-8" import="java.io.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>BufferedReader</title>
</head>
<body>
<%
String path = this.getServletContext().getRealPath("/target/message.txt");
File file = new File(path);
FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
while(br.ready()){
out.print(br.readLine() + "<BR>");
}
reader.close();
%>
</body>
</html>
/target/message.txt is the virtual absolute path (from the root of the webapp). The call to getRealPath is the way you get a real physical path that allows you to create a File object.
I'll have a look later at using exec to run a batch file, but now you're thinking of a powerful language/library like Java why do you want to run a batch file? Wouldn't it make sense to write the program in Java?
use child_process.spawn of node.js

Categories