问题描述
我目前正在开发一个浏览器扩展程序,该扩展程序可让我从特定网页获取数据,并且我想将其下载到 csv 文件中。
现在我点击保存了所有数据,但是当我尝试使用 chrome.downloads.download() 时,它不断告诉我“无法读取未定义的属性”并标记该函数。我做错了什么?
这是我的 manifest.json 的样子:
"manifest_version": 2,"name": "Data Downloader","description": "Download a csv file with data from MDR","version": "1.0","icons": {
"48": "icon.png"
},"permissions": [
"tabs","<all_urls>","downloads"
],"browser_action": {
"default_icon": "icon.png","default_popup": "popup.html"
}
}
这是我的 popup.html,它工作正常:
<html>
<body style="width: 300px">
Open <a href="http://stackoverflow.com" target="_blank">this page</a> and then
<button id="clickme">click me</button>
<script type="text/javascript" src="popup.js"></script>
</body>
</html>
这就是我点击弹出按钮时调用的 javascript
const copy = [];
const array1 = [];
const array2 = [];
//console.log("1");
//var file = document.getElementById("file").files[0];
//var reader = new FileReader();
//reader.readAsText(file);
//reader.onload=function(){
// var textall=this.result;
//alert(_table);
//var parsedDoc = new DOMParser().parseFromString(textall,'text/html');
//console.log("2");
//console.log(parsedDoc);
for (let i=0; i<6; i++) {
array1.push(document.getElementsByClassName("horse_basics tab_padding")[i]);
}
console.log(array1);
array1.forEach(function(item) {
array.push(item.children[1].children[1].innerText);
array.push(item.children[5].children[1].innerText);
array.push(item.children[9].children[1].innerText);
array.push(item.children[13].children[1].innerText);
});
for (let i=6; i<8; i++){
array2.push(document.getElementsByClassName("horse_basics tab_padding")[i]);
}
array2.forEach(function(item){
array.push(item.children[1].children[1].innerText);
array.push(item.children[5].children[1].innerText);
array.push(item.children[9].children[1].innerText);
array.push(item.children[13].children[1].innerText);
array.push(item.children[17].children[1].innerText);
array.push(item.children[21].children[1].innerText);
array.push(item.children[25].children[1].innerText);
array.push(item.children[29].children[1].innerText);
});
array.push(document.getElementsByClassName("horse_disciplines tab_padding")[0].children[1].children[1].innerText);
array.push(document.getElementsByClassName("horse_disciplines tab_padding")[0].children[5].children[1].innerText);
array.push(document.getElementsByClassName("horse_disciplines tab_padding")[0].children[9].children[1].innerText);
array.push(document.getElementsByClassName("horse_disciplines tab_padding")[0].children[13].children[1].innerText);
array.forEach(function(item) {
var newItem = item.match(/\d+/)[0];
copy.push(newItem);
});
alert(copy);
var names = document.getElementsByClassName("horse_name").innerText;
var blob = new Blob(copy,{type: "text/plain"});
var url = URL.createObjectURL(blob);
chrome.downloads.download({
url: url,filename: "csv/file"+names
});
最后一部分不起作用,我不明白为什么......你能帮我吗?
我确实知道我必须将操作限制在它匹配的内容上(仅限我想使用它的特定页面),但这不是问题,对吧?
感谢您的帮助!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)