Friends, I need to get the link of the src <iframe> which lies within the <description>. How do I?
My xml:
<description><![CDATA[<img src='http://parafernalha.s3.amazonaws.com/images/logo_parafernalha.png' /><br /><br /><h2>MAIS UMA COISA #2 Crítica de Batman - The Dark Knight Rises</h2><iframe width='640' height='390' src='http://www.youtube.com/embed/Yq7luhnbl2w' frameborder='0' allowfullscreen='true'></iframe>]]></description>
I’m doing well, but does not work.
item.description.iframe[“src”]
Can we see what you have coded so far?
It’s an app for Chrome.
background:
var feedUrl = 'http://feeds.feedburner.com/parafernalhavideos';
var reset = function(){
console.log('reset');
localStorage.clear();
setLocalStorage();
lidoFeed();
}
var setLocalStorage = function(){
if(typeof localStorage.items == 'undefined'){
console.log('set localStorage');
localStorage.items = '';
localStorage.lido = '[]';
}
}
var setLido = function(lidoId){
console.log('lido ' + lidoId);
var lido = JSON.parse(localStorage.lido);
var items = JSON.parse(localStorage.items);
var newItems= 0;
lido.push(lidoId.toString());
$.each(items, function(lauf, item){
if(item.id == lidoId){
console.log('ok');
item.naolido = 0;
}
console.log(item);
if(item.naolido == 1){
newItems++;
}
});
if(lido.length > 10){
lido.shift();
}
setBadgeText(newItems);
console.log(lido);
localStorage.lido = JSON.stringify(lido);
localStorage.items = JSON.stringify(items);
}
var lidoAll = function(){
var items = JSON.parse(localStorage.items);
$.each(items, function(lauf, item){
setLido(item.id);
});
}
var getFeed = function(){
var items = JSON.parse(localStorage.items);
return items;
}
var setBadgeText = function(count){
if(count > 0){
chrome.browserAction.setBadgeText({'text': count.toString()});
}else{
chrome.browserAction.setBadgeText({'text': ''});
}
}
var lidoFeed = function(){
console.log('inicia lidoFeed');
$.getFeed({
url: feedUrl,
success: function(feed) {
console.log('compute Feed: ' + feed.title);
var items = new Array();
var lido = JSON.parse(localStorage.lido);
var newItems= 0;
$.each(feed.items, function(lauf, item){
if($.inArray(item.id, lido) > -1){
naolido = 0;
console.log('lido');
} else {
naolido = 1;
console.log('naolido');
newItems++;
}
items.push({'id': item.id, 'title': item.title, 'link': item.link, 'description': item.description, 'naolido': naolido});
console.log(item.title + ' ' + item.id);
});
console.log(items);
console.log(newItems);
localStorage.items = JSON.stringify(items);
setBadgeText(newItems);
setTimeout(lidoFeed, 900000);
}
});
}
$(function(){
console.log('start script');
setLocalStorage();
lidoFeed();
});
popup:
jQuery.each(bkg.getFeed(), function(id, item){
if(item.naolido == '1') lidoClass = 'naolido';
else lidoClass = 'lido';
$('#feed').append('<li>'+item.description.iframe["src"]+'<a id="' + item.id + '" href="' + item.link + '" class="' + lidoClass + '">' + item.title + '</a></li>');
});
Code for grabbing the URL of the iframe
function GrabIFrameURL(feedDescription)
{
var regex = new RegExp("iframe(?:.*?)src='(.*?)'", 'g');
var matches = regex.exec(feedDescription);
var url = '';
if (matches.length == 2)
{
url = matches[1];
}
return url;
}
Implementation:
jQuery.each(bkg.getFeed(), function(id, item){
if(item.naolido == '1') lidoClass = 'naolido';
else lidoClass = 'lido';
$('#feed').append('<li>'+GrabIFrameURL(item.description)+'<a id="' + item.id + '" href="' + item.link + '" class="' + lidoClass + '">' + item.title + '</a></li>');
});
It worked, but now have another problem, I’m using a class to generate thumbnail of videos from youtube. The problem is that the thumbnails are showing everyone in the first div. See:
<li>
<a id="thumbs" href="http://www.parafernalha.com.br/video/Nostalgia---Digimon"><img src="http://img.youtube.com/vi/eXVNBnDozjk/default.jpg"><img src="http://img.youtube.com/vi/PX0GQasWIOE/default.jpg"><img src="http://img.youtube.com/vi/JO1jU8m0BLA/default.jpg"><img src="http://img.youtube.com/vi/Yq7luhnbl2w/default.jpg"><img src="http://img.youtube.com/vi/C8WwKwKPAqo/default.jpg"><img src="http://img.youtube.com/vi/Bqc2AUvfl0s/default.jpg"><img src="http://img.youtube.com/vi/Vu3xk-olBDA/default.jpg"><img src="http://img.youtube.com/vi/xf0ClPEPWPI/default.jpg"><img src="http://img.youtube.com/vi/Nqctbfzm5Uc/default.jpg"></a><a id="http://www.parafernalha.com.br/video/Nostalgia---Digimon" href="http://www.parafernalha.com.br/video/Nostalgia---Digimon" class="naolido">Nostalgia - Digimon</a>
</li>
class:
(function($){
$.extend({
jYoutube: function( url, size ){
if(url === null){ return ""; }
size = (size === null) ? "big" : size;
var vid;
var results;
results = url.match("[\\\\?&]v=([^&#]*)");
vid = ( results === null ) ? url : results[1];
if(size == "small"){
return "http://img.youtube.com/vi/"+vid+"/default.jpg";
}else {
return "http://img.youtube.com/vi/"+vid+"/0.jpg";
}
}
})
})(jQuery);
Can you show me the code that calls “jYoutube” passing in the url and size?
jQuery.each(bkg.getFeed(), function(id, item){
if(item.naolido == '1') lidoClass = 'naolido';
else lidoClass = 'lido';
var thumb = GrabIFrameURL(item.description);
$('#thumbs').append($('<img src="'+$.jYoutube(thumb, 'small')+'" />'));
$('#feed').append('<li><a id="thumbs" href="'+ item.link +'"></a><a id="' + item.id + '" href="' + item.link + '" class="' + lidoClass + '">' + item.title + '</a></li>');
});
Ah, okay, because your output HTML is invalid.
Try this
jQuery.each(bkg.getFeed(), function(id, item){
if(item.naolido == '1') lidoClass = 'naolido';
else lidoClass = 'lido';
var thumb = GrabIFrameURL(item.description);
$('#feed').append('<li><a id="thumbs' + item.id + '" href="'+ item.link +'"></a><a id="' + item.id + '" href="' + item.link + '" class="' + lidoClass + '">' + item.title + '</a></li>');
$('#thumbs' + item.id).append($('<img src="'+$.jYoutube(thumb, 'small')+'" />'));
});
Do not return any error but did not work.
<li><a id="thumbshttp://www.parafernalha.com.br/video/Nostalgia---Digimon" href="http://www.parafernalha.com.br/video/Nostalgia---Digimon"></a><a id="http://www.parafernalha.com.br/video/Nostalgia---Digimon" href="http://www.parafernalha.com.br/video/Nostalgia---Digimon" class="naolido">Nostalgia - Digimon</a></li>
Okay, my bad, didn’t realize that item.id was a url.
Try this
var itemId = 0;
jQuery.each(bkg.getFeed(), function(id, item){
if(item.naolido == '1') lidoClass = 'naolido';
else lidoClass = 'lido';
var thumb = GrabIFrameURL(item.description);
$('#feed').append('<li><a id="thumbs' + itemId + '" href="'+ item.link +'"></a><a id="' + item.id + '" href="' + item.link + '" class="' + lidoClass + '">' + item.title + '</a></li>');
$('#thumbs' + itemId).append($('<img src="'+$.jYoutube(thumb, 'small')+'" />'));
itemId++;
});
Not sure why I didn’t think of this sooner, but why do a second append, when you can avoid it all together.
This code should also work:
jQuery.each(bkg.getFeed(), function(id, item){
if(item.naolido == '1') lidoClass = 'naolido';
else lidoClass = 'lido';
var thumb = GrabIFrameURL(item.description);
$('#feed').append('<li><a href="'+ item.link +'"><img src="'+$.jYoutube(thumb, 'small')+'" /></a><a id="' + item.id + '" href="' + item.link + '" class="' + lidoClass + '">' + item.title + '</a></li>');
});