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 14 of 14
  1. #1
    DbR
    DbR is offline
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can Javascript Read Source Code?

    Hi! I'm new to Javascript and was wondering if it can read a website's source code. What I'm trying to accomplish is taking information from btc-e.com about bitcoin prices and trading information to make a calculator to help me with trading. If Javascript cannot read and copy parts of a websites source code can anyone think of a way to do this?

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    JavaScript has access to the Document Object Model built from the source code of the page it is attached to.

    JavaScript can also communicate with the server to retrieve information that a server side script may have obtained about the source of a different web page.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    DbR
    DbR is offline
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What code would I need to communicate with the javascript of a different website?

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by DbR View Post
    What code would I need to communicate with the javascript of a different website?
    If you mean where both scripts are attached to pages displayed in the browser at the same time then you can use postMessage in JavaScript on the web one page and attach a message event listener in the javaScript of the other web page to pass information from one to the other.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    It's a two-way communication. Both websites that need to communicate must have the postMessage() and 'message' event handler to enable them to exchange messages. And they need to have agreement what information they can share.

    So if the target website doesn't have those or, if they have, but only allow communication between their domain and/or trusted sites, or they only allow certain information to be shared, then you won't be able to get the information you need from them.

    Here's a reference to postMessage:

    https://developer.mozilla.org/en-US/...ow.postMessage
    Last edited by glenngv; 04-11-2014 at 08:54 PM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    I looked into btc-e.com and they provide API.

    Take a look at their documentation if they have an API method to get the information you need.

    https://btc-e.com/api/documentation
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #8
    DbR
    DbR is offline
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Despite the help you have given me, I'm still lost. Can you write the code for me that I would need for getting the 9th line after the semi colon from the source code on this site litecoinexchangerate.org
    Remember I'm really new to JavaScript and coding in general. Your help is greatly appreciated.

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    You don't have direct access to the source code of the page - only the document object model view of it. You access the source code by referencing tags within the page - there are no lines in HTML code so there is no 9th line to retrieve.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You can solve this with a server-side solution (e.g. using cURL in PHP) just like how Facebook extracts the document title and description of a website when you share a link. But still, this is not reliable in your case as the HTML source of the target site may change in the future so the information you need from the HTML output might have been changed, moved or deleted.
    Last edited by glenngv; 04-12-2014 at 01:51 AM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #11
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Javascript wasn't really meant to do what you are trying to do - the best way to do this is by using the API as glenngv suggests. If you're looking for quick n' dirty, though, this does the job:
    Code:
    <body>
    <div id="output"></div>
    
    <script>
    function print(data){
    document.getElementById("output").innerHTML=data.query.results.body.div.table[1].tr.td[0].h1.split("$")[1];
    }
    </script>
    <script src='http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.litecoinexchangerate.org%2F%22&format=json&callback=print'></script>
    
    </body>
    (and of course, glenn's caveat about the code breaking if the html changes still applies)

  • #12
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    YQL is also your alternative as xelawho suggested. (In fact, I want to use it in a new mobile app that I plan to develop )

    But note that using it is subject to usage limits and restrictions. From their FAQ:
    How many times can I call YQL in a minute/hour/day?
    Rate limits in YQL are based on your authentication. If you use IP-based authentication, then you are limited to 2,000 calls/hour/IP to the public YQL Web service URL (/v1/public/*) or 20,000 calls/hour/IP to the private YQL Web service URL (/v1/yql/*) that requires OAuth authorization. See the YQL Web Service URLs for the public and private URLs. Applications (identified by an Access Key) are limited to 100,000 calls/day/key*. However, in order to make sure the service is available for everyone we ask that you don't call YQL more than 0.2 times/second or 1,000 times/hour for IP authenticated users and 2.7 times/second or 10,000 times/hour
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #13
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Or (being that it appears to be a page that only OP will be using) you could do this by making a userscript, and just get that number from the title:
    Code:
    document.title.match(/[0-9\.]+/)[0];

  • #14
    New to the CF scene
    Join Date
    Apr 2014
    Location
    Illinois
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    everyone beat me to this, darn. I was really excited to answer his question but you people helped he/she out already. I hope you found your answer though, DbR.


  •  

    Posting Permissions

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