Search code examples
javascriptnode.jsreal-time-updatesreal-time-data

Get HTML content update of an URL


Basically i want to have an update content of the web, https://www.investing.com/indices/indices-futures source

In Dow 30, the last value is updating itself(real-time update from Investing.com server) and i would like to know is there any method to capture the change of values without requesting the website again, so that i can update on my code asynchronously. Since all i found online about being notified on change is based on their own html, but in my case it is external url, so i am asking here to gain some insight


Solution

  • You can add some code into the chrome console and track this value every second to notify you.

    let last_value = -1
    let class_selector = 'pid-8873-last'
    setInterval(function() {
      let v = document.getElementsByClassName(class_selector)[0].innerText
      if (v != last_value) {
        console.log("Value as been updated to " + v)
        last_value = v
      }
    }, 1000)
    
    > Value as been updated to 25,799.5
    > Value as been updated to 25,798.5
    

    But you must have a browser open, and create an ajax request when value is updated. If you don't want any Browser, but be run into a server, you can check PhantomJS


    EDIT WITH PHANTOMJS

    They're some update to do to work with PhantomJS.

    • You need to replace let by var
    • document isn't accessible, so you need to use evaluate
    • https may require to add --ssl-protocol=any

    ./test.js

    var page = require('webpage').create();
    page.open('https://www.investing.com/indices/indices-futures', function(status) {
      var last_value = -1
    
      setInterval(function() {
        var value = page.evaluate(function() {
          return document.getElementsByClassName('pid-8873-last')[0].innerText
        })
    
        if (value != last_value) {
          console.log("Value as been updated to " + value)
          last_value = value
        }
      }, 1000)
    //  phantom.exit()
    })
    

    Then run it from the same directory:

    # phantomjs test.js 
    Value as been updated to 25,799.0