[英]Access DOM of linked pages in Chrome bookmarks
I want to create a Chrome extension where I programmatically access the DOM of the bookmarked links, check for the page title and display it in the popup.html
. 我想创建一个Chrome扩展程序,我可以通过编程方式访问已添加书签的链接的DOM,检查页面标题并将其显示在
popup.html
。
Can we programmatically access DOM of multiple URLs without having to open them? 我们可以编程方式访问多个URL的DOM而无需打开它们吗?
In short: Nope, not always! 简而言之:不,不总是!
Slightly longer: To build a page's DOM, you need to request that page's source, and build the DOM from it. 稍微长一点:要构建页面的DOM,您需要请求该页面的源代码,并从中构建DOM。 Since page source might contain executable scripts that modify the DOM, this is a non-trivial operation.
由于页面源可能包含修改DOM的可执行脚本,因此这是一项非常重要的操作。
Even longer: 甚至更长:
You could potentially load the page's source via XHR (setting aside all authorization-related questions). 您可以通过XHR加载页面的源(保留所有与授权相关的问题)。 It may contain a title that's usable for you in the
<head>
, but some pages modify that title dynamically, and what you grab will not be the "final" title. 它可能包含一个在
<head>
可用的标题,但有些页面会动态修改该标题,而你抓取的标题将不是“最终”标题。 Even then, parsing the (static) HTML for the title can be non-trivial. 即便如此,解析标题的(静态)HTML也是非常重要的。
In general, you could try to load the site in an <iframe>
and read the "final" title from there. 通常,您可以尝试在
<iframe>
加载网站,然后从那里读取“最终”标题。 Except a lot of sites refuse to load in an iframe, so this approach also fails. 除了很多网站拒绝加载iframe,所以这种方法也失败了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.