Run a script in a google cloud from a local html page - javascript

I would like to run a script in a google cloud server using a local HTML page.
To be more clear the steps would be:
open a local HTML page on my local computer.
push a button that triggers a script in my google cloud server.
the script creates a file in the server that I can download pressing
another button.
I'm new in this field and I don't know where to start.
How do I connect to the server via HTML? (PHP?, Javascript?)
How does the authorization process work?

There are several languages and strategies that you can use.
You can use locally Javascript or PHP (it needs installation and configuration) that will allow you, for example, to make an HTTP request (it may also be another protocol), to a script (that can be in PHP or Javascript or others) on the server, which, upon receiving the request, processes and generates a file for a specific path.
Then on that other button you make a request to that path to download the file.
My suggestion is to choose the languages and implement with these to understand the process.

Create an HTML page, put a button.
Attach a function to button onClick to send an Ajax request
That would cause cross server request challenge for you down the road..
You can simply put a URL from your local web page styled as a button to your Google Cloud hosted application.
Create the file on the server side and you can set an HTML header
Content-Disposition: attachment; filename="results.csv"
to make a file downloaded to the user end.

Related

How to know if a JS file is loaded on server side?

I'm a developer for the website Friconix, a collection of free icons. Users can include our JS file on their web pages as explain here : https://friconix.com/start/.
I would like to gather statistics on clients using our tools. How to know, on server side, information on pages (URL or at least domains) that request our JS file ?
It's important to explain that, for decreasing the loading time, the JS file is not dynamically generated. There is no PHP file loaded every time the file is requested. The file is saved in plain text on our server. I wonder if the right solution is not to add something in the .htaccess file ?
Since the script is requested from your server every time a user loads a browser-page you can track who and how often that path is requested.
A simple approach is that it will be present in you request log files. So you can create a script and read your log files every so often.
A second approach is to setup a special rule/location in nginx/apache/which-ever-server-you-are-running
A third approach is to serve the script via CDN that has all these attributes built in (ie. CloudFront)
This can be done via a simplistic REST API call from the script. Thus when your script will load it will call the rest API via an AJAX or XHR call. The request can contain a unique client ID. On the server-side, you can implement a simple API that will accept these requests and store them by extracting the necessary information for analytics.
All the information like domains and IP about the client can be gathered from the API request or requests which will be made from clients page.
Reference - How do I call a JavaScript function on page load?

Loading .html and .js with websockets

I have the idea of having something like a web server but without a web server. Instead I want to use websockets. Is for internal use.
The user will have a basic webpage which will only open a websocket connection and should receive an .html, and immediately navigate to it.
Also it should be able to load a .js with helper functions for this new html.
I saw here something to load an image file
http://liamkaufman.com/blog/2012/02/11/12-pushing-files-to-the-browser-using-deliveryjs-socketio-and-nodejs/
but I don't know how to navigate to a received .html file and how to execute a received .js file.
Do not try to convince me of using a webserver or other technologies, I have my reaasons for doing this :-)
Thanks
Using a websocket inherently means you need to use a server. A websocket is be definition a connection between the client and the server.
From here
WebSockets is an advanced technology that makes it possible to open an interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.
Regarding running a javascript file that you have downloaded, you can use eval, or you can create a new script tag on the body to load your javascript.

How to cache remote images in a NodeJS / AngularJS app?

The scenario:
A user posts a link to a website
A little AngularJS service fetches a preview, including an image
This information (including the remote URL of the image) is copied to a hidden form and sent to the server when the user clicks submit.
Now I have to download the image to my server for the preview. Otherwise I would be hotlinking them which isn't good and could also cause problems later.
Because of the cross-origin policy I guess this can only be done server side.
My idea so far is to create a little API where I pass in the URL to the image and the server checks if a local copy exists and if not downloads one on the fly
http://example.com/staticapi/v1/get?img_url=ENCODED-IMG-URL
What I don't like about this idea is that a malicious user could just bombard this system with URLs.
An alternative would be to save the remote URL temporarily and let a background job download any remote images that haven't been processed yet.
How would you approach this?

Chrome Extension - Process output with a hosted PHP script

I have a chrome extension I've developed that reads and parses HTML contents into a CSV file.
This part works great, and the user is able to download the file.
What I want to do at this point, is find a way to send that data directly to a MySQL database.
I know that a chrome extension cannot run PHP (obviously). I do however have access to a hosted web server to which I can upload custom PHP scripts.
Is there any way to send my parsed data directly to a hosted PHP script which will process the data and then send the data to a MySQL database on the same server?
Everything I've researched thus far has resulted in "chrome extensions cannot run PHP scripts" . . .
Thanks for your help!
Edit: One option I was thinking of would be to inject HTML hidden fields with values of my data, and then use javascript to POST the data to the PHP URL . . . . Would that be an option? Can chrome extensions inject hidden fields?
You can absolutely make a POST method HTTP request to a server from a chrome extension. It doesn't require any kind of hack to accomplish this. With native JavaScript use the 'POST' method to an XMLHttpRequest or with a library like jQuery just make a $.post request.
A couple of things:
You're going to want to use HTTPS, so get an SSL certificate for your server/domain. If you are parsing any kind of website content on an HTTPS protected page and sending that in the clear text to your own server you are doing A Very Bad Thing. Do Not Do That.
Make sure you tell your users that you are doing this. Some/Most people may not want that.
Use a content script to grab the content from the DOM. Pass it to your extensions background script using postMessage message passing.
Make the POST request from your background script.
You will need to update your manifest.json to have permissions to the URLs you want to inject the content into, and permissions to make the POST request to your server's domain. Users will have to see and approve these permissions, but they will not know what you are doing, so be sure to tell them.

Run cgi script without changing the current html page?

I have a cgi script written in C, which is actually a client program.
What I need to do is when the up arrow key is pressed, I need the cgi program to execute and send data to a C server program residing in my webserver?
How can I exectute cgi scripts without the page changing ?
my C server program is listening on port 5000,
my html page is in http server at port 80. On loading, when the user presses the up arrow key,
the cgi script must run and send a value 1 to the server program. Also my cgi script is a client program.
I have already written the Javascript code to take keyboard inputs and displays particular values.
Running a CGI script is just a matter of making an HTTP request. There are numerous ways to do this without leaving the page.
Some of the more common ones are:
Load the URL in an (i)frame
Have the script return an image and dynamically generate an <img> element
Use XMLHttpRequest to make the request
Have the script return a piece of JS and dynamically generate an <script> element
Most of these come under the general heading of Ajax (and XHR being the most well associated method). There are plenty of libraries that help with this, and most big libraries (such as YUI and jQuery) include Ajax helper methods.
You want to communicate with the server without having to reload the page? Use AJAX (javascript framework).

Categories

Resources