我希望迭代json并在每次打开新标签时将信息发送到html。但是,chrome.tabs.onCreated.addListener不起作用。有什么想法吗?
谢谢!
var x;
chrome.tabs.onCreated.addListener(function() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
console.log(xmlhttp.responseText)
myObj = JSON.parse(xmlhttp.responseText);
title = myObj[x].Title;
date = myObj[x].Date;
artist = myObj[x].Artist;
var obj = document.getElementById('main_image');
obj.style.background = 'url("'+myObj[x].Image+'")';
document.getElementById("title").innerHTML = title;
document.getElementById("date").innerHTML = date;
document.getElementById("artist").innerHTML = artist;
document.getElementById("help").innerHTML =
xmlhttp.responseText;
}
if (x < myObj.length + 1) {
x = x + 1;
}
else {
x = 0;
}
}
xmlhttp.open("GET", chrome.extension.getURL("config.json"), true);
xmlhttp.send();
});
答案 0 :(得分:0)
您可以尝试使用ramada函数中的参数吗?
chrome.tabs.onCreated.addListener(函数(标签){
并检查tab.id是否正确重新开始。
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/onCreated#Syntax