Java Scripting Help
 

  You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more

[Closed] Java Scripting Help

18 Posts
5 Users
0 Reactions
55 Views
Posts: 0
Free Member
Topic starter
 

Hi All,

I'm no java programmer but have been asked to take a look at something for someone.

I'm trying to figure out how to return a piece of text that is in the source code of a web page as a variable to then be passed in to another section of the page to return a result. The latter section is all done and the results are returning fine if I manually enter the text but need it to come in as a variable.

I want to return the IP address and it is in the source code with the format 'IP: 192.168.0.1', I only want the IP though, not the preceding 'IP:' label.

Any help greatly received!


 
Posted : 08/03/2016 3:28 pm
Posts: 0
Free Member
 

It’s difficult to tell exactly what you are asking for at the moment. In the title you mention “java scripting” and then in the text you say “java”. Trouble is; they are entirely different things (and a complete arse they have similar names)

Is this script something that runs on the webserver or in the browser on the visitors computer?

Rachel


 
Posted : 08/03/2016 3:34 pm
Posts: 0
Free Member
Topic starter
 

Yeah, apologies I wasn't clearer.

It's run in the browser as part of a web page.

Essentially, the user will navigate to web page and the page already reads their IP and puts it in to the source code of the html, I just need to read that value back out as a variable to pass in to some other bits.


 
Posted : 08/03/2016 3:36 pm
Posts: 0
Free Member
 

okay - so it’s javascript, then 🙂

Can you point us at the page? I can take a look for you


 
Posted : 08/03/2016 3:41 pm
Posts: 0
Free Member
 

Sometimes, a library like jQuery (jquery.com) can make it easier to write javascript code that is readable.

If your webpage included something like

<div id=“ip-address”>192.168.1.1</div>

then you could have some script like

$( document ).ready(function() {
var my_ip = $(‘#ip-address’).text();
alert(my_ip);
});

or not alert() but do something useful!

(yes this could be done directly in js - I just wanted to make it readable)

Rachel


 
Posted : 08/03/2016 3:45 pm
Posts: 0
Free Member
Topic starter
 

I can't show you the page I'm afraid and after removing customer info it wouldn't make a lot of sense.

There are no div markings around the IP either, it's sitting hidden in the source code along with some other variables that we record at the same time.


 
Posted : 08/03/2016 3:48 pm
Posts: 0
Free Member
 

okay - can you post a section of the “code” that shows an example IP address? Otherwise it’s a bit difficult to guess!

Rachel


 
Posted : 08/03/2016 3:52 pm
Posts: 0
Free Member
 

and if it looks like this, we’re in business… 🙂

[code]
jQuery.extend(Drupal.settings, { "basePath": "/", "user_relationships_ui": { "loadingimage": "/profiles/drupal_commons/modules/contrib/user_relationships/user_relationships_ui/images/loadingAnimation.gif", "savingimage": "/profiles/drupal_commons/modules/contrib/user_relationships/user_relationships_ui/images/savingimage.gif", "position": { "position": "absolute", "left": "0", "top": "0" } }, "colorbox": { "transition": "elastic", "speed": 350, "opacity": "0.85", "slideshow": false, "slideshowAuto": false, "slideshowSpeed": 2500, "slideshowStart": "", "slideshowStop": "", "current": "{current} of {total}", "previous": "« Prev", "next": "Next »", "close": "Close", "overlayClose": true, "maxWidth": "100%", "maxHeight": "100%" }, "uc_ajax_cart": { "CALLBACK": "/uc_ajax_cart/add/item", "SHOW_CALLBACK": "/uc_ajax_cart/show", "CART_LINK_CALLBACK": "/uc_ajax_cart/addlink", "BLOCK_UI": 1, "TIMEOUT": 2000, "UPDATE_CALLBACK": "/uc_ajax_cart/update", "UPDATE": 0, "CART_VIEW_ON": 0, "SHOW_VIEW_CALLBACK": "/uc_ajax_cart/show-cart-view", "TRACK_CLOSED_STATE": 0, "INITIAL_CLOSED_STATE": 0, "CART_PANE_EFFECT": 0, "CART_PANE_EFFECT_DURATION": 200, "HIDE_CART_OPERATIONS": 0, "COLLAPSIBLE_CART": 0, "ADD_TITLE": "Please wait", "ADD_MESSAGES": [ "Adding product to cart..." ], "REMOVE_TITLE": "Please wait", "REMOVE_MESSAGES": [ "Removing product from cart..." ], "UPDATE_TITLE": "Please wait", "UPDATE_MESSAGES": [ "Updating cart..." ], "CART_OPERATION": "Cart" }, "googleanalytics": { "trackOutbound": 1, "trackMailto": 1, "trackDownload": 1, "trackDownloadExtensions": "7z|aac|arc|arj|asf|asx|avi|bin|csv|doc(x|m)?|dot(x|m)?|exe|flv|gif|gz|gzip|hqx|jar|jpe
[code]


 
Posted : 08/03/2016 3:55 pm
Posts: 2086
Free Member
 

Assuming you have access to the string in question:

function parseIPAddress(str) {
var results, ip
var regex = /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/
if (results = regex.exec(str)) ip = results[0]
return ip
}

var str = 'IP: 192.168.0.1'

console.dir(parseIPAddress(str))

the function will either return a string or undefined.


 
Posted : 08/03/2016 4:02 pm
Posts: 0
Free Member
Topic starter
 

Lol, there isn't any code around it either, the IP is inserted by another piece that is server side before the page is returned to the user.

It is quite literally just a list of variables at the bottom of the page one of which is IP: 192.168.0.1

Looks a bit like this:
<!--
ID: 172.24.1.2
Username:
Description:
UserGroups: []
UserGroup ids: []
Until: Tue Mar 22 11:14:51 2016
Max score: 400
Max size: 5000
IP: 172.24.1.2
Port: 3128
Computer:
Flag ids: [201, 203, 250, 251, 254, 255, 260, 261, 266, 267, 401, 425, 2100, 2101, 1000030]
Decision ids: [408, 1000, 1202, 1205, 1000030]
Download: 0
Notify:
language: en_GB.utf8
IP address: -1407713022
Server address: 172.21.138.120
-->


 
Posted : 08/03/2016 4:03 pm
Posts: 2086
Free Member
 

If you can't get access to the node in question, then you could try parsing the whole document body - can't imagine there'll be many IP addresses in there:

parseIPAddress(document.body.innerHTML)


 
Posted : 08/03/2016 4:04 pm
Posts: 2086
Free Member
 

Ah, ok, you DO have multiple IP addresses in there ... try this:

;(function () {

'use strict'

function getIPAddress(str) {
var results, ip
var regex = /\bIP:\s?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/
if (results = regex.exec(str)) ip = results[1]
return ip
}

var ip = getIPAddress(document.body.innerHTML)

alert(ip)

})()


 
Posted : 08/03/2016 4:10 pm
Posts: 0
Free Member
Topic starter
 

Using that code I get an alert but it just says 'undefined' so I'm presuming it's not running the code before it properly to determine the IP.


 
Posted : 08/03/2016 4:16 pm
Posts: 0
Free Member
 

somouk - is this some sort of weird squid cache thing?? It’s really odd what you’re trying to do. I have a feeling it will end up a squiggly mess before long…

Rachel


 
Posted : 08/03/2016 4:17 pm
Posts: 0
Free Member
Topic starter
 

It is indeed Squid related Rachel, no problems if it's squiggly at all!


 
Posted : 08/03/2016 4:20 pm
Posts: 2086
Free Member
 

Try this, or put the original code at the bottom of the page if you can:

<script>
;(function () {

'use strict'

function getIPAddress(str) {
var results, ip
var regex = /\bIP:\s?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/
if (results = regex.exec(str)) ip = results[1]
return ip
}

document.addEventListener('DOMContentLoaded', function () {
var ip = getIPAddress(document.body.innerHTML)
alert(ip)
})

})()
</script>


 
Posted : 08/03/2016 4:49 pm
Posts: 3427
Full Member
 

Hang on a minute! Someone's come along requesting free work being done for them.

Shouldn't we be providing javascript that generates unicorns and rainbows?


 
Posted : 08/03/2016 5:05 pm
Posts: 0
Free Member
Topic starter
 

Lol, that would be pretty funny actually.

Thank you for those who have had an input, will give it a try in the morning with a fresh head!


 
Posted : 08/03/2016 5:17 pm
Posts: 0
Free Member
 

If you want free work done, that's a job for Stack Overflow. That's what all the Indian offshore guys do to get their work done 😉


 
Posted : 08/03/2016 9:04 pm

6 DAYS LEFT
We are currently at 95% of our target!