How to get elements from a different page?

Asked

Viewed 59 times

0

I’m creating a system to take the first 4 paragraphs of a particular Wikipedia page and paste them into div id="txtTextoResposta" of the page the user is, however the problem I am having, is that I do not know how to get the content of 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 function document.download() to illustrate better what I want to do.

I also tried using jQuery this way:

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 the following error returns to me:

Xmlhttprequest cannot load https://pt.wikipedia.org/wiki/Immanuel_Kant. No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'http://www.educacional.com.br' is therefore not allowed access.

  • You want to do this with javascript? You don’t want to cache these results so you don’t have to run this Grab routine over and over for the same term ? May I suggest a solution in PHP.

  • Yes, it needs to be done in javascript. Because I’m not doing it for a website, but to use in the browser console.

  • But you could use an ajax to backend upload with PHP using XPATH and DOM and return the response to the javascript you would print on the console.

2 answers

0

I don’t know if this would help, but you could request an external url with jquery

$.get( "url", Function( date ) { $( ".result" ).html( date ); Alert( "Load was performed." ); });

Get method

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

Reference to the use of GIFT

  • I edited my question, see the error that returns me.

  • It’s I figured that would give the browser blocked for security. But I wasn’t sure. In this case both by performance and by security restrictions I advise ajax for a PHP.

  • Yeah, but like I said, there’s no way, because I’m going to use this on the Chrome developer console and not on a website.

0

According to the Wikipedia documentation, available at https://www.mediawiki.org/wiki/Manual:CORS:

For Anonymous requests, origin query string Parameter can be set to * which will allow requests from Anywhere.

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

So, it works if your wiki variable is set as follows:

var wiki = document.download("https://pt.wikipedia.org/w/index.php?origin=*&search=" + input);
  • Did not work, returns the same error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.