为什么这个过滤器不输出IFRAME ?
Why isn't this filter outputting IFRAME?
我试图自动嵌入youtube视频用户生成的内容。我的过滤器一般会查找链接,然后测试它们是否为有效的YouTube视频。如果是,它应该用标准的iframe代码嵌入视频。如果不是,它只是一个链接。但是,过滤器根本不输出iframe代码。我假设这有一些东西可以防止跨站点脚本攻击,但我不知道如何绕过它。
function ytVidId(url) {
var p = /^(?:https?:'/'/)?(?:www'.)?(?:youtu'.be'/|youtube'.com'/(?:embed'/|v'/|watch'?v=|watch'?.+&v=))(('w|-){11})(?:'S+)?$/;
return (url.match(p)) ? RegExp.$1 : false;
}
myapp.filter('parseUrls', function() {
//with protocol
var urlPattern = /(http|ftp|https):'/'/['w-]+('.['w-]+)+(['w.,@?^=%&:'/~+#-]*['w@?^=%&'/~+#-])?/gi;
return function(text, target, otherProp) {
if (text == null) {
return "";
}
angular.forEach(text.match(urlPattern), function(url) {
if(ytVidId(url)){
text = text.replace(url, '<div class="video-container"><iframe src="//www.youtube.com/embed/'+ ytVidId(url) +'" frameborder="0" width="560" height="315"></iframe></div>');
}else{
text = text.replace(url, '<a target="' + target + '" href='+ url + '>' + url + '</a>');
}
});
return text;
};
})
在使用:<span ng-bind-html="p.body | noHTML | newlines | parseUrls:'_blank'"></span>
Angular要求你通过'sce'(严格上下文转义)提供程序传递HTML。
关于SCE提供程序的文档在这里
所以它看起来像这样(未经测试,但理论上应该)
function ytVidId(url) {
var p = /^(?:https?:'/'/)?(?:www'.)?(?:youtu'.be'/|youtube'.com'/(?:embed'/|v'/|watch'?v=|watch'?.+&v=))(('w|-){11})(?:'S+)?$/;
return (url.match(p)) ? RegExp.$1 : false;
}
myapp.filter('parseUrls', ['$sce', function() {
//with protocol
var urlPattern = /(http|ftp|https):'/'/['w-]+('.['w-]+)+(['w.,@?^=%&:'/~+#-]*['w@?^=%&'/~+#-])?/gi;
return function(text, target, otherProp) {
if (text == null) {
return "";
}
angular.forEach(text.match(urlPattern), function(url) {
if(ytVidId(url)){
text = text.replace(url, $sce.trustAs('html', '<div class="video-container"><iframe src="//www.youtube.com/embed/'+ ytVidId(url) +'" frameborder="0" width="560" height="315"></iframe></div>'));
}else{
text = text.replace(url, $sce.trustAs('html', '<a target="' + target + '" href='+ url + '>' + url + '</a>'));
}
});
return text;
};
}])`
相关文章:
- 防止Iframe窗体在新窗口中打开
- 将样式表插入iframe
- 在JavaScript中输出转义字符
- 在Twitter上用ie9中的空白src访问iframe的contentWindow
- 如何在jQuery中将函数的输出分配给变量
- 使用jQuery从原始页面内容创建iframe
- Highslide(iframe的侦听器)
- iframe正在添加标签,需要删除它们
- 通过javascript/html访问twitter共享iframe
- 如何在Facebook上的iframe应用程序中使后退按钮返回到上一页
- 如何将JavaScript结果输出到iframe中
- 如何显示基于JS输出的iframe
- 引导 3 工具提示 - 轻拂鼠标输出..如果它在 iFrame 中发生
- IE9中iFrame中的document.write没有输出
- 如何输出宽度为“0”的iframe;100%”;或者宽度=“&”;.不是,宽度=“;100〃;
- 使用Javascript将URL变量输出到字符串,然后在iframe中使用该字符串
- 当url生成PDF输出时,Iframe onload不工作
- 为什么这个过滤器不输出IFRAME ?
- 将可点击的树元素输出到iframe或区域:以jeasyui为中心
- 输入到表单,输出到iframe URL