Javascript 如何生成Chrome扩展';基于定期获取的数据的弹出式正文?
我正在编程一个Chrome扩展,它将每分钟获取JSON数据,并根据结果更新弹出窗口的html正文(单击图标后可访问) 这是我的background.js:Javascript 如何生成Chrome扩展';基于定期获取的数据的弹出式正文?,javascript,json,google-chrome,dom,google-chrome-extension,Javascript,Json,Google Chrome,Dom,Google Chrome Extension,我正在编程一个Chrome扩展,它将每分钟获取JSON数据,并根据结果更新弹出窗口的html正文(单击图标后可访问) 这是我的background.js: var lastResponse = ""; //Here I save the JSON I receive function generateTable() { //This function generates the html body table console.log('Generating table'); v
var lastResponse = ""; //Here I save the JSON I receive
function generateTable() { //This function generates the html body table
console.log('Generating table');
var resp = JSON.parse(lastResponse);
var headers=new Array("", "last", "buy", "sell", "15m", "24h");
// creates a <table> element and a <tbody> element
var tbl = document.createElement("table");
var tblBody = document.createElement("tbody");
// creating all cells
var row = document.createElement("tr");
for (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 a table row
row = document.createElement("tr");
for (var i = 0; i < 6; i++) {
// Create a <td> element and a text node, make the text
// node the contents of the <td>, and put the <td> at
// the end of the 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 the row to the end of the table body
tblBody.appendChild(row);
}
// put the <tbody> in the <table>
tbl.appendChild(tblBody);
// appends <table> into <body>
document.body.appendChild(tbl);
// sets the border attribute of tbl to 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();
var lastResponse=“”//在这里,我保存收到的JSON
函数generateTable(){//此函数生成html正文表
log(“生成表”);
var resp=JSON.parse(lastResponse);
var头=新数组(“,”上次“,”买入“,”卖出“,”15米“,”24小时“);
//创建一个元素和一个元素
var tbl=document.createElement(“表”);
var tblBody=document.createElement(“tbody”);
//创建所有单元格
var行=document.createElement(“tr”);
对于(var i=0;i由于函数generateTable()
未关闭,因此您将获得该错误
是的,但是在第26行中,你有一个for(var key in resp){{{
…这是一个额外的{
,导致了一个格式错误的javascript函数。(未关闭)
修正那条线,告诉我们这是否是唯一的问题
祝你的项目好运
编辑:(新信息更新) 将json变量传递给函数,并仔细检查格式是否正确。 我还建议您使用
try
功能
// your JSON data as string
var lastResponse = '{"id":1, "color":"red"}';
// the function...
function generateTable(jsonString){
...some code...
...some code...
try {
var resp = JSON.parse(jsonString);
} catch (e) {
// return false if the string can't be parsed to json
console.log('error found:' + e);
return false;
}
// looking good, go ahead!
...some code...
...some code...
}
// and last, call the function with the JSON string in it:
generateTable(lastResponse);
最后,一如往常,祝你好运,继续尝试;-)你会遇到这个错误,因为
函数generateTable()
没有关闭
是的,但是在第26行中,你有一个for(var key in resp){{{
…这是一个额外的{
,导致了一个格式错误的javascript函数。(未关闭)
修正那条线,告诉我们这是否是唯一的问题
祝你的项目好运
编辑:(新信息更新) 将json变量传递给函数,并仔细检查格式是否正确。 我还建议您使用
try
功能
// your JSON data as string
var lastResponse = '{"id":1, "color":"red"}';
// the function...
function generateTable(jsonString){
...some code...
...some code...
try {
var resp = JSON.parse(jsonString);
} catch (e) {
// return false if the string can't be parsed to json
console.log('error found:' + e);
return false;
}
// looking good, go ahead!
...some code...
...some code...
}
// and last, call the function with the JSON string in it:
generateTable(lastResponse);
最后,和往常一样,祝你好运,继续尝试;-)Hmm,现在它在
var resp=JSON.parse(lastResponse);
,这意味着它尝试解析最有可能不存在的JSON……尝试将JSON变量传递给函数,如:函数生成表(jsonString){…var resp=JSON.parse(jsonString);…}
然后,用generateTable(lastResponse)
Hmm调用函数,现在它在var resp=JSON.parse(lastResponse);
处声明“输入意外结束”;,这意味着它尝试解析最有可能不存在的JSON…尝试将JSON变量传递给函数,如:function generateTable(jsonString){…var resp=JSON.parse(jsonString);…}
然后用generateTable(lastResponse)