javascript Cheatsheet¶
javaScript, often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, often incorporating third-party libraries.
Print message to the console¶
Autoscroll entire page¶
var scrollInterval = setInterval(function() {
document.documentElement.scrollTop = document.documentElement.scrollHeight;
}, 50);
Extract all urls from a website and open a new tab with the output¶
var x = document.querySelectorAll("a");
var myarray = []
for (var i=0; i<x.length; i++){
var nametext = x[i].textContent;
var cleantext = nametext.replace(/\s+/g, ' ').trim();
var cleanlink = x[i].href;
myarray.push([cleantext,cleanlink]);
};
function make_table() {
var table = '<table><thead><th>Name</th><th>Links</th></thead><tbody>';
for (var i=0; i<myarray.length; i++) {
table += '<tr><td>'+ myarray[i][0] + '</td><td>'+myarray[i][1]+'</td></tr>';
};
var w = window.open("");
w.document.write(table);
}
make_table()
Extract all urls in console¶
var urls = document.getElementsByTagName('a');
for (url in urls) {
console.log ( urls[url].href );
}
Change colors to black/green¶
var allDivs = document.querySelectorAll('div');
for(var i = 0; i < allDivs.length; i++){
allDivs[i].style['background-color'] = 'black';
allDivs[i].style['color'] = 'green';
allDivs[i].style['font-family'] = 'Monospace';
}
Grab XSS in url¶
Rotate website in 180 degrees within 3s¶
setTimeout(function(){
document.onmousemove = document.onkeypress =
function(){
document.body.style['transition'] = 'transform 3s';
document.body.style['transform'] = 'rotate(180deg)';
}
}, 5000);
Read text with newlines¶
var text = this.responseText;
var lines = text.split("\n");
var formattedText = "";
for (var i = 0; i < lines.length; i++) {
formattedText += lines[i] + "<br>";
}