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.
– Rafael Salomão
Yes, it needs to be done in javascript. Because I’m not doing it for a website, but to use in the browser console.
– Francisco
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.
– Rafael Salomão