javascript - How to generate Chrome Extension's popup body based on periodically fetched data? -


i programming chrome extension fetching json data every minute , update popup's html body (accessible after clicking icon) based on results.

this background.js:

var lastresponse = ""; //here save json receive  function generatetable() { //this function generates html body table     console.log('generating table');     var resp = json.parse(lastresponse);      var headers=new array("", "last", "buy", "sell", "15m", "24h");      // creates <table> element , <tbody> element     var tbl      = document.createelement("table");     var tblbody = document.createelement("tbody");      // creating cells      var row = document.createelement("tr");     (var i=0;i<6;i++)     {         var cell = document.createelement("td");         var celltext = document.createtextnode(headers[i]);         cell.appendchild(celltext);         row.appendchild(cell);     }     tblbody.appendchild(row);       for(var key in resp){ {         // creates table row         row = document.createelement("tr");          (var = 0; < 6; i++) {             // create <td> element , text node, make text             // node contents of <td>, , put <td> @             // end of table row             var cell = document.createelement("td");             if (i==0) {                 var celltext = document.createtextnode("");             } else {                 var celltext = document.createtextnode(key);             }             cell.appendchild(celltext);             row.appendchild(cell);         }          // add row end of table body         tblbody.appendchild(row);     }      // put <tbody> in <table>     tbl.appendchild(tblbody);     // appends <table> <body>     document.body.appendchild(tbl);     // sets border attribute of tbl 2;     tbl.setattribute("border", "2"); }  function readblock() {     var xhr = new xmlhttprequest();     xhr.open("get", "https://blockchain.info/ticker");     xhr.onreadystatechange = function() {         if (xhr.readystate == 4) {             if (xhr.status == 200) {                 lastresponse = xhr.responsetext;             }         }     }     xhr.send(); }  function onalarm(alarm) {     if (alarm && alarm.name == 'refresh') {         readblock();     } }  document.addeventlistener('domcontentloaded', function () {   generatetable(); });   chrome.alarms.create('refresh', {periodinminutes: 1.0}); chrome.alarms.onalarm.addlistener(onalarm); readblock(); 

when try run this, error of

uncaught syntaxerror: unexpected end of input background.js:82

line 82 being last line of .js. guessing having problems because try accessing json before available. how should go making program work properly?

you getting error because function generatetable() not closed...

well, is, in line 26 have for(var key in resp){ { ... that's { , causing malformed javascript function. (unclosed)

fix line , tell if problem.

good luck whith project.


edit: (new info update)

pass json variable function , double check has correct format. suggest also, use try function...

   // json data string    var lastresponse = '{"id":1, "color":"red"}';    // function...    function generatetable(jsonstring){       ...some code...       ...some code...       try {          var resp = json.parse(jsonstring);       } catch (e) {          // return false if string can't parsed json          console.log('error found:' + e);          return false;       }       // looking good, go ahead!       ...some code...       ...some code...    }    // , last, call function json string in it:     generatetable(lastresponse); 

and , always, luck , keep trying ;-)


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -