给定 URL 我如何从中接收文档对象?
Given a URL how do I receive the document object from it?
我正在使用 document.evaluate(xpath, document, ..)
在浏览器中执行 xpath。但是现在我从页面中检索了一些 URLS 并想对它们执行 document.evaluate(xpath, document, ..)
而无需在浏览器本身中打开 URL 或离开当前页面
甚至可以这样做吗?如果我设法检索 URL 的标记,似乎无法执行 markup.evaluate(xpath, document, ..)
之类的操作。我可以检索 URL 的文档吗?
... and want to perform document.evaluate(xpath, document, ..) on them too without opening the URL in the browser itself or leaving the current page
Is it even possible to do this?
可能不会。仅当 A) 网址来自与您的来源相同的 origin as the page you're doing this on, or B) The server(s) for the origin(s) the URLs are on passlist the origin of your page with CORS, relaxing the Same Origin Policy 时才有可能。
不过,如果其中一个为真,您可以使用 ajax(例如,fetch
) to fetch the content from the URL and DOMParser
将其解析为文档,然后您可以查询该文档。
但同样,如果这些都不是真的,您不能从 browser-hosted JavaScript 执行此操作。你可以从 Node.js 或 simlar.
我正在使用 document.evaluate(xpath, document, ..)
在浏览器中执行 xpath。但是现在我从页面中检索了一些 URLS 并想对它们执行 document.evaluate(xpath, document, ..)
而无需在浏览器本身中打开 URL 或离开当前页面
甚至可以这样做吗?如果我设法检索 URL 的标记,似乎无法执行 markup.evaluate(xpath, document, ..)
之类的操作。我可以检索 URL 的文档吗?
... and want to perform document.evaluate(xpath, document, ..) on them too without opening the URL in the browser itself or leaving the current page
Is it even possible to do this?
可能不会。仅当 A) 网址来自与您的来源相同的 origin as the page you're doing this on, or B) The server(s) for the origin(s) the URLs are on passlist the origin of your page with CORS, relaxing the Same Origin Policy 时才有可能。
不过,如果其中一个为真,您可以使用 ajax(例如,fetch
) to fetch the content from the URL and DOMParser
将其解析为文档,然后您可以查询该文档。
但同样,如果这些都不是真的,您不能从 browser-hosted JavaScript 执行此操作。你可以从 Node.js 或 simlar.