简体   繁体   English

是否可以通过带有 javascript 的 URL 访问网站的 DOM?

[英]Is it possible to access the DOM of a website through a URL with javascript?

I understand that to access the DOM (document object Model) of a website through javascript you must use the Chrome console while on the website.. But what if I need to access the DOM of that same website but from an external document with extension.js or.html?我知道要通过 javascript 访问网站的 DOM(文档 object 模型),您必须在网站上使用 Chrome 控制台。但是如果我需要访问同一网站的 DOM,但需要从带有扩展名的外部文档访问。 js还是.html?

How can I access the DOM of a website without using the browser console?如何在不使用浏览器控制台的情况下访问网站的 DOM?

If the site does not have CORS restrictions, or if you're trying to connecting to a page from a page on the same origin, it's possible.如果该站点没有 CORS 限制,或者如果您尝试同一来源的页面连接到页面,这是可能的。 Once you have the response text (which you can get with fetch or XMLHttpRequest), you can turn it into a document with DOMParser , and then you can navigate through it using DOM methods like querySelector :获得响应文本(可以通过fetch或 XMLHttpRequest 获得)后,您可以使用DOMParser将其转换为文档,然后您可以使用 DOM 方法(如querySelector )浏览它:

 fetch('https://cors-anywhere.herokuapp.com/example.com/').then(res => res.text()).then((responseText) => { const doc = new DOMParser().parseFromString(responseText, 'text/html'); const h1 = doc.querySelector('h1'); console.log(h1.textContent); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过JavaScript访问DOM-Javascript - DOM access through javascript - Javascript 是否可以在打开的javascript窗口中访问DOM? - Is it possible to access the DOM inside an opened javascript window? 是否可以通过 chrome 扩展来观察网站 DOM 上的 class 变化? - Is it possible to observe class changes on the DOM of a website through a chrome extension? 如何通过URL栏将JavaScript注入网站? - How to inject JavaScript into a website through the URL bar? 是否可以通过父文档访问 Shadow DOM 元素? - Is it possible to access Shadow DOM elements through the parent document? 是否可以使用网站的网址在网站上运行javascript函数? - is it possible to run a javascript function on a website by using the url of the website? 如何通过html dom在此脚本中传递javascript url参数? - How to pass javascript url parameter in this script through html dom? 通过jQuery的DOM遍历直接访问javascript对象 - Access javascript object directly through DOM traversing with jQuery Vanilla javascript:如何通过 function 参数访问 DOM 元素属性? - Vanilla javascript: How to access DOM elements properties through a function parameter? 如何在网站上通过URL仅使用Javascript放置参数 - How to put Parameter only with Javascript through URL in the website
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM