How to get time counter from this web link to C# from - javascript

http://www.tumangobajito.com/auctions/Blackberry-9360-Blanco-484
I have used WebBrowser control to navigate this link, but there is javascript error, so I cannot see the timer working.
Is there anyway for me to get this timer to a textbox in C# form ?

The timer is being compared against an internal end-time field. You could attempt to reverse engineer the scripts and see if that data is something you have access to (there is 8 Javascript files active so I'll let you take a look).
Another method would be to use C# to download the page. Then parse the data to look only for what is needed.
<div id="timer_484" class="timer countdown" title="1341363012">
CAPTURE THE DATA HERE
</div>
If you're needing the times from different pages, you'll have to modify your parser to fit accordingly. Once you have that data though, you could write your own countdown timer (if that's the route you're taking with this).

Related

How to preserve userscript modifications in Chrome after an asynchronous content update on an .aspx webpage

I'm trying to automate the workflow of a webpage for my company's inventory system. The page is generated by the server-side logic of an ASP.Net page, which I don't have access to. The page has several fields on it to allow you to enter a new container barcode, the item that should go in the container, etc. Each of these fields has an onchange event listener hooked up to it which calls the page's __doPostBack() function to verify the entered data. If the data is verified, the page code is re-served with the data entered so far, and focus is set to the next field on the form.
I want to automate this page with a userscript in Chrome. I started by using ViolentMonkey to inject a custom script, but I could only get the script to trigger on the initial load, not after each data entry. After this, I tried using Chrome Local Overrides to change __doPostBack() to try to capture the data I need to automate the page. That also only works once; after a field is filled and loses focus and new HTML is served, it overwrites Chrome's local copy.
I think that my problems are being caused by an asynchronous refresh of the entire page contents, which wipes out the injected userscript and Chrome's Local Override without triggering the normal page refresh listeners in Chrome Overrides or ViolentMonkey to re-inject the modified code. Does anyone have any thoughts on how I could modify the JavaScript in such a way that it would persist after the page content is replaced with new HTML?
P.S. I don't think the code itself is relevant to this particular problem, but if anyone thinks it would be helpful to share a limited section of the client-side code, let me know.
Edit 1: Here's a more in-depth view of what I'm trying to accomplish, and the progress I've made so far. For reference, the form looks like this:
My Original Plan
The user loads the page. ViolentMonkey injects a userscript which issues a series of prompts, collecting data on the range of new barcodes that the user would like entered into the system. (Specifically, the barcode prefix, the starting barcode number, and the ending barcode number.) This values are stored in localStorage.
After this data has been collected and validated by the user, the page loads normally. For reference, the form looks something like this:
The user fills out the fields as normal. After each field is filled out (with the exception of the Container Description field), the page pushes focus to the next field. (For example: <script language="javascript"> try { document.getElementById('txtContDesc').focus() } catch (e) { } </script>. The id of the field to focus is dynamically changed via the server logic.)
I need to collect the User Badge, Container Type, and Destination Barcode values so that I can refill them later when I automate the form. My original plan was to add a onfocus event listener to the Container Description field, since focus will be shifted to it once the Destination Barcode field has been verified. I will know at this point that the user has successfully entered a valid entry for each of the fields above the Container Description field, and I would then be able to collect these values and store them in localStorage.
Once I have all the data needed for the form, I would pilot the form using the userscript in ViolentMonkey and the data stored in localStorage, to persist data across page refreshes.
Other Alternatives:
The eventListener idea on an element doesn't work, because ASP.NET updates the page with fresh code every time a field is verified, wiping out the listener. It also doesn't trigger a refresh, so ViolentMonkey doesn't rerun my userscript.
My other thought was to modify doPostBack(). The doPostBack() function looks like this (as far as I can tell):
<script type="text/javascript">
var theForm = document.forms['formNewContainer'];
if (!theForm) {
theForm = document.formNewContainer;
}
function __doPostBack(eventTarget, eventArgument) {
console.log("Form submitted");
}
</script>
It is called on verified fields with the following onchange handler:
onchange="javascript:setTimeout('__doPostBack(\'ctl00$newContPage$txtBarcode\',\'\')', 0)"
My goal would be to modify doPostBack() to save the information I need to localStorage before executing the rest of doPostBack() without changing it.
(Note: doPostBack() here looks incredibly simplistic, so I think I'm missing some information about how ASP.NET works here. This is outside of the question though, unless it's relevant for what I'm trying to do.)
I was able to successfully modify doPostBack() in this way using Chrome Local Overrides to serve myself a local copy of the page on page load, instead of the server version. But this only works for the first doPostBack() request. After the first request, the server serves me new code. Like with ViolentMonkey, the lack of a refresh trigger prevents Chrome Local Overrides from re-serving my local copy, and I'm served code without the doPostBack() modification.
So that's where I'm at. I'll try adding a global listener like #wOxxOm suggested, and see where that gets me.
I ended up using a Chrome extension called "Run Javascript" (has an elephant for it's logo), which runs the JavaScript code even on AJAX requests.
Link: https://chrome.google.com/webstore/detail/run-javascript/lmilalhkkdhfieeienjbiicclobibjao/
I don't see how this is possible at all. You need to work with the people that created that web page.
Asp.net and the server side code will be EXTENSIVE .net code (c# or vb.net). Each of those events you trigger will set variables and server side session (or viewstate) values for the code behind to run.
That's how asp.net pages work. You post back, page travels up to server, THEN the .net code behind runs. That code will modify the page, modify controls, and modify the view state for that page. And after that code runs (say on a button click), then you client side will receive a whole new fresh page - that will blow out any JavaScript you try and inject. (you would have to re-inject each time). But, it gets worse, since quite of bit of that code behind also checks and often will NOT tolerate that the page settings have been messed with, and will be rejected.
About the only way to do this would be to write some desktop software, and that software would "house" or "host" a full "com" object copy of the web page, and you thus automate that given page. (and even then, you still fighting a losing battle).
Hint:
Web development, business logic, and a functional business applcation is NOT some simple markup and JavaScript (despite what that lame 2 week HTML course tells you).
This is a application, and asp.net applcation. Trying to think of this as just some markup and JavaScript is actually quite silly here. It not how you write, or build business solutions for a company.
If you can't write and modify the code and the web server side of things then find out if that site has some kind of web api or whatever.
But, really - this is silly, and unless this is some simple college project, or some hacked up html page and some JavaScript? Forget this approach - you dealing with FAR too much server side and code behind on the server.
In fact, asp.net as noted has quite a bit built in features that check if the page being posted back been messed with, and you never really be sure that you set values and that the proper amounts of code behind that runs to setup row values, database primary key values and a WHOLE boatload of state values that are probably 100% saved in server side session() based class objects - and objects that are never exposed server side.
Tring to supposed modify or assume you can create or modify such a system with only client side tools is not going to work - its just not.
code behind runs, it re-processes the page with .net code and then sends the whole page back down - all with new state values etc. This is not some lame html + JavaScript, but is a full server side code driven system written in c# .net code.

How do I parse a number out of a web page that is being generated by Javascript, not HTML?

I've just ordered a ControlByWeb temperature sensor. It has a built in web server. I had planned to have my home automation system poll this device periodically to use its temperature readings for various HA tasks. I'm using Python (2.7.x on Mac Mojave). I know how to get the html code of the device's web server. And I was planning to figure out how to parse the code to extract the actual temperature numbers. Simple enough. But the numbers are not actually there in the HTML! The manufacturer provides a sample page of a sample device, and while the temperatures show up while viewing the page, the numbers aren't in the page source. They're being generated (and refreshed) in real time by the page's javascript! How do I extract the three temperature numbers?!? Here's the page: http://107.1.170.22:9036.
You could use a library like Selenium or Puppeteer which will actively execute JavaScript exactly as an end-user’s browser would. A better route might be to reverse-engineer the API calls the app itself is making and hook in that way.
In this specific example, it looks like the page you've linked is calling http://107.1.170.22:9036/state.xml?time=1629304236823. All you'd need to whenever you need an up-to-date reading is to build the URL with the current timestamp (which appears to only be a cache-busting mechanism anyway), do a GET request to the resulting URL, then parse the resulting XML to meet the requirements of your end product.
This seems to be the ticket, unless someone has a more elegant/bullet-proof way...
import re
import requests
import time
try:
vTimeInSeconds = unicode(int(time.time()))
vUrl = 'http://107.1.170.22:9036/state.xml?time=' + vTimeInSeconds
vResult = requests.get(vUrl)
vState = vResult.text
vTempOutside = re.search('<sensor1>(.+?)</sensor1>', vState).group(1)
vTempInside = re.search('<sensor2>(.+?)</sensor2>', vState).group(1)
vTempMaster = re.search('<sensor3>(.+?)</sensor3>', vState).group(1)
except:
# my error handler...
The temperature value does exist in the HTML:
Screenshot
However, it sends a request for this value to the server after the page loads. You can check this in the dev console, the value is empty for a small period of time after page load. You'll need to use Selenium to read the page, you can load the page and introduce a delay before you read the element. You could use Selenium's built in Waits or you could simply use time.sleep(x) to wait x number of seconds before reading the html.

Display result (image) of computation in website

I have a python script that generates a heightmap depending on parameters, that will be given in HTML forms. How do I display the resulting image on a website? I suppose that the form submit button will hit an endpoint with the given parameters and the script that computes the heightmap runs then, but how do I get the resulting image and display it in the website? Also, the computation takes a few seconds, so I suppose I need some type of task queue to not make the server hang in the meanwhile. Tell me if I'm wrong.
It's a bit of a general question because I myself don't know the specifics of what I need to use to accomplish this. I'm using Flask in the backend but it's a framework-agnostic question.
Save the image to a file. Return a webpage that contains an <IMG SRC=...> element. The SRC should be a URL pointing at the file.
For example, suppose you save the image to a file called "temp2.png" in a subdirectory called "scratch" under your document root. Then the IMG element would be <IMG SRC="/scratch/temp2.png"> .
If you create and save the image in the same program that generates the webpage that refers to it, your server won't return the page until the image has been saved. If that only takes a few seconds, the server is unlikely to hang. Many applications would take that long to calculate a result, so the people who coded the server would make sure it can handle such delays. I've done this under Apache, Tomcat, and GoServe (an OS/2 server), and never had a problem.
This method does have the disadvantage that you'll need to arrange for each temporary file to be deleted after an expiry period such as 12 hours or whenever you think the user won't need it any more. On the webpage you return, if the image is something serious that the user might want to keep, you could warn them that this will happen. They can always download it.
To delete the old files, write a script that checks when they were last updated, compares that with the current date and time, and deletes those files that are older than your expiry period.
You'll need a way to automatically run it repeatedly. On Unix systems, if you have shell access, the "cron" command is one way to do this. Googling "cron job to delete files older than 1 hour on web server" finds a lot of discussion of methods.
Be very careful when coding any automatic-deletion script, and test it thoroughly to make sure it deletes the right files! If you make your expiry period a variable, you can set it to e.g. 1 minute or 5 minutes when testing, so that you don't need to wait for ages.
There are ways to stream your image back without saving it to a file, but what I'm recommending is (apart possibly from the file deleter) easy to code and debug. I've used it in many different projects.

How do I prevent website user from seeing certain information until a certain condition is met.

So this is what I would like my website to do:
The website will generate a random number between 1 and 10,000.
The website user will then guess the number that the website generated by imputing their guess into a text-box.
If the user gets the number wrong a new number is generated and the user has to guess again.
If the user gets the number right a code-number is revealed.
My problem is that if the user simply inspects the java script they will be able to see the code-number within the script. How do i prevent the website user from manipulating the javascript code to know which number the website generated? also, how do I prevent the user from accessing the code by looking at the javascript? Do i need to use php or something?
Thanks for taking the time to read my question.
first of all you don't have to generate the number until they actually guess it.
function guess(number){
var rand=Math.random()*10000;
if(number==rand){
//success
}else{
//failure
}
}
This way no number is generated until they guess and you can't inspect that.
Also thinking outside the box: Based on the conditions, the number from the guess is irrelevant. If the number changes every time, they have a 1/10,000 chance every time to guess correctly. You simply have to write a function that only succeeds once every ten thousand times. However if they are looking at the source, they might feel betrayed by this.
If you put all your game logic on the client side, the client will be able to hack your game. All major web browsers come with a debugger which could easily manipulate and navigate your client side code in a variety of different ways.
That is why the server side should be responsible for these kinds of checks and validation. If the client had to submit the number to your server, the answer could be generated on the server side where it would be safe from any tampering.

How to get the number of seconds a page loads after all data are shown on the page?

Is it possible to get the TOTAL NUMBER OF SECONDS a page fetch and display the data?
like from the moment I click a link to the moment all data are displayed on the page, done on OnInit, onrender, pageload onprerender and so on..... is it possible?
thanks
yes its possible just you need to add the code in your web.config file and run the application it will prompt you the loading time just after page rendering. Scroll the mouse and get the details.
<system.web>
<trace pageOutput="true" requestLimit="10" enabled="true" localOnly="true" traceMode="SortByTime" mostRecent="true"/>
</system.web>
Note: you need to write the trace part of the code in system.web which already exists in your web.config file.
You can easily check the time PHP needs to run the script: start to finish.
Simply store the time at start and end. Look here:
http://nl.php.net/manual/en/function.microtime.php
However, this is not the time the user experiences.
For example: If PHP needs 0.1 sec to produce the HTML, and the HTML contains 100 big images, the actual pageloading takes a lot longer than 0.1 sec.
The actual time the enduser experiences depends on a lot of factors, like webserver that is inbetween (and that need to invoke PHP), networkspeed, caching, etc..
I think your best bet is to approach this via Javascript, and use the onLoad event handler that can be attached to body.
Use some external window to do the timing between clicking and the firing of onload.
ALso, keep in mind that result might differ for other visitors with different cache-settings, different networkspeed, etc.. SO you can only get an approximation.
It's possible, but kinda complicated, because the load time from click to full load consists of so many things:
request to the server (connection roundtrip, dns lookup sometimes etc)
request processing server side (this you can measure insude your APS code)
request load till any of the events fire
etc
Long story short is would be impossible to measure it with any single method and combining many would be a pain and would not include all the parts to be measured.
In this particular case the best thing you could do is: bind onclick (on link) an ajax request with current timestamp (millisecond precision) and do a 2nd request, with current timestamp onload and substract the two.
Send a variable from server having current time in it before displaying page.
On HTML page, run a javascript function on onload(). This function is called after page is loaded. Get the current time again in this function.
Match the both time variables. One sent from server and one in onload() function. You will get the number of seconds.

Categories

Resources