Javascript save variable outside of program - javascript

I am writing a simple iterator script in a third party program:
var i = 1.
function run(){
output1 = i;
i = i + 1;
}
This function is run for every record. But the program works with batches of 25 records. So for 100 records it outputs 4 times 1 to 25. I have access to the java libs. It is a linux server. Can I put i in some kind of global java variable or memory, so i can live outside of my program? I tried reading and writing i to a text file. This works but is to slow giving me some empty's, ones and two's. Making the program wait would be my last option (how can I do that?
I have no other easy way to add these numbers. I am limited by the program Oracle EDQ options.

Unfortunately you can't your Java memory resides in the memory of your server whereas your javascript variables are part of the memory of user's browser i.e. user's system. So you can't have share memory but you definitely can establish link between your server and client through http.
So if you need to save some javascript variables on your server, you can initiate an ajax request from client which will pass the data to server. Once you get it there you can do whatever you like to do with it

Related

How can I make a webpage that checks my server for a folder and counts the number of folders under that

I want to use a html webpage to look on it's source server to a specific subfolder (/home/pi/Pictures) and then count the number of subfolders there and print that out on the page when viewed remotely on the LAN. The source server is a Raspberry Pi, the software has .net6, and is mostly written in c#, with some js, css.
I've found a lot of old (and conflicting) answers to parts of the whole, so what do i need to do this in June 2022, on current browsers?
This seems like two topics in one, one of them JS and the other C#, since your files are on a server and your server code is in C#.   The actual counting of directories in C# is simple, just using syntax like string[] folders = Directory.GetDirectories("path"); and then return folders.Length;.   Of course you want to make sure that the path can't be provided by the user, since that's a security risk.
Then you need code for an API call at the C# end, like public async Task<IActionResult<int>> GetCountOfFolders() { ... } and it needs to return the number.
At the JavaScript end, you'd probably write let result = await fetch("/your/url/here) and then something like let count = await result.JSON();.
And finally, assuming you're just displaying this in a web page, you might do something like let display = document.getElementById("your-DOM-element-ID"); and finally display.innerText = count; or some other value setting.
I'm assuming here that Raspberry Pi can be accessed with the Windows-type Directory class from System.IO, and some other things.   I'm also omitting a lot of detail about where you'd put that C# API call (in a controller, but that's just the beginning) and other things.   And of course, if you want to load a whole page that gets the number, you wouldn't use JavaScript at all, you'd just have that API call return an HTML page with the number already inlined into it, and you'd use a browser just to request that page from your API URL -- at which point it's not really an "API" anymore.

How can I exchange data between nodejs and puppeteer?

I made a nodejs application that starts from index.js.
Then, index.js launches puppeteer and injects bot.js on a headless-api page by addscripttag function.
I made index.js sets a cookie for conveying initial values before injecting javascript, but I need more common way to exchange data.
I thought two ways; the first is using cookie, and the second is networking via socket connection.
Is there other way for send and receive data between index.js(node) and puppeteer(headless chrome)?
First, puppeteer IS nodejs side application, so they have a single environment and you don't need to "send" anything. Just pass data around as you'd do in any other JS code. I assume you want to transfer data between page and nodejs then.
To pass data from nodejs to page use page.evaluate. You can call any code in page context, ranging from simply setting some variables to directly calling whatever functions with necessary arguments.
To initiate transfer from page side to nodejs, first register a nodejs-side callback function with page.exposeFunction and then call it from page code and it will be executed in nodejs context. Just like in previous case, everything else depends on code of that function. It can be as simple as storing whatever argument you pass to it in some variable or directly perform with data pretty much whatever you want.

php: two client to share server user defined variable

GOAL: create kitchen and front desk can notify each other.
Q: how do i create php variable that can be read and set from kitchen and front desk. Let say the variable is vStatus. so the plan is when the front desk create order then vStatus = 'kitchen1', and when kitchen notify order is ready then vStatus='frontdesk1'.
on each other i create timer function to monitor the vstatus:
on front desk when vstatus='frontdesk1' then page is refreshed, and set vstatus=''. so the timer function on front desk side cycle continue until vstatus=frondesk1.
on kitchen when vstatus='kitchen1' the kitchen page is refreshed and set vstatus='' and timer function on kitchen side cycle until vstatus='kitchen1'
the hardware setup is like this:
front desk :apache web server, web browser
kitchen : web browser
You cannot share a PHP variable between separate requests - that would be a terrible thing from security point of view.
What you are looking for is a storage - some place outside of the php process that would persist your value even when the request processing (php execution) ends.
There are numerous options available for that, but here's a few most common and easiest to setup/implement:
Files - e.g. at the beginning of the execution read the value from file and when the value changes, write the new value to the file overwriting the old value
Key-Value store (e.g. Memcached or Redis) - You would need to install the memcached/redis server that would run as independent system process. Then you would connect to it and read the value and write the value when it changes. (Note these can be configured to be persistent or non-persistent storages, see next point or explanation)
APC cache - it's a special case of key-value store which works as a PHP extension rather than external service. The idea is still reading/writing the value stored in it. Note that this is not a persistent storage, meaning that when apache or php-fpm process is restarted the cache will be cleared and the value lost.
Database (e.g. MySQL or MongoDB). Again, you would have to install the database server and do some reading on how to use them.
There are certainly more options, but these are something to start with.
You need to use SQL to store that variable on server and AJAX requests to if you want to check the value in any set interval

How to save a Javascript variable as serverside variable so everyone can see it

Hey I have been looking around for like 2 hours now and cant really find what I'm looking for. I want to turn a JavaScript variable into a server-side variable so that when it is changed it is changed for everyone visiting the site. I have tried looking at Node JS, XML, PHP and SQL but i have absolutely no clue which one is the one I need to do this. If any of you guys could give me something to research in order to accomplish this I would be grateful.
My code is a voting function that just increments a variable by 1 and I want it to stay as that variable when refreshed and visited by others.
JS:
/*global*/ buttonText = "Global Clicks: 0";
/*global*/ amountOfvotes = 0;
function addVote() {
var newButtonText = buttonText.replace("0", amountOfvotes++);
document.getElementById('global-respects').innerHTML = newButtonText;
}
Thanks!
There are a lot of solutions that you can use for your problem. The first two things which are in my mind are a (1) classic client-server communication, and (2) the usage of web sockets. Well, I am not so firm in the usage of web sockets, so I describe the "easier" client-server communication.
An information before I start: The tools and programming languages are mostly independent. However, you can use JavaScript on the client and PHP on the server side for example.
The first, you need, is a (e.g. JavaScript) variable on your client. You had called it amountOfVotes (cf. the picture at the end). There are two things happen during the client is active: (1) if the client add a vote it has to send it to the server; (2) all clients have to stay up-to-date.
If the Add Vote button is clicked, then you send a message (e.g. via Ajax) to your server application. Then, the server takes its current amountOfVotes, adds one vote, and stores it (e.g., in a file or mysql database).
Your clients need an intervalled function. This function sends (e.g., via Ajax) a message to the server to get the current amountOfVotes. The server's responds with the current number and the client is up-to-date.

Understanding the data flow when fetching chart data using AJAX

I am trying to create a test automation results dashboard using a JS library called Chart.JS. I would like to display a bar graph over time showing the total number of tests passed & failed.
To do this, I have done the following things:
Created a dash_proj.html file in which I include Chart.js and in this file I will actually be drawing the graph onto the canvas.
Created a .php file in which I use a PDO connection to query a local database copy on my machine (testing locally for now through localhost).
In that same .php file, I display, in text, the results onto the browser to ensure I have grabbed the appropriate data.
Now, I am getting confused as to the proper flow of things from here. From what I have read, the next step should be to use JavaScript to call an AJAX function and tell it which PHP file name to look at (the one running the MySQL query), and that will return the data in real time (no screen refresh). Within the HTML file, I should wait for the JavaScript to return that info and the final step will be the actually drawing of the graph.
Where does jQuery come into play? And can I just put my AJAX calls inside my php file which makes the query to the database?
I was thinking of testing to make sure that AJAX call is working by first inserting dummy data into the database and checking to see if the results appear in real time on my PHP file through localhost. I was thinking that the next step would be to store all of the data from my $query->fetch() into two different arrays (one for tests passed and one for tests failed), then somehow access that array from my HTML file which calls Chart.JS and stick that data into the draw bar graph function?
You don't have to use jQuery. This JavaScript library contains a number of functions to simplify making AJAX calls and accessing the DOM, though some would argue that the convergence of browser APIs make it less necessary these days. Nevertheless, it remains popular.
Your first task is probably to fire off an AJAX operation upon page load. You can start off by adding this JavaScript directly to the page, though you'll probably want to add it as a minified asset once you have your logic working.
function ajax() {
// #todo Add your ajax logic in here
}
// Load the AJAX data into the chart as soon as the DOM is ready
$(document).on('ready', function() {
ajax();
});
It is common to do read operations using a get operation returning JSON, for which getJSON would work fine. Add this logic in place of the #todo comment above.
After that, you'll probably want to do a periodic refresh of your data, say every 60 seconds. You can do this thus:
setInterval(60 * 1000, ajax);
Note the interval timer works on milliseconds, hence the need to multiply by 1000.
One downside of the above is that if you expect a large number of users, or wish to reduce the interval to a very small value, your web server will be processing a lot of redundant requests (since most calls will result in no screen change). Using AJAX here is therefore not very scalable.
A better approach is to configure the server to push updates to browsers using Web Sockets. However, this requires a separate kind of web server, and so I probably would not recommend it for you just yet.

Categories

Resources