0

I'm trying to use a script for a webpage that scrapes and then parses an RSS feed using request-promise. From what I understand, request-promise has to run server-side, so I'm calling the script externally in my html. I've been using localStorage.setItem and localStorage.getItem to pass the variables between the external script and another embedded script that assigns the variables to html elements that should be displayed throughout the page as text.

In Chrome, when the page first loads, it's blank, but then works fine after I refresh it once. In Firefox and Safari, it remains blank no matter how many times I refresh. The issue seems to be that the html is not "receiving" the variables from localStorage.getItem. I suspect it has something to do with the way the different browsers are handling the request-promise function, but I'm stumped. What am I doing wrong?

Here's the request-promise script:

var xmltext
var latlonquery, latquery, lonquery, url

const rp = require('request-promise')
const options = {
            headers: {'user-agent': 'node.js'}
}

function doQuery() {
    latlonquery = new URLSearchParams(window.location.search)
    latquery = latlonquery.get("lat")
    lonquery = latlonquery.get("lon")
    url = "https://forecast.weather.gov/MapClick.php?lat=" + latquery + "&lon=" + lonquery + "&FcstType=digitalDWML"
}

function doRequest() {
return rp(url,options).then(function(html){
  return html
  })
}

doQuery()
doRequest().then(function(html){
                 xmltext = html
                 localStorage.setItem("xml_says_this", xmltext)
                 localStorage.setItem("lat_says_this", latquery)
                 localStorage.setItem("lon_says_this", lonquery)
})

You can see an example of the problem in action here.

Schlockading
  • 113
  • 3
  • 2
    As far as I know require isn't supported browser-wise. Maybe Chrome is an exception, but the way to go is to exclude external libraries via the ` – Zer0 Sep 15 '20 at 15:28
  • Also in temp-chart `datehour` is accessed before initialization – brk Sep 15 '20 at 15:29
  • the first issue is in the first line: **var xmltext = localStorage.getItem("xml_says_this")** When xmltext is null .... – gaetanoM Sep 15 '20 at 15:35
  • 2
    It's an issue specifically with the network request. I can reproduce it too: on Firefox, the response payload from weather.gov is empty, but it's populated on Chrome. – CertainPerformance Sep 15 '20 at 15:36
  • After I fixed local storage it works on ff – gaetanoM Sep 15 '20 at 15:38
  • @gaetanoM Fixed local storage how? Does localStorage.getItem not populate xmltext with the html string in doRequest()? – Schlockading Sep 15 '20 at 16:54

0 Answers0