我正在使用这个脚本为我的readmore hack生成图像缩略图一切正常但问题是没有脚本为没有图像的帖子添加默认图像而且出现的图像是具有defualt分辨率的但我想要的将其更改为最大值,即s1600。
function stripTags(s, n) {
return s.replace(/<.*?>/ig, "").split(/\s+/).slice(0, n - 1).join(" ")
}
function rm(a) {
var p = document.getElementById(a);
imgtag = "";
ifrtag = "";
ifrsrc = "";
ifrtb = -1;
var video;
var videoUrl;
var videoId;
var service = "";
var checkCase = "";
var thumbnailUrl;
img = p.getElementsByTagName("img");
console.log( a );
/* */
// The following will log the number of post in this particular post
var isImageSlider = 0;
var postImageSlider = ""
+ "<div class='post-image-slider'>"
+ "<ul class='rslides'>";
console.log( "Number of images in the post: " + img.length );
// the `img` variable is an array of images in the post, so we run a for loop to get all the images in that array
for (var i = 0; i < img.length; i++) {
var imgSrc = img[i].src;
console.log( "Image #" + i + " : " + imgSrc );
}
if ( img.length > 1 ) {
isImageSlider = 1;
for (var i = 0; i < img.length; i++) {
var imgSrc = img[i].src;
postImageSlider += "<li><img src='"+ imgSrc +"' alt=''></li>";
}
postImageSlider += "</ul></div>";
}
// This simply checks in there's more than one image
if ( img.length > 1 ) {
console.log("This post contains more than one image");
}
else if ( img.length < 1 ) {
console.log("This post contains no images at all");
}
else {
console.log("This post contains only one image");
}
/* deepak's code ends here */
ifr = p.getElementsByTagName("iframe");
for (var i = 0; i < ifr.length; i++) {
videoUrl = ifr[i].src;
if (videoUrl.indexOf("//www.youtube.com/embed/") != -1) {
ifrtb = i;
service = "youtube";
break
}
else if (videoUrl.indexOf("//player.vimeo.com/video/") != -1) {
ifrtb = i;
service = "vimeo";
break
}
else if (videoUrl.indexOf("//www.dailymotion.com/embed/video/") != -1) {
ifrtb = i;
service = "dailymotion";
break
}
}
videoId = check_id( videoUrl, service );
if ( service == "vimeo" ) {
thumbnailUrl = "http://i.vimeocdn.com/video/" + videoId + "_.jpg";
}
else if ( service == "youtube" ) {
thumbnailUrl = "http://img.youtube.com/vi/"+ videoId +"/maxresdefault.jpg";
}
else if ( service == "dailymotion" ) {
thumbnailUrl = "http://www.dailymotion.com/thumbnail/video/" + videoId;
}
if ( !isImageSlider ) {
if (ifrtb != -1) ifrtag = '<div class="block-image"><div class="thumb"><a href="' + y + '"><img src="'+ thumbnailUrl +'"/></a></div></div>';
else if (img.length >= 1) imgtag = '<div class="block-image"><div class="thumb"><a href="' + y + '"><img src="'+ img[0].src +'"/></a></div></div>';
else imgtag = '';
p.innerHTML = ifrtag + imgtag + '';
}
else {
ifrtag = '<div class="block-image"><div class="thumb slider-images-container">'+ postImageSlider +'</div></div>';
p.innerHTML = ifrtag + '';
}
}
function check_id( url, service ) {
var regExp;
if (service == "vimeo") {
regExp = /^.*(?:vimeo.com)\/(?:channels\/|channels\/\w+\/|groups\/[^\/]*\/videos\/|albu\/\d+\/video\/|video\/|)(\d+)(?:$|\/|\?)/;
var idArr = url.match( regExp );
if ( idArr[1] && parseInt(idArr[1]) ) {
return idArr[1];
}
else {
return 0;
}
}
else if (service == "youtube" ) {
var ID = '';
url = url.replace(/(>|<)/gi,'').split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);
if(url[2] !== undefined) {
ID = url[2].split(/[^0-9a-z_\-]/i);
ID = ID[0];
}
else {
ID = url;
}
return ID;
}
else if (service == "dailymotion" ) {
var m = url.match(/^.+dailymotion.com\/(video|hub|embed)\/([^_]+)[^#]*(#video=([^_&]+))?/);
if (m !== null) {
if(m[4] !== undefined) {
return m[4];
}
return m[2];
}
}
}
答案 0 :(得分:0)
要添加具有默认图像的功能,请替换以下代码
else imgtag = '';
与
else imgtag = '<div class="block-image"><div class="thumb"><a href="' + y + '"><img src="'+ defaultImageURL +'"/></a></div></div>';
并添加一个变量defaultImageURL
,其中包含指向默认图片的链接。
要将图像调整为更高分辨率,请替换以下代码 -
imgSrc = img[i].src
&安培;
img[0].src
分别使用以下代码 -
imgSrc = img[i].src.replace(/s\B\d{2,4}/,'s1600')
和
img[0].src.replace(/s\B\d{2,4}/,'s1600')