简体   繁体   中英

Chrome extension How to get source code of page from url

I want to download source code of page from url in Chrome extension.

I have something like this. But don't know how to format text to html. Or if this formatting works how to display the source code even in console.

 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); });

For example I would like to get text from span "dataValue". How can I achieve that?

在此处输入图像描述

Your wording makes it hard to guess what you want

The first console.log is the textual representation

The second is the DOM object where you can get stuff to show

 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); } }); })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM