如何查找维基百科页面的所有部分并将其全部显示?

时间:2016-11-09 14:42:16

标签: javascript jquery wikipedia-api wikimedia

维基百科页面按部分划分,因此如果我使用API​​并说出section=0,我将阅读本节中的文字。但是一个页面可能有很多部分,如何循环所有部分以加载它们?

This

  

我得到section=0 only

MediaWiki Docs

HTML

<button id="wiki" data-subject="Paris">Wikipedia</button>

<output id="results">
 <ul>
 </ul>
</output>

JS

    $("ul").on("click", "a", function(e) {
       e.preventDefault();
       e.stopPropagation();
    });

    $("#wiki").one('click', function(e) {
      var articleName = $(this).data('subject');
      $.getJSON("https://en.wikipedia.org/w/api.php?callback=?", {
          srsearch: articleName,
          action: "query",
          list: "search",
          format: "json"
      }, function(data) {
          $("#results ul").empty();
          $("#results ul").append("<h3>Results for <b>" + articleName + "</b></h3>").text();
          $.each(data.query.search, function(i, item) {
            $("#results").append("<div><a href='https://en.wikipedia.org/wiki/" + encodeURIComponent(item.title) + "' data-toggle='modal' data-target='.bs-example-modal-lg'>" + item.title + "</a><br>" + item.snippet + "</div");
            var myLink = $("#results a").attr("href");
            $("#results div a").attr("href", "#");
          });
          $('.modal').on('show.bs.modal', function (e) {
              $.getJSON("https://en.wikipedia.org/w/api.php?action=parse&section=0&prop=text&format=json&callback=?", {
                  page: e.relatedTarget.textContent
              }, function(data) {
                var markup = data.parse.text["*"];
                var blurb = $('<div></div>').html(markup);
                blurb.find('a').each(function() { 
                  $(this).replaceWith($(this).html()); 
                });
                blurb.find('sup').remove();
                blurb.find('.mw-ext-cite-error').remove();
                $(".modal-header .modal-title").html(articleName);
                $(".modal-header .modal-title").promise().done(function(){
                    $(".modal-title").css({opacity: 0.0, visibility: "visible"}).animate({opacity: 1.0});
                });
                $(".modal-body").html($(blurb).find("p"));
                $(".modal-body").promise().done(function(){
                    $(".modal-body").css({opacity: 0.0, visibility: "visible"}).animate({opacity: 1.0});
                                });
              });
          });
      });
  });

1 个答案:

答案 0 :(得分:0)

只需删除$.getJSON("https://it.wikipedia.org/w/api.php?format=json&action=parse&prop=text&callback=?", {... 即可:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<!-- Bootstrap JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>