Javascript 为Jquery Oembed使用自己的处理程序
我正在尝试使用自己的函数(从未喜欢过Javascript 为Jquery Oembed使用自己的处理程序,javascript,jquery,oembed,Javascript,Jquery,Oembed,我正在尝试使用自己的函数(从未喜欢过$(document).ready())从URL动态加载嵌入 我试着这样做: function video(donde,url) { $("#"+donde).oembed(url); return false; } 例如: <div class="texto"> [my title] <span style="float: right;"> <img onclick="video('vide
$(document).ready()
)从URL动态加载嵌入
我试着这样做:
function video(donde,url) {
$("#"+donde).oembed(url);
return false;
}
例如:
<div class="texto">
[my title]
<span style="float: right;">
<img onclick="video('video68084','http://www.youtube.com/watch?v=ORZTCQjAuZY');" src="http://i1.ytimg.com/vi/ORZTCQjAuZY/default.jpg" alt="preview" onerror="this.src='images/linket.png';" class="caja_con_sombra" style="width: 80px; height: 60px;"> </span>
<div style="float: left; width: 76%;">
[my description]
</div>
<div id="video68084"></div>
</div>
点击víafirebug:(
你知道我遗漏了什么吗?错误源文件是jquery.min.js:S你没有遗漏任何东西,你在oembed插件中发现了一个bug。很好的发现,也许你将来会从事质量保证工作:) 看起来oembed和jQuery都想使用
.type
,但jQuery最终得到了oembed的字符串.type
,它想要自己的函数.type
在jQuery-1.3.2中工作正常,但在jQuery-1.4.3中中断
您可以从以下JSFIDLE中获取oembed插件的功能版本:
这个问题已经解决了,但编入词典的作者似乎没有倾听。如果您需要,这里有一个插件的固定版本:
(function($) {
$.fn.oembed = function(url, options, callback) {
options = $.extend(true, $.fn.oembed.defaults, options);
return this.each(function() {
var container = $(this),
resourceURL = (url != null) ? url : container.attr("href"),
provider;
if (!callback) callback = function(container, oembed) {
$.fn.oembed.insertCode(container, options.embedMethod, oembed);
};
if (resourceURL != null) {
provider = getOEmbedProvider(resourceURL);
if (provider != null) {
provider.params = getNormalizedParams(options[provider.name]) || {};
provider.maxWidth = options.maxWidth;
provider.maxHeight = options.maxHeight;
provider.embedCode(container, resourceURL, callback);
return;
}
}
callback(container, null);
});
};
// Plugin defaults
$.fn.oembed.defaults = {
maxWidth: null,
maxHeight: null,
embedMethod: "replace" // "auto", "append", "fill"
};
$.fn.oembed.insertCode = function(container, embedMethod, oembed) {
if (oembed == null)
return;
switch(embedMethod)
{
case "auto":
if (container.attr("href") != null) {
$.fn.oembed.insertCode(container, "append", oembed);
}
else {
$.fn.oembed.insertCode(container, "replace", oembed);
};
break;
case "replace":
container.replaceWith(oembed.code);
break;
case "fill":
container.html(oembed.code);
break;
case "append":
var oembedContainer = container.next();
if (oembedContainer == null || !oembedContainer.hasClass("oembed-container")) {
oembedContainer = container
.after('<div class="oembed-container"></div>')
.next(".oembed-container");
if (oembed != null && oembed.provider_name != null)
oembedContainer.toggleClass("oembed-container-" + oembed.provider_name);
}
oembedContainer.html(oembed.code);
break;
}
};
$.fn.oembed.getPhotoCode = function(url, data) {
var alt = data.title ? data.title : '';
alt += data.author_name ? ' - ' + data.author_name : '';
alt += data.provider_name ? ' - ' +data.provider_name : '';
var code = '<div><a href="' + url + '" target="_blank"><img src="' + data.url + '" alt="' + alt + '"/></a></div>';
if (data.html)
code += "<div>" + data.html + "</div>";
return code;
};
$.fn.oembed.getVideoCode = function(url, data) {
var code = data.html;
return code;
};
$.fn.oembed.getRichCode = function(url, data) {
var code = data.html;
return code;
};
$.fn.oembed.getGenericCode = function(url, data) {
var title = (data.title != null) ? data.title : url,
code = '<a href="' + url + '">' + title + '</a>';
if (data.html)
code += "<div>" + data.html + "</div>";
return code;
};
$.fn.oembed.isAvailable = function(url) {
var provider = getOEmbedProvider(url);
return (provider != null);
};
/* Private Methods */
function getOEmbedProvider(url) {
for (var i = 0; i < providers.length; i++) {
if (providers[i].matches(url))
return providers[i];
}
return null;
}
function getNormalizedParams(params) {
if (params == null)
return null;
var normalizedParams = {};
for (var key in params) {
if (key != null)
normalizedParams[key.toLowerCase()] = params[key];
}
return normalizedParams;
}
var providers = [
new OEmbedProvider("fivemin", "5min.com"),
new OEmbedProvider("amazon", "amazon.com"),
new OEmbedProvider("flickr", "flickr", "http://flickr.com/services/oembed", "jsoncallback"),
new OEmbedProvider("googlevideo", "video.google."),
new OEmbedProvider("hulu", "hulu.com"),
new OEmbedProvider("imdb", "imdb.com"),
new OEmbedProvider("metacafe", "metacafe.com"),
new OEmbedProvider("qik", "qik.com"),
new OEmbedProvider("revision3", "revision3.com"),
new OEmbedProvider("slideshare", "slideshare.net"),
new OEmbedProvider("twitpic", "twitpic.com"),
new OEmbedProvider("viddler", "viddler.com"),
new OEmbedProvider("vimeo", "vimeo.com", "http://vimeo.com/api/oembed.json"),
new OEmbedProvider("wikipedia", "wikipedia.org"),
new OEmbedProvider("wordpress", "wordpress.com"),
new OEmbedProvider("youtube", "youtube.com"),
new OEmbedProvider("vids.myspace.com", "vids.myspace.com", "http://vids.myspace.com/index.cfm?fuseaction=oembed"),
new OEmbedProvider("screenr", "screenr.com", "http://screenr.com/api/oembed.json")
];
function OEmbedProvider(name, urlPattern, oEmbedUrl, callbackparameter) {
this.name = name;
this.urlPattern = urlPattern;
this.oEmbedUrl = (oEmbedUrl != null) ? oEmbedUrl : "http://oohembed.com/oohembed/";
this.callbackparameter = (callbackparameter != null) ? callbackparameter : "callback";
this.maxWidth = 500;
this.maxHeight = 400;
this.matches = function(externalUrl) {
// TODO: Convert to Regex
return externalUrl.indexOf(this.urlPattern) >= 0;
};
this.getRequestUrl = function(externalUrl) {
var url = this.oEmbedUrl;
if (url.indexOf("?") <= 0)
url = url + "?";
else
url = url + "&";
var qs = "";
if (this.maxWidth != null && this.params["maxwidth"] == null)
this.params["maxwidth"] = this.maxWidth;
if (this.maxHeight != null && this.params["maxheight"] == null)
this.params["maxheight"] = this.maxHeight;
for (var i in this.params) {
// We don't want them to jack everything up by changing the callback parameter
if (i == this.callbackparameter)
continue;
// allows the options to be set to null, don't send null values to the server as parameters
if (this.params[i] != null)
qs += "&" + escape(i) + "=" + this.params[i];
}
url += "format=json&url=" + escape(externalUrl) +
qs +
"&" + this.callbackparameter + "=?";
return url;
};
this.embedCode = function(container, externalUrl, callback) {
var request = this.getRequestUrl(externalUrl);
$.getJSON(request, function(data) {
var code, type = data.type;
var oembed = $.extend({ }, data);
switch (type) {
case "photo":
oembed.code = $.fn.oembed.getPhotoCode(externalUrl, data);
break;
case "video":
oembed.code = $.fn.oembed.getVideoCode(externalUrl, data);
break;
case "rich":
oembed.code = $.fn.oembed.getRichCode(externalUrl, data);
break;
default:
oembed.code = $.fn.oembed.getGenericCode(externalUrl, data);
break;
}
callback(container, oembed);
});
};
}
})(jQuery);
(函数($){
$.fn.oembed=函数(url、选项、回调){
options=$.extend(true,$.fn.oembed.defaults,options);
返回此值。每个(函数(){
变量容器=$(此),
resourceURL=(url!=null)?url:container.attr(“href”),
供应商;
如果(!callback)callback=函数(容器,oembed){
$.fn.oembed.insertCode(容器,options.embeddeMethod,oembed);
};
if(resourceURL!=null){
provider=getOEmbedProvider(resourceURL);
如果(提供程序!=null){
provider.params=getNormalizedParams(选项[provider.name])| |{};
provider.maxWidth=options.maxWidth;
provider.maxHeight=options.maxHeight;
embedCode(容器、资源URL、回调);
返回;
}
}
回调(容器,null);
});
};
//插件默认值
$.fn.oembed.defaults={
maxWidth:null,
maxHeight:null,
嵌入方法:“替换”/“自动”、“追加”、“填充”
};
$.fn.oembed.insertCode=函数(容器、嵌入方法、oembed){
如果(oembed==null)
返回;
开关(嵌入法)
{
案例“自动”:
if(container.attr(“href”)!=null){
$.fn.oembed.insertCode(容器,“append”,oembed);
}
否则{
$.fn.oembed.insertCode(容器,“替换”,oembed);
};
打破
案例“替换”:
container.replaceWith(oembed.code);
打破
案例“填充”:
html(oembed.code);
打破
案例“追加”:
var oembedContainer=container.next();
if(oembedContainer==null | |!oembedContainer.hasClass(“oembed容器”)){
oembedContainer=容器
.在(“”)之后
.下一个(“.oembed容器”);
if(oembed!=null&&oembed.provider\u name!=null)
oembedContainer.toggleClass(“oembed容器-”+oembed.provider_名称);
}
html(oembed.code);
打破
}
};
$.fn.oembed.getPhotoCode=函数(url,数据){
var alt=data.title?data.title:“”;
alt+=data.author\u name?'-'+data.author\u name:'';
alt+=data.provider\u名称?'-'+data.provider\u名称:“”;
var代码=“”;
if(data.html)
代码+=“”+data.html+“”;
返回码;
};
$.fn.oembed.getVideoCode=函数(url,数据){
var code=data.html;
返回码;
};
$.fn.oembed.getRichCode=函数(url,数据){
var code=data.html;
返回码;
};
$.fn.oembed.getGenericCode=函数(url,数据){
var title=(data.title!=null)?data.title:url,
代码=“”;
if(data.html)
代码+=“”+data.html+“”;
返回码;
};
$.fn.oembed.isAvailable=函数(url){
var provider=getOEmbedProvider(url);
返回(提供程序!=null);
};
/*私有方法*/
函数getOEmbedProvider(url){
对于(变量i=0;i(function($) {
$.fn.oembed = function(url, options, callback) {
options = $.extend(true, $.fn.oembed.defaults, options);
return this.each(function() {
var container = $(this),
resourceURL = (url != null) ? url : container.attr("href"),
provider;
if (!callback) callback = function(container, oembed) {
$.fn.oembed.insertCode(container, options.embedMethod, oembed);
};
if (resourceURL != null) {
provider = getOEmbedProvider(resourceURL);
if (provider != null) {
provider.params = getNormalizedParams(options[provider.name]) || {};
provider.maxWidth = options.maxWidth;
provider.maxHeight = options.maxHeight;
provider.embedCode(container, resourceURL, callback);
return;
}
}
callback(container, null);
});
};
// Plugin defaults
$.fn.oembed.defaults = {
maxWidth: null,
maxHeight: null,
embedMethod: "replace" // "auto", "append", "fill"
};
$.fn.oembed.insertCode = function(container, embedMethod, oembed) {
if (oembed == null)
return;
switch(embedMethod)
{
case "auto":
if (container.attr("href") != null) {
$.fn.oembed.insertCode(container, "append", oembed);
}
else {
$.fn.oembed.insertCode(container, "replace", oembed);
};
break;
case "replace":
container.replaceWith(oembed.code);
break;
case "fill":
container.html(oembed.code);
break;
case "append":
var oembedContainer = container.next();
if (oembedContainer == null || !oembedContainer.hasClass("oembed-container")) {
oembedContainer = container
.after('<div class="oembed-container"></div>')
.next(".oembed-container");
if (oembed != null && oembed.provider_name != null)
oembedContainer.toggleClass("oembed-container-" + oembed.provider_name);
}
oembedContainer.html(oembed.code);
break;
}
};
$.fn.oembed.getPhotoCode = function(url, data) {
var alt = data.title ? data.title : '';
alt += data.author_name ? ' - ' + data.author_name : '';
alt += data.provider_name ? ' - ' +data.provider_name : '';
var code = '<div><a href="' + url + '" target="_blank"><img src="' + data.url + '" alt="' + alt + '"/></a></div>';
if (data.html)
code += "<div>" + data.html + "</div>";
return code;
};
$.fn.oembed.getVideoCode = function(url, data) {
var code = data.html;
return code;
};
$.fn.oembed.getRichCode = function(url, data) {
var code = data.html;
return code;
};
$.fn.oembed.getGenericCode = function(url, data) {
var title = (data.title != null) ? data.title : url,
code = '<a href="' + url + '">' + title + '</a>';
if (data.html)
code += "<div>" + data.html + "</div>";
return code;
};
$.fn.oembed.isAvailable = function(url) {
var provider = getOEmbedProvider(url);
return (provider != null);
};
/* Private Methods */
function getOEmbedProvider(url) {
for (var i = 0; i < providers.length; i++) {
if (providers[i].matches(url))
return providers[i];
}
return null;
}
function getNormalizedParams(params) {
if (params == null)
return null;
var normalizedParams = {};
for (var key in params) {
if (key != null)
normalizedParams[key.toLowerCase()] = params[key];
}
return normalizedParams;
}
var providers = [
new OEmbedProvider("fivemin", "5min.com"),
new OEmbedProvider("amazon", "amazon.com"),
new OEmbedProvider("flickr", "flickr", "http://flickr.com/services/oembed", "jsoncallback"),
new OEmbedProvider("googlevideo", "video.google."),
new OEmbedProvider("hulu", "hulu.com"),
new OEmbedProvider("imdb", "imdb.com"),
new OEmbedProvider("metacafe", "metacafe.com"),
new OEmbedProvider("qik", "qik.com"),
new OEmbedProvider("revision3", "revision3.com"),
new OEmbedProvider("slideshare", "slideshare.net"),
new OEmbedProvider("twitpic", "twitpic.com"),
new OEmbedProvider("viddler", "viddler.com"),
new OEmbedProvider("vimeo", "vimeo.com", "http://vimeo.com/api/oembed.json"),
new OEmbedProvider("wikipedia", "wikipedia.org"),
new OEmbedProvider("wordpress", "wordpress.com"),
new OEmbedProvider("youtube", "youtube.com"),
new OEmbedProvider("vids.myspace.com", "vids.myspace.com", "http://vids.myspace.com/index.cfm?fuseaction=oembed"),
new OEmbedProvider("screenr", "screenr.com", "http://screenr.com/api/oembed.json")
];
function OEmbedProvider(name, urlPattern, oEmbedUrl, callbackparameter) {
this.name = name;
this.urlPattern = urlPattern;
this.oEmbedUrl = (oEmbedUrl != null) ? oEmbedUrl : "http://oohembed.com/oohembed/";
this.callbackparameter = (callbackparameter != null) ? callbackparameter : "callback";
this.maxWidth = 500;
this.maxHeight = 400;
this.matches = function(externalUrl) {
// TODO: Convert to Regex
return externalUrl.indexOf(this.urlPattern) >= 0;
};
this.getRequestUrl = function(externalUrl) {
var url = this.oEmbedUrl;
if (url.indexOf("?") <= 0)
url = url + "?";
else
url = url + "&";
var qs = "";
if (this.maxWidth != null && this.params["maxwidth"] == null)
this.params["maxwidth"] = this.maxWidth;
if (this.maxHeight != null && this.params["maxheight"] == null)
this.params["maxheight"] = this.maxHeight;
for (var i in this.params) {
// We don't want them to jack everything up by changing the callback parameter
if (i == this.callbackparameter)
continue;
// allows the options to be set to null, don't send null values to the server as parameters
if (this.params[i] != null)
qs += "&" + escape(i) + "=" + this.params[i];
}
url += "format=json&url=" + escape(externalUrl) +
qs +
"&" + this.callbackparameter + "=?";
return url;
};
this.embedCode = function(container, externalUrl, callback) {
var request = this.getRequestUrl(externalUrl);
$.getJSON(request, function(data) {
var code, type = data.type;
var oembed = $.extend({ }, data);
switch (type) {
case "photo":
oembed.code = $.fn.oembed.getPhotoCode(externalUrl, data);
break;
case "video":
oembed.code = $.fn.oembed.getVideoCode(externalUrl, data);
break;
case "rich":
oembed.code = $.fn.oembed.getRichCode(externalUrl, data);
break;
default:
oembed.code = $.fn.oembed.getGenericCode(externalUrl, data);
break;
}
callback(container, oembed);
});
};
}
})(jQuery);