go ahead... be a heretic | |
PerlMonks |
Re: Running JavaScript from within Perlby harangzsolt33 (Chaplain) |
on Sep 14, 2019 at 05:10 UTC ( [id://11106159]=note: print w/replies, xml ) | Need Help?? |
The JavaScript program on a web page can dynamically modify the page, so what you see has very little or no resemblance to the HTML source code! So, if you can scrape your web page using JavaScript, you get a peek at what's actually on the screen.
Here is an example. When you click on the "View HTML" button on this page, you'll see one thing. Then you click on the "Change" button which modifies the code, and then you click on View HTML again, and you'll see the code with some slight changes. The source code hasn't changed, but what's in the memory has changed, and when you get to harvest that, you get the real picture. Here is the JavaScript program that harvests the HTML code: var DATA = document.all[0].innerHTML; If the block of HTML code you're trying to harvest is marked with an ID tag like this: then you don't need to harvest the entire HTML page. All you have to do is harvest whatever is tagged. So, you would just do this: var DATA = document.getElementById("Part3").innerHTML; Instead of using "innerHTML," you could also use "innerText" which gives you only the plain text without all the HTML tags and whatnot: var DATA = document.getElementById("Part3").innerText; Once you have the code in the DATA variable, then you can run a regex or something to get the actual number you're looking for.. JavaScript regex works like perl's.
I tested the above code, and it works in Firefox 52, KMeleon 7.5, QupZilla 1.8.6, Safari 5.1.7, Google Chrome 75, Internet Explorer 6, Opera 7.5, and Vivaldi 1.0. I have also tested it with an iPhone 7, Nokia Lumia 930 Windows Phone and an old Android 6 tablet. I haven't used any "ultra modern technology" that will break your phones. Everything in this example script is pretty standard. Once you get the number you want to send back to your perl script, you could send it back by loading a picture:
Here you're sending the number 90 back to your perl script. You could also signal to your perl script when somebody loads your web page with JavaScript turned off by putting a picture within the NOSCRIPT tags. Whatever you put between the NOSCRIPT tags will only appear when JavaScript is disabled on the page:
In Section
Seekers of Perl Wisdom
|
|