[英]Javascript function for RSS JSON Display not getting the right profile picture
I wanted to get the profile photos per post of this site ( https://medium.com/@femalefounderssg ) using this javascript function but I am not getting the right photo. 我想使用此javascript函数获取该网站每个帖子( https://medium.com/@femalefounderssg )的个人资料照片,但我没有得到正确的照片。 Please guide me in changing my code for me to get the appropriate photo.
请指导我更改代码,以获取适当的照片。 Thank you.
谢谢。 Here is the code.
这是代码。
$(function () {
var $content = $('#jsonContent');
var data = {
rss_url: 'https://medium.com/feed/@femalefounderssg'
};
$.get('https://api.rss2json.com/v1/api.json', data, function (response) {
if (response.status == 'ok') {
var output = '';
$.each(response.items, function (k, item) {
var visibleSm;
if(k < 3){
visibleSm = '';
} else {
visibleSm = ' visible-sm';
}
output += '<div class="col-sm-6 col-md-4' + visibleSm + '">';
output += '<div class="blog-post"><header>';
output += '<h4 class="date">' + $.format.date(item.pubDate, "dd<br>MMM") + "</h4>";
var tagIndex = item.description.indexOf('<img'); // Find where the img tag starts
var srcIndex = item.description.substring(tagIndex).indexOf('src=') + tagIndex; // Find where the src attribute starts
var srcStart = srcIndex + 5; // Find where the actual image URL starts; 5 for the length of 'src="'
var srcEnd = item.description.substring(srcStart).indexOf('"') + srcStart; // Find where the URL ends
var src = item.description.substring(srcStart, srcEnd); // Extract just the URL
output += '<div class="blog-element"><img class="img-responsive" src="' + src + '" width="360px" height="240px"></div></header>';
output += '<div class="blog-content"><h4><a href="'+ item.link + '">' + item.title + '</a></h4>';
output += '<div class="post-meta"><span>By ' + item.author + '</span></div>';
var yourString = item.description.replace(/<img[^>]*>/g,""); //replace with your string.
var maxLength = 120 // maximum number of characters to extract
//trim the string to the maximum length
var trimmedString = yourString.substr(0, maxLength);
//re-trim if we are in the middle of a word
trimmedString = trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" ")))
output += '<p>' + trimmedString + '...</p>';
output += '</div></div></div>';
return k < 3;
});
$content.html(output);
}
});
});
Current Profile Output: 当前配置文件输出:
Desired Profile Output: 所需的配置文件输出:
You are getting the wrong image because the first result you get when searching for <img
index is the author's image. 您得到的图像不正确,因为搜索
<img
索引时获得的第一个结果是作者的图像。
You need to skip the first result and get the second one: 您需要跳过第一个结果并获得第二个结果:
Finding second occurrence of a substring in a string in Java [it's Java, but the concept remains] 在Java字符串中查找子字符串的第二次出现 [是Java,但概念仍然存在]
var tagIndex = item.description.indexOf('<img', item.description.indexOf('<img') + 1);
String#indexOf
supports a second argument as "fromIndex". String#indexOf
支持第二个参数作为“ fromIndex”。 By adding 1 to the index of the first result we make sure that the first <img
will not match, and instead the second <img
will. 通过将1添加到第一个结果的索引中,我们确保第一个
<img
不匹配,而第二个<img
将匹配。
Simplified JSFiddle: https://jsfiddle.net/yuriy636/03n1hffh/ 简化的JSFiddle: https ://jsfiddle.net/yuriy636/03n1hffh/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.