How do I get elements of a different page?

0

I'm creating a system to get the first 4 paragraphs of a given Wikipedia page and paste them into the div id="txtTextoResposta" of the page that the user is, but the problem I'm having is that I do not know how to do it content from another site.

My code:

function resp(){
    var input = prompt();
    var wiki = document.download("https://pt.wikipedia.org/w/index.php?search=" + input);
    var x = wiki.getElementById("mw-content-text").getElementsByTagName("p");
    var linhas = x.length > 4 ? 5 : x.length;
    for (var i = 0; i < linhas; i++){
        document.getElementById("txtTextoResposta").innerHTML += x[i].innerText;
    }
}

I invented the document.download() function to better illustrate what I want to do.

I also tried using jQuery, like this:

javascript: (function(e, s) {
    e.src = s;
    e.onload = function() {
        jQuery.noConflict();
        console.log('jQuery injected');
    };
    document.head.appendChild(e);
})(document.createElement('script'), '//code.jquery.com/jquery-latest.min.js')

$.get("https://pt.wikipedia.org/w/index.php?search=" + input, function(data) { $(".result").html(data); alert("Load was performed."); });
//Neste caso, o input tinha o valor "Kant"

But I get the following error:

  

XMLHttpRequest can not load link . In 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' link ' is therefore not allowed access.

    
asked by anonymous 11.07.2017 / 00:42

2 answers

0

I do not know if this would help, but you could make a request to the external url with jquery

$. get ("url", function (data) {     $ (".result") .html (data);     alert ("Load was performed.");   });

get method

Then using the callback to read the content with DOM, I hope this answer is useful!

Reference to the use of DOM

    
11.07.2017 / 00:56
0

According to the Wikipedia documentation, available at link :

  

For anonymous requests, origin query string parameter can be set to *   which will allow requests from anywhere.

To make anonymous requests, you must use a query origin=* .

So it works if your wiki variable is defined as follows:

var wiki = document.download("https://pt.wikipedia.org/w/index.php?origin=*&search=" + input);
    
11.07.2017 / 03:50