Can JavaScript Read the Source of Any Web Page

How do I get source code from a webpage?

Thanks to:

  • @PLB
  • @Shadow Wizard
  • Getting the source code of an iframe
  • http://www.frihost.com/forums/vt-32602.html
  • @Matt Coughlin.

First, you must know that you will never be able to get the source code of a page that is not on the same domain as your page in javascript. (See http://en.wikipedia.org/wiki/Same_origin_policy).

In PHP, this is how you do it :

file_get_contents($theUrl);

In javascript, there is three ways :

Firstly, by XMLHttpRequest : http://jsfiddle.net/635YY/1/

var url="../635YY",xmlhttp;//Remember, same domain
if("XMLHttpRequest" in window)xmlhttp=new XMLHttpRequest();
if("ActiveXObject" in window)xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
xmlhttp.open('GET',url,true);
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)alert(xmlhttp.responseText);
};
xmlhttp.send(null);

Secondly, by iFrames : http://jsfiddle.net/XYjuX/1/

var url="../XYjuX";//Remember, same domain
var iframe=document.createElement("iframe");
iframe.onload=function()
{
alert(iframe.contentWindow.document.body.innerHTML);
}
iframe.src=url;
iframe.style.display="none";
document.body.appendChild(iframe);

Thirdly, by jQuery : http://jsfiddle.net/edggD/2/

$.get('../edggD',function(data)//Remember, same domain
{
alert(data);
});

When I view the source code of a potentially malicious page, can the website harm my computer?

The general answer is to just disable javascript and cookies in your browser first.

  1. Generally yes it's ok to view source, especially if javscript is disabled prior.

  2. You can if their scripts are readable, many sites however will minimize the code, which is generally not very readable.

  3. If javascript is disabled it's likely that their tracking would not work or at best be incomplete.

How do I get the HTML source from the page?

Use

document.documentElement.outerHTML

or

document.documentElement.innerHTML

How to I find JavaScript source in web page?

In firefox open the desired page, press F12 to open the developer tools.
In the "debugger" tab press ctrl + shift + F (Search in files) and search for "jspaginate". It will present you all the files containing that word.

Firefox Developer Tools - Search

Firefox Developer Tools: search for files with the debugger (Video)


Or with FireBug that is no longer supported in Firefox >57*

Install firebug, activate it, activate the script panel too and then look for the jspaginate object. In a js file you will find it:

var jspaginate = {
data:{},
loading: false,
init: function(action, last){
var view = this,
target, current;
if(this.loading !== true){
view.loadingSequence();
if(action === 'first'){
target = 0;
view.update(target, 0);
}else if(action === 'prev'){
current = parseInt(view.data.pageIndex)-1;
target = (current)*view.data.range;
view.update(target, current);
}else if(action === 'next'){
current = parseInt(view.data.pageIndex)+1;
target = (current)*view.data.range;
view.update(target, current);
}else if(action === 'last'){
current = parseInt(last)-1;
target = (current)*view.data.range;
view.update(target, current);
}
}
},
update: function(target, current){
this.data.pageIndex = current;
this.pushState(target, current);
this.getData(target);
},
pushState: function(target, current){
var state = { 'page_id': current, 'user_id': 2 },
title = 'Page'+ current,
url = '?start='+target+'&tstart=0';
history.pushState(state, title, url);
},
loadingSequence: function(){
this.loading = true;
$j('.j-pagination').append('<div class="j-loading-big"><span></span></div>');
$j('.all-replies-container').css('opacity','.5');
},
removeLoading: function(){
$j('.j-loading-big').remove();
$j('.all-replies-container').css('opacity','1');
this.loading = false;
},
updateUI: function(data){
$j('.all-replies-container').html(data);
$j('html, body').animate({
scrollTop: ($j(".all-replies-container").offset().top -180)
}, 800);

this.removeLoading();
},
getData: function(target){
var view = this,
tId = (this.data.threadId).split('/')[2],
urlString = jive.app.url({path:'/inline-thread.jspa?thread='+tId+'&start='+target+'&tstart=0'});
$j.ajax({
url: urlString,
cache: true,
async: true,
type:'POST',
dataType : 'html'
}).success(function(data) {
view.updateUI(data);
}).error(function(data) {
console.log(data);
});
}
}
;


Related Topics



Leave a reply



Submit