вторник, 20 августа 2013 г.

JSONP API: vk, wikipedia, Flickr


VK используя JSONP API


<script>
$(function (){
var textto = '';
$.getJSON('http://api.vk.com/method/wall.get?owner_id=-47211921&filter=owner&count=2&callback=?', function (data) {
    for (var i = 1; i < 10; i++) {
        $("#to").append(data.response[i].text);
    }
});
 
});  
</script>
<p id="to"></p>




Flickr используя JSONP API




<script>
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
  {
    tags: "cat",
    tagmode: "any",
    format: "json"
  },
  function(data) {
    $.each(data.items, function(i,item){
      $("<img/>").attr("src", item.media.m).appendTo("#images");
      if ( i == 3 ) return false;
    });
  });
</script>
<div id="images"></div>










WIKIPEDIA используя JSONP API


<script>
(function($) {
 $.fn.TexWikiPlugin =function(){
var url = "http://ru.wikipedia.org/wiki/Третьякова_Лена";
var title = url.split("/");
title = title[title.length - 1];
var url2 = "http://ru.wikipedia.org/wiki/Третьякова_Лена (группа)";
var title2 = url.split("/");
title2 = title2[title2.length - 1];
try
{
//Get Leading paragraphs (section 0)
$.getJSON("http://ru.wikipedia.org/w/api.php?action=parse&page=" + title + "&prop=text&section=0&format=json&callback=?", function (data) {
for (text in data.parse.text) {
var text = data.parse.text[text].split("<p>");
var pText = "";
for (p in text) {
//Remove html comment
text[p] = text[p].split('<!--');
if (text[p].length > 1) {
text[p][0] = text[p][0].split(/\r\n|\r|\n/);
text[p][0] = text[p][0][0];
text[p][0] += '</p> ';
}
text[p] = text[p][0];
//Construct a string from paragraphs
if (text[p].indexOf('</p>') == text[p].length - 5) {
var htmlStrip = text[p].replace(/<(?:.|\n)*?>/gm, '') //Remove HTML
var splitNewline = htmlStrip.split(/\r\n|\r|\n/); //Split on newlines
for (newline in splitNewline) {
if (splitNewline[newline].substring(0, 11) != "Site error:") {
pText += splitNewline[newline];
pText += '\n';
}
}
}
}
pText = pText.substring(0, pText.length - 2); //Remove extra newline
pText = pText.replace(/\[\d+\]/g, ''); //Remove reference tags (e.x. [1], [4], etc)

document.getElementById('div_text').innerHTML = pText
}
});
}
catch(err)
{
console.log('Исключение: '+err.description);
}
 return this;
 };
 setTimeout(function() {  
        $.fn.TexWikiPlugin();
    }, 1500);
})(jQuery);
</script>
<div id="div_text"></div>

Комментариев нет:

Отправить комментарий

Постоянные читатели

Популярные сообщения