问题描述
我正在使用 HTML 和 js 构建一个简单的 chrome 扩展
我卡在了第 3 步。
我知道这可以使用 Python 中的 Beautiful Soup 来完成,但不知道如何使用 JS 来实现它。
在执行 console.log(html)
时,我得到一个空字符串,但相同的代码适用于任何其他 html 页面。
HTML 代码 -
<!doctype html>
<html>
<head>
<script src="popup.js"></script>
</head>
<body>
<h1>Top 5 searches from google</h1>
<button id="checkPage">Click!</button>
<ul>
</ul>
</body>
</html>
JS代码-
document.addEventListener('DOMContentLoaded',function() {
var checkPageButton = document.getElementById('checkPage');
checkPageButton.addEventListener('click',function() {
chrome.tabs.query({active: true,lastFocusedWindow: true},tabs => {
let myUrl = 'https://www.google.com/search?q='+tabs[0].url;
fetch(myUrl,{mode: 'no-cors'}).then(function (response) {
console.log(response)
console.log(response.text())
return response.text();
}).then(function (html) {
console.log(html)
var parser = new DOMParser();
var doc = parser.parseFromString(html,'text/html');
console.log(doc)
var h3Elements = doc.getElementsByTagName("h3");
console.log(h3Elements);
}).catch(function (err) {
console.warn('Something went wrong.',err);
});
});
},false);
},false);
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)