HTML 页面中所有嵌入对象的 URL
URLs of all embedded objects in a HTML page
您将如何获取嵌入到网页中的所有对象的URL(或仅主机名(?您将使用哪种标签-属性组合?(还是别的什么?
例如,Stackoverflow 页面的开头如下所示
<!DOCTYPE html>
<html>
<head>
<title>Stack Overflow</title>
<link rel="shortcut icon" href="//cdn.sstatic.net/stackoverflow/img/favicon.ico?v=4f32ecc8f43d">
<link rel="apple-touch-icon image_src" href="//cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png?v=c78bd457575a">
[...]
<meta property="og:image" itemprop="image primaryImageOfPage" content="http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded&a" />
在这里,URL//cdn.sstatic.net/stackoverflow/img/favicon.ico?v=4f32ecc8f43d 和//cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png?v=c78bd457575a 位于 href
属性中,而 http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded&a 位于content
属性中。此外,图像和脚本具有src
属性。
images
HTMLCollection 将是一个起点,但 DOM 规范建议
不使用此属性查找文档中的图像,而是
getElementsByTagName
标签.要考虑的属性组合:a.href
、img.src
、link.href
、script.src
和meta.content
。还有哪个?
这是一种给定正确标签组合的方法。这是锚标记的示例:
var urls = [];
var allA = document.getElementsByTagName("A");
for ( var i = 0; i < allA.length; i++ ) {
if ( typeof allA[i].href === "string" && allA[i].href !== "" ) {
urls.push(allA[i].href);
}
}
可以对所有标记-属性组合重复此操作。
我错过了哪些具有哪些属性的标签?
标签<a>
和<meta>
太多了:<a>
元素没有嵌入,<meta>
发现了一些URL,但也没有嵌入。因此,尝试看起来像
function getAttributeFromTags(tag, attribute) {
var out = [];
var allA = document.getElementsByTagName(tag);
for (var i = 0; i < allA.length; i++) {
if (typeof allA[i][attribute] === 'string' && allA[i][attribute] !== '') {
out.push(allA[i][attribute]);
}
}
return out;
}
var urls = [];
Array.prototype.push.apply(urls, getAttributeFromTags('AUDIO', 'src'));
Array.prototype.push.apply(urls, getAttributeFromTags('EMBED', 'src'));
Array.prototype.push.apply(urls, getAttributeFromTags('IMG', 'src'));
Array.prototype.push.apply(urls, getAttributeFromTags('LINK', 'href'));
Array.prototype.push.apply(urls, getAttributeFromTags('OBJECT', 'data'));
Array.prototype.push.apply(urls, getAttributeFromTags('SCRIPT', 'src'));
Array.prototype.push.apply(urls, getAttributeFromTags('SOURCE', 'src'));
Array.prototype.push.apply(urls, getAttributeFromTags('VIDEO', 'src'));
警告
- 使用
link.href
包含太多的URL(例如,请查看view-source:https://www.youtube.com/watch?v=kPUglMKGXRM(SO不允许查看源链接...((。
实现
HTMLCollection
不提供forEach
(除了使用奇怪的语法(,并且解决方法没有得到广泛支持。
相关文章:
- 在URL中传递JS对象
- 遍历 JSON 对象并检查 URL 是否以某个值结尾
- 如何仅在对象URL's已下载
- Mapbox JS,来自URL的GeoJSON featureCollection对象只添加了一个标记
- Android上的Chrome无法播放来自对象URL的录制音频
- 读取在具有IE的工作程序中生成的对象URL
- 将URL字符串${userid}替换为javascript对象中的实际用户id
- 如何使用 Backbone 读取通过同一 REST URL 返回的两个不同对象
- 我的 AJAX 请求对象无法正确解析 URL 的协议部分
- 向jQueryajax调用添加参数,最好添加到URL或对象中
- 从url对对象进行角度编码和解码
- 如何使用API URL的JSON.parse将JSON字符串更改为Javascript对象
- 如何从每个对象获取jQuery的“title”和“url”值
- 如何用php url返回的JSON对象填充表
- 如何从JSON对象部分获取URL
- 将模型/对象放在Http Get-URL上
- 将参数传递给 BLOB 对象 URL
- chrome/ff中带有createObjectURL的对象URL的数据URI
- 为什么我的AJAX文件下载,使用基于Blob的对象url,破坏文件
- 类似于对象URL的方式在Chrome for Android中播放本地音频文件