Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to read the contents of a cell?

    I need to make a javascript that when I am at a page within a game I play and click the javascript, it will get the value of a table cell, separate them to x and y variables and then put the values into a link that opens. The open link is of a php script that uses $_GET for the values of the variables to query my database.

    I am terrible at javascript and have been trying to do this for 2 days now with no success. So I hope I can find help here.

    Here's the scripts so it is easier to understand:
    This is part of the html page that is from the game. I need to have the javascript get 432|608 and assign x=432 and y=608 in this case, although those number change, the place in the table that that they are displayed do not change.

    <td>Coordinates:</td>
    <td><a href="game.php?village=19392&amp;s=map&amp;x=432&amp;y=608">432|608</a></td>


    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Language" content="de">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Page-Enter" content="revealTrans(Duration=0.1,Transition=4)">
    <link rel="stylesheet" type="text/css" href="kingsage.css">
    <link rel="shortcut icon" href="favicon.ico" type="image/ico">
    <script src="js/kingsage.js?hash=1e4c479f8ba66202f3b1bfb74414eef4"
    type="text/javascript"></script>
    <script
    src="js/mootools-1.2-core-nc.js?hash=1e4c479f8ba66202f3b1bfb74414eef4"
    type="text/javascript"></script>
    <script
    src="js/mootools-1.2-more.js?hash=1e4c479f8ba66202f3b1bfb74414eef4"
    type="text/javascript"></script>
    <script src="js/map_src.js?hash=1e4c479f8ba66202f3b1bfb74414eef4"
    type="text/javascript"></script>
    </head>
    <body>
    <script type="text/javascript">
    //<![CDATA[

    lang = new Array();
    lang['DAY'] = 'Day';
    lang['DAYS'] = 'Days';

    //]]>
    </script>
    <div
    style="background: rgb(0, 0, 0) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"
    align="center">
    <table style="border-collapse: collapse; height: 100%;" width="100%"
    cellpadding="0" cellspacing="0">
    <tbody>
    <tr valign="top">
    <td width="50%" align="right">
    <br>
    </td>
    <td style="width: 840px;">
    <table
    style="border-collapse: collapse; width: 840px; height: 100%;"
    cellpadding="0" cellspacing="0">
    <tbody>
    <tr>
    <td class="lay_content" valign="top"><a name="head2"></a>
    <div class="contentpane">
    <table class="borderlist" style="width: 420px;">
    <tbody>
    <tr>
    <th colspan="2">Abandoned settlement</th>
    </tr>
    <tr>
    <td>Coordinates:</td>
    <td><a href="game.php?village=19392&amp;s=map&amp;x=432&amp;y=608">432|608</a></td>
    </tr>
    <tr>
    <td>Points:</td>
    <td>390</td>
    </tr>
    <tr>
    <td>Player:</td>
    <td><br>
    </td>
    </tr>
    <tr>
    <td>Alliance:</td>
    <td><a href="game.php?village=19392&amp;s=info_ally&amp;id="></a><br>
    </td>
    </tr>
    </tbody>
    </table>
    <br>
    </div>
    </td>
    </tr>
    </tbody>
    </table>
    </td>
    <td width="50%" align="left">
    <br>
    </td>
    </tr>
    </tbody>
    </table>
    </div>
    <div id="settlement" style="display: none;"></div>
    <script type="text/javascript">
    //<![CDATA[
    startCounter();
    //]]>
    </script>
    </body>
    </html>


    Here's the php script that will process the x and y
    Code:
    <?php
    putenv("TZ=Europe/Berlin");
    session_start(); 
    include("includes/config.php");
    
    $username = $_GET['username'];
    $pass = $_GET['pass'];
    $x = $_GET['x'];
    $y = $_GET['y'];
    $now = time();
    
    //confirm user can make claim
    global $conn;
    $q = "SELECT password FROM users WHERE username = '$username' AND active = 1";
    $result = mysql_query($q,$conn);
    if(!$result || (mysql_numrows($result) < 1))
    {
    	$data = "Your identity can not be authenticated";
    }
    else
    {
    	$dbarray = mysql_fetch_array($result);
    	if($pass == $dbarray['password'])
    	{
    	      $continue = true;
    	}
    	else
    	{
    		$data = "Your password can not be authenticated";
    	}
    }
    
    //if can claim then check the village
    if($continue)
    {
    	$q = "SELECT * FROM `claims` WHERE x = '$x'  AND y = '$y' AND state = 1";
    	$village = mysql_query($q, $conn);
    	$result = mysql_fetch_assoc($village);
    	if(!$result)
    	{
    		$can_claim = true;
    	}
    	elseif($result)
    	{
    		if($result['username'] == $username)
    		{
    			$data = "You already have an active claim on ($x|$y)";
    		}
    		elseif($result['username'] <> $username)
    		{
    			$claimer = $result['username'];
    			$data = "$claimer already has an active claim on ($x|$y)";
    		}
    	}
    	if($can_claim == true)
    	{
    		global $conn;
    		$claim_made = time();
    		$claim_expires = $claim_made + 432000;
    		$state = 1;
    		$q = "INSERT INTO `claims` (x, y, username, claim_made, claim_expires, state) VALUES ('$x', '$y', '$username', '$claim_made', '$claim_expires', '$state')";		
    		$add_claim = mysql_query($q, $conn);
    		if($add_claim)
    		{
    			$data = "Success! You claimed ($x|$y)";
    		}
    		else
    		{
    			$data = "Error: Your claim on ($x|$y) did not complete.";
    		}
    	}
    echo  "<SCRIPT>alert(\"$data\");</SCRIPT>";
    }
    ?>
    The javascript should link to this php script and send the variables like this: 'http://twv-kingsage.tw-family.us/auto_add_claim.php?username=username&pass=md5_password&x="+x+"&y="+y'

    Can someone tell me how to extract the values for x and y from the page and get them to the link?

    Thanks for the help, it is much appreciated.

  • #2
    New Coder
    Join Date
    Nov 2008
    Posts
    59
    Thanks
    5
    Thanked 4 Times in 4 Posts
    Why not put the values in a link within the cell, rather than extracting them with javascript and creating the link?

    Also, are you really passing unescaped form variables directly into a sql statement?

  • #3
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    Quote Originally Posted by imoen View Post
    <td><a href="game.php?village=19392&amp;s=map&amp;x=432&amp;y=608">432|608</a></td>
    &amp; is a Javascript character entity. %26 is the escape code for ampersand.

    Run - escape("village=1939&s=map&x=432&y=608") in a console window, IE8, Webkit, Opera, Firebug, whichever. That will show the proper escape codes.

    As for grabbing values, are you saying that you DON'T have 423 and 608 in the search string already? You have to read them, somehow, from the anchor text itself? If so, on the event - I assume 'on'click - you have the text node or innerHTML of the anchor. The onclick would pass the element reference to your function as the keyword - this. Just .split("|") and the first array position is x, the second y.

    If you don't want an event handler, you could just modify all the hrefs when the page loads. Identify the anchors that you want modified with a particular class name. That's fairly standard. Get all the anchors with that class name. And proceed on each as above, reading the text, splitting it, getting your x and y, composing the search string, and updating each href attribute.

  • #4
    New Coder
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I think you are not understanding me completely.

    village=1939&s=map&x=432&y=608 is from the game. The html code is a page from the game that I did not write. I need my script to scan the page in the game and pull out the x & y so that they can be added to my own database.

    I just need 'http://twv-kingsage.tw-family.us/auto_add_claim.php?username=username&pass=md5_password&x="+x+"&y="+y' to be replaced with 'http://twv-kingsage.tw-family.us/auto_add_claim.php?username=username&pass=md5_password&x=432&y=608

    when the bookmarklet is clicked (or the coordinated of what ever is on the page that is being viewed at that time).

  • #5
    New Coder
    Join Date
    Nov 2008
    Posts
    59
    Thanks
    5
    Thanked 4 Times in 4 Posts
    Right, so what you are saying is that you have pasted copyrighted code to this forum which you don't have the permission to do so? If so, I'd remove it from your post now.

  • #6
    New Coder
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by scrappy View Post
    Right, so what you are saying is that you have pasted copyrighted code to this forum which you don't have the permission to do so? If so, I'd remove it from your post now.
    I wasn't asking how to copy the code, just how to have a script read what is in the box. I only posted the code because from the information that I have read (and do not understand properly) the script needs to know where to find the cell that contains the text and the only way for anyone to tell me how it's done is to know the page's structure.

    Can anyone explain how this is done or not?

  • #7
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    Quote Originally Posted by imoen View Post
    Can anyone explain how this is done or not?
    How what is done, exactly? You didn't answer my questions to try to clarify what you're trying to do.

  • #8
    New Coder
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    What I need is to have the javascript stored as a bookmark. When I am on a village overview page and click on the bookmark I need to have the script get the village coordinates, which is the numbers like in this line:
    <td><a href="game.php?village=19392&amp;s=map&amp;x=432&amp;y=608">432|608</a></td>

    Those numbers are different for every village. Then I need the script to open up a link that calls my own script to run. This is the link:
    'http://twv-kingsage.tw-family.us/auto_add_claim.php?username=username&pass=md5_password&x=x&y=y
    except the bolded X & Y should be replaces with the village's X & Y coordinates from the page.

  • #9
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    Quote Originally Posted by imoen View Post
    What I need is to have the javascript stored as a bookmark. When I am on a village overview page and click on the bookmark I need to have the script get the village coordinates, which is the numbers like in this line:
    <td><a href="game.php?village=19392&amp;s=map&amp;x=432&amp;y=608">432|608</a></td>
    Best I could figure, your 'bookmarklet' would get all the anchor elements, getElementsByTagName, and examine the .href property. If you find "village=" then you know x and y are in the same string for that property. Isolate those with names and put the names in your preferred URL. If you're trying to change pages, then, top.location = , should do it.

  • #10
    New Coder
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Code:
    var url = "game.php?village=10351&amp;s=map&amp;x=424&y=598";
    var pattern = /&amp;s=map&amp;x=(\d{3})&y=(\d{3})/;
    var result = url.match(pattern);
    alert('http://twv-kingsage.tw-family.us/auto_add_claim.php?username=username&pass=pass&x='+result[1]+'&y='+result[2]);
    This works in the testing, but when I am having it search the actual page it doesn't work, instead telling me the value is null. when I put alert(document.links.length); it returns 0 to me too. Am I missing something?
    Last edited by imoen; 07-26-2009 at 01:21 AM.

  • #11
    New Coder
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I got some plugins for firefox and it tells me the cell I need to get the data from is /html/body/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/div[3]/table/tbody/tr[2]/td[2]/a but I'm not sure how the node thing works to get there.

  • #12
    New Coder
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    No one knows?

    Well, what if I would just highlight the 432|608 on the page, is there a way then to have the javascript get the selected text to use in the code?

  • #13
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Greasmonkey
    Code:
    // ==UserScript==
    // @name           sendtwv
    // @namespace      
    // @include        http://s8.kingsage.pl/game.php*
    // ==/UserScript==
    //----- Change here --------------
    username="username";
    password="password";
    //-------stop-------------------------
    	var XPFirst = XPathResult.FIRST_ORDERED_NODE_TYPE;
    
    	function find(xpath, xpres, startnode) {
    		if (!startnode) startnode = document;
    		var ret = document.evaluate(xpath, startnode, null, xpres, null);
    		return xpres == XPFirst ? ret.singleNodeValue : ret;
    	}
    	function elem(tag, aContent){
    		var ret = document.createElement(tag);
    		ret.innerHTML = aContent;
    		return ret;
    	}	
    // what  text with "|"	- Xpath
    
    //------------------------------------------
    
    
    var co="//a[contains(text(),'|')]";
    
    //search & find ()
    var q = find(co, XPFirst);
    w=q.textContent;
    // result "432|608" ->split in array w (x=w[0] and y=w[1])
    w=w.split("|");
    
    
    //var paragraphCount = document.evaluate( 'count(//p)', document, null, XPathResult.ANY_TYPE, null );
    
    tab="//table[@class='borderlist']";
    aLink =elem("a", '    <img src="http://s8.kingsage.pl/img/layout/sc_marker.png" alt="" />claim on');
    aLink.href ="http://twv-kingsage.tw-family.us/auto_add_claim.php?username="+username+"&pass="+password+"&x="+w[0]+"&y="+w[1];
    q.parentNode.insertBefore(aLink, q.nextSibling);
    Or bookmarklet
    Code:
    javascript:var%20username%3Dprompt('%20Enter%20Username%3A'%2C%22Your%20Username%22)%3B%0Avar%20password%3Dprompt('%20Enter%20password%3A'%2C%22Your%20Password%22)%3B%0Avar%20co%3D%22%2F%2Fa%5Bcontains(text()%2C'%7C')%5D%22%3B%0Avar%20ret%20%3D%20document.evaluate(co%2C%20document%2C%20null%2C%20XPathResult.FIRST_ORDERED_NODE_TYPE%2C%20null)%3B%0Aq%3Dret.singleNodeValue%3B%0Aw%3Dq.textContent%3B%0Aw%3Dw.split(%22%7C%22)%3B%0Alink%3D%22http%3A%2F%2Ftwv-kingsage.tw-family.us%2Fauto_add_claim.php%3Fusername%3D%22%2Busername%2B%22%26pass%3D%22%2Bpassword%2B%22%26x%3D%22%2Bw%5B0%5D%2B%22%26y%3D%22%2Bw%5B1%5D%3B%0Adocument.location%3Dlink%3B


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •