我想从 Chrome 扩展程序中的 url 下载页面的源代码。
我有这样的东西。但不知道如何将文本格式化为html。
或者,如果这种格式有效,如何在控制台中显示源代码。
fetch('https://www.transfermarkt.com/robert-lewandowski/profil/spieler/38253').then(r => r.text()).then(result => {
// Result now contains the response text, do what you want...
console.log("Fetch result: " + result);
var parser = new DOMParser();
var source_code = parser.parseFromString(result, "text/html");
console.log("Source code: " + source_code);
});
例如,我想从跨度“dataValue”获取文本。
我怎样才能做到这一点?
下面的第一个 console.log 是文本表示。
第二个是 DOM 对象,您可以在其中获取要显示的内容。
fetch('https://www.transfermarkt.com/robert-lewandowski/profil/spieler/38253').then(r => r.text()).then(result => {
// Result now contains the response text, do what you want...
// console.log("Source code: " + result); // textual representation
var parser = new DOMParser();
var DomObject = parser.parseFromString(result, "text/html");
console.log("Name: " + DomObject.querySelector("title").textContent);
[...DomObject.querySelectorAll(".dataItem")].forEach(item => {
if (item.textContent.trim() === "Joined:") {
console.log("Joined:",item.nextElementSibling.textContent);
}
});
})
附:不要忘记添加站点(或"<all_urls>"
)在manifest.json中:
- 清单V2:
"permissions": ["*://*.transfermarkt.com/"]
- 清单V3:
"host_permissions": ["*://*.transfermarkt.com/"]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)