HTTP requisitions GET - get

When I go to the developer tools of the browser, and click a button on a site, I receve a get requisitions. If do this get with the link that appears in the developer tolls, its the same that i "click" on the button?
GET
My code:
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const http = new XMLHttpRequest();
const url = 'https://www.meuspremiosnick.com.br/vote/tiktoker-do-ano'
var j=0;
function votar()
{
http.open('GET',url,false);
http.send('https://sviakcanickglobal.112.2o7.net/b/ss/viakcanickglobal,vianickelodeonintl/1/JS-2.2.0/s5711041450902?AQB=1&ndh=1&pf=1&t=31/7/2020 14:33:26 1 180&mid=91423556843279868591240211340791917389&aamlh=4&ce=UTF-8&pageName=kca-br|events|voting&g=https://www.meuspremiosnick.com.br/vote/&c.=&v.=&activity=actioncall&appName=kca Ubuntu&brandID=kca-br&pageFranchise=kca2020&pageURL=https://www.meuspremiosnick.com.br&pageType=voting-page&actName=clicks on vote_TikToker do Ano_Doarda&destination=no destination&modname=voting_items&bentoVersion=2.0.0&.v=&.c=&aamb=6G1ynYcLPuiQxYZrsz_pkqfLG9yMXBpb2zX5dvJdYQJzPXImdj0y&pe=lnk_o&pev2=https://www.meuspremiosnick.com.br/vote/tiktoker-do-ano&s=1680x1050&c=24&j=1.6&v=N&k=Y&bw=807&bh=919&mcorgid=ED7001AC512D2ABD0A490D4C#AdobeOrg&AQE=1');
http.onreadystatechange=function(){
if(this.readyState==4 && this.status==200)
{
j=j+1;
console.log(http.responseText);
console.log('Votos: '+j);
}
}
}
setInterval(votar,200);

From what i have seen it depends on the site and usually it's not the case. For example this site i was automating today had a next page button. When that button is clicked next 100 data rows are loaded. On click a POST request is made which returns the data rows in response. Now when i made the same request in the console nothing changed but i did see the response data in console. The reason being on the front end when that next page button is clicked POST request is made by some function. That function then waits for the response and on successful response it loads the rows. Now when i make the request myself no function is waiting for the response so the data won't load.
In short the request is usually not what triggers the related functionality. Usually it's just a middle man to fetch data while the original triggered function waits.

Related

server response to webform: how to answer duplicates?

I'm running a small server that needs to receive webforms. The server checks the request and sends back "success" or "fail" which is then displayed on the form (client screen).
Now, checking the form may take a few seconds, so the user may be tempted to send the form again.
What is the corret way to ignore the second request?
So far I have come out with this solutions: If the form is duplicate of the previous one
Don't check and send some server error back (like 429, or 102, or some other one)
Close directly the connection req.destroy();res.destroy();
Ignore the request and exit from the requestListener function.
With solution 1 and 2 the form (on client's browser) displays a message error (even if the first request they sent was correct, so as the duplicates). So it's not a good one.
Solution 3 gives the desired outcome... but I'm not sure if it is the right way around it... basically not changing req and res instead of destroying them. Could this cause issues, or slow down the server? (like... do they stack up?). Of course the first request, once it has been checked, will be sent back with the outcome code. My concern is with the duplicate requests, which I don't destroy nor answer...
Some details on the setup: Nodejs application using the very default code by the http module.
const http = require("http");
const requestListener = function (req, res) {
var requestBody = '';
req.on('data', (data)=>{
requestBody += data;
});
req.on('end', ()=>{
if (isduplicate(requestBody))
return;
else
evalRequest(requestBody, res);
})
}

HTTP GET Request, Response

I have a file where I send GET request to another file, and I got the response show up under Network tab of Google Dev Tools, but it did not display on my browser.
This is what I do for passing the response to display in my browser.
xmlhttp.onreadystatechange = function() {
if(this.readyState == 4){
res = xmlhttp.responseText;
document.getElementById('table3').innerHTML = res;
}
}
And I want to display the response under the table of id = "table3" like below.
<td id="table3">
<td>
The content inside was passing from the response of GET request.
Any help is appreciated. Thank you
I think that your if statement in the onreadystatechange callback is wrong. xmlhttp is the instance of the XHR class, which would mean that instead of using this, you would have to use xmlhttp, and not this. this in the context of your program likely is the window object.

in nodeJs request, how can we get the html after an ajax loading?

doing
var page_url = "http://skiferie.danskbilferie.dk/sidste_chance_uge7_norge_sverige.html";
http.get(page_url, (http_res) => {
var data = "";
http_res.on("data", function (chunk) {
data += chunk;
});
http_res.on("end", function () {
resolve({data});
});
});
get's the correct HTML from that page, but how can I wait for the table of deals to be filled?
as that page only fills the data I need after it loads, calling an ajax method to fill the data in... is there a wait for me to wait for such action to be completed?
To wait until ajax will be loaded, you need to add timeout for your request.
Your goal is to somehow tell script to wait for some period of time and than get rendered html.
You can implement such behavior as #GabrielBleu said, with puppeteer and here is nice tutorial with example: tutorial
Or with webdriver or you can try with this resource

Node.js listen to session variable change and trigger server-sent event

I am writing a webapp, using express.js.
My webapp achieves the following
User posts 100 json objects
Each json object is processed via a service call
Once the service call is completed, a session variable is incremented
On incrementation of the session variable, a server side event must be sent to the client to update the progress bar
How do i achieve listening on a session variable change to trigger a server-sent event?
Listening to a variable change is not the only solution I seek?
I need to achieve sending a server-sent event once a JSON object is processed.
Any appropriate suggestion is welcome
Edit (based on Alberto Zaccagni's comment)
My code looks like this:
function processRecords(cmRecords,requestObject,responseObject)
{
for (var index = 0; index < cmRecords.length; index++)
{
post_options.body = cmRecords[index];
request.post(post_options,function(err,res,body)
{
if(requestObject.session.processedcount)
requestObject.session.processedcount = requestObject.session.processedcount + 1;
else
requestObject.session.processedcount = 1;
if(err)
{
appLog.error('Error Occured %j',err);
}
else
{
appLog.debug('CMResponse: %j',body);
}
var percentage = (requestObject.session.processedcount / requestObject.session.totalCount) * 100;
responseObject.set('Content-Type','text/event-stream');
responseObject.json({'event':'progress','data':percentage});
});
};
}
When the first record is updated and a server side event is triggered using the responseObject (express response object)
When the second record is updated and I try triggering a server side event using the same responseObject. I get an error saying cannot set header to a response that has already been sent
It's hard to know exactly what the situation is without seeing the routes/actions you have in your main application...
However, I believe the issue you are running into is that you are trying to send two sets of headers to the client (browser), which is not allowed. The reason this is not allowed is because the browser does not allow you to change the content type of a response after you have sent the initial response...as it uses that as an indicator of how to process the response you are sending it. You can't change either of these (or any other headers) after you have sent them to a client once (one request -> one response -> one set of headers back to the client). This prevents your server from appearing schizophrenic (by switching from a "200 Ok" response to a "400 Bad Request," for example).
In this case, on the initial request, you are telling the client "Hey, this was a valid request and here is my response (via the status of 200 which is either set elsewhere or being assumed by ExpressJS), and please keep the communication channel open so I can send you updates (by setting your content type to text/event-stream)".
As far as how to "fix" this, there are many options. When I've done this, I've used the pub/sub feature of redis to act as the "pipe" that connects everything up. So, the flow has been like this:
Some client sends a request to /your-event-stream-url
In this request, you set up your Redis subscriber. Anything that comes in on this subscription can be handled however you want. In your case, you want to "send some data down the pipe to the client in a JSON object with at least a data attribute." After you have set up this client, you just return a response of "200 Ok" and set the content type to "text/event-stream." Redis will take care of the rest.
Then, another request is made to another URL endpoint which accomplishes the task of "posting a JSON object" by hitting /your-endpoint-that-processes-json. (Note: obviously this request may be made by the same user/browser...but the application doesn't know/care about that)
In this action, you do the processing of their JSON data, increment your counters, or do whatever...and return a 200 response. However, one of the things you'd do in this action is "publish" a message on the Redis channel your subscribers from step #1 are listening to so the clients get the updates. Technically, this action does not need to return anything to the client, assuming the user will have some type of feedback based on the 200-status code or on the server-sent event that is sent down the pipe...
A tangible example I can give you is this gist, which is part of this article. Note that the article is a couple years old at this point so some of the code may have to be tweaked a bit. Also note this is not guaranteed to be anything more than an example (ie: it has not been "load tested" or anything like that). However, it may help you get started.
I came up with a solution please let me know if this is the right way to do stuff ?
Will this solution work across sessions ?
Server side Code
var events = require('events');
var progressEmitter = new events.EventEmitter();
exports.cleanseMatch = function(req, res)
{
console.log('cleanseMatch Inovked');
var progressTrigger = new events.EventEmitter;
var id = '';
var i = 1;
id = setInterval(function(){
req.session.percentage = (i/10)*100;
i++;
console.log('PCT is: ' + req.session.percentage);
progressEmitter.emit('progress',req.session.percentage)
if(i == 11) {
req.session.percentage = 100;
clearInterval(id);
res.json({'data':'test'});
}
},1000);
}
exports.progress = function(req,res)
{
console.log('progress Inovked');
// console.log('PCT is: ' + req.session.percentage);
res.writeHead(200, {'Content-Type': 'text/event-stream'});
progressEmitter.on('progress',function(percentage){
console.log('progress event fired for : ' + percentage);
res.write("event: progress\n");
res.write("data: "+percentage+"\n\n");
});
}
Client Side Code
var source = new EventSource('progress');
source.addEventListener('progress', function(e) {
var percentage = JSON.parse(e.data);
//update progress bar in client
App.updateProgressBar(percentage);
}, false);

Chrome Omnibox extension to post form data to a website?

How can an Omnibox extension create and post form data to a website and then display the result?
Here's an example of what I want to do. When you type lookup bieber into the Omnibox, I want my extension to post form data looking like
searchtype: all
searchterm: bieber
searchcount: 20
to the URL http://lookup.com/search
So that the browser will end up loading http://lookup.com/search with the results of the search.
This would be trivial if I could send the data in a GET, but lookup.com expects an HTTP POST. The only way I can think of is to inject a form into the current page and then submit it, but (a) that only works if there is a current page, and (b) it doesn't seem to work anyway (maybe permissions need to be set).
Before going off down that route, I figured that somebody else must at least have tried to do this before. Have you?
You could do this by using the omnibox api:
chrome.omnibox.onInputChanged.addListener(
function(text, suggest) {
doYourLogic...
});
Once you have you extension 'activated' due to a certain keyword you typed you can call something like this:
var q = the params you wish to pass
var url = "http://yourSite.com";
var req = new XMLHttpRequest();
req.open("POST", url, true);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.onreadystatechange = function() {
if (req.readyState == 4) {
callback(req.responseXML);
}
}
req.send(q);

Resources