无法读取空的属性"插入行"



我正在创建一个谷歌chrome扩展,我在其中创建了一个包含草稿信息的html表,并允许将该表保存在csv文件中。我最初的目标是每当他们访问一个新的url时,在表中追加一个新行。

我的popup.js文件的第一个函数在每次访问新网站时都会添加url的div元素,但我的目标是每次访问新站点时在表中都有一行url。我有两个函数(addRow和addRowUsingJQuery(可以工作,但似乎都不起作用。

我的javascript函数一直给我一个错误。有人知道可能是什么问题吗?

错误截图——我一直收到这个错误

manifest.json

{
//Required naming
"name" : "Activity logger",
"version": "1.0",
"manifest_version": 2,
"description": "This support includes the development of a  Chrome Browser Activity Logger.",

"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js", "popup.js"]
}

],


"browser_action": {
"default_icon": "act.png",
"default_title": "Activity",
"default_popup": "popup.html"

},
"background": {
"scripts": ["background.js"]

},


"permissions": ["tabs", "storage"]




}

popup.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
Activity Logger
</title>
<style>
*{
color:#2b2b2b;
font-family: "Roboto Condensed";
}
table{ width:40%;}
th{ text-align:left; color:#4679bd}
tbody > tr:nth-of-type(even) { background-color:#daeaff;)
button( cursor:pointer; margin-top:1rem;)

</style>
</head>
<body onload="addRow()">
<script src="popup.js" charset="utf-8"></script>

<h2>Activity Logger</h2>


<table id = "tableID" border="1">
<thead>
<!--Example table header row with the user, url visited, and time they visited the url etc-->
<tr>
<!--categories-->
<th>Browser</th>
<th>Timestamp</th>
<th>URL</th>
<th>Protocol</th> 
<th>Downloaded File Names</th> 
<th>Description</th>

</tr>
</thead>
<tbody id= "tbodyID">
<tr>
<!--1st row-->
<td>Google</td>
<td>000000</td>
<td>https://stackoverflow.com/questions/ask</td>
<td>example</td>
<td>example</td>
<td>example</td>
</tr>

</tbody>
<!--Goal is to append to this table-->

</table>
<!--when clicked it, ID uses function, exportTableToCSV to download file-->
<a id="click-this">
<u> Save as CSV </u>
</a>
</body>
</html>

popup.js

//loads element url on page
document.addEventListener('DOMContentLoaded', function () {

const bg = chrome.extension.getBackgroundPage()
Object.keys(bg.bears).forEach(function (url) {
const div = document.createElement('div')
div.textContent = `${url}`
document.body.appendChild(div)

})

}, false)

// creates ID to export table to csv--works
document.addEventListener('DOMContentLoaded', function() {
document.getElementById("click-this").addEventListener("click", exportTableToCSV);


});

/*document.addEventListener('DOMContentLoaded', function() {
document.getElementById("click-this").addEventListener("click", addRow);


});
*/


//function to append row to HTML table --underwork--
function addRow() {
//perhaps need an for loop for every page visited 

const bg = chrome.extension.getBackgroundPage()    
Object.keys(bg.bears).forEach(function (url) {

//get html table
// Append product to the table
var table = document.getElementById("tbodyID");

// add new empty row to the table
//1 = in the top 
//table.rows.length = the end
//table.rows.length/2+1 = the center 
var newRow = table.insertRow(0);

// add cells to the row
var nameCell = newRow.insertCell(0);
var urlCell = newRow.insertCell(1);
var timeCell = newRow.insertCell(2);

// add the data to the cells
nameCell.innerHTML = name; 
urlCell.innerHTML = `${url}`; 
timeCell.innerHTML = time;
console.log("works");
}) 
}
addRow()
//console.log(addRowUsingJquery);
//perhaps add row using JQuery--underwork
/*function addRowUsingJquery() {
// Get a reference to table
let table = document.querySelector('#tableID');

const add = chrome.extension.getBackgroundPage()
Object.keys(add.bears).forEach(function (url) {



// Build the row
let template = `
<tr>
<td>${USERNAME}</td>
<td>${`url`}</td>
<td>${TIMESTAMP}</td>
</tr>`;
// Add the row to the end of the table
table.innerHTML += template; 
console.log("works");

})

}
*/                          
//function to for onClick function--works
function downloadCSV(csv, filename) {
var csvFile;
var downloadLink;

csvFile = new Blob([csv], {type:"text/csv"});


downloadLink = document.createElement("a");
downloadLink.download = filename;
downloadLink.href = window.URL.createObjectURL(csvFile);
downloadLink.style.display = "none";
downloadLink.setAttribute("download", "ChromeLog.csv");

document.body.appendChild(downloadLink);


downloadLink.click();
}
//function to export HTML table to csv file--works
function exportTableToCSV(filename) {
var csv = [];
var rows = document.querySelectorAll("table tr");

for(var i = 0; i < rows.length; i++) {
var row = [], cols = rows[i].querySelectorAll("td, th");
for(var j=0; j < cols.length; j++)
row.push(cols[j].innerText);

csv.push(row.join(","));
}   


//download csv file
downloadCSV(csv.join("n"), filename);


}    

如果有人知道如何解决这个问题,为什么我的addRow函数不工作,以及如何正确地编写函数,我会非常感激

将脚本标记放在body标记的最底部

最新更新