6 Snippets to Keep in Your Chrome DevTools
Plain JavaScript - Functions and Helpers
including personal scripts
var storageKey = 'overlay-default';
var overlayDefault = localStorage.getItem(storageKey) || '';
var url = prompt('paste overlay url', overlayDefault);
if (url) {
localStorage.setItem(storageKey, url);
var overlay = document.createElement('div');
overlay.style.position = 'absolute';
overlay.style.left = 0;
overlay.style.top = 0;
overlay.style.width = '100%';
overlay.style.height='100%';
overlay.style.backgroundImage = 'url(' + url + ')';
overlay.style.backgroundSize = 'cover';
overlay.style.zIndex=10000;
overlay.style.opacity = 0.5;
document.body.appendChild(overlay);
}
// dataurl.js
// https://github.com/bgrins/devtools-snippets
// Print out data URLs for all images / canvases on the page.
(function() {
console.group("Data URLs");
[].forEach.call(document.querySelectorAll("img"), function(i) {
var c = document.createElement("canvas");
var ctx = c.getContext("2d");
c.width = i.width;
c.height = i.height;
try {
ctx.drawImage(i, 0, 0);
console.log(i, c.toDataURL());
}
catch(e) {
console.log(i, "No Permission - try opening this image in a new tab and running the snippet again?", i.src);
}
});
[].forEach.call(document.querySelectorAll("canvas"), function(c) {
try {
console.log(c, c.toDataURL());
}
catch(e) {
console.log(c, "No Permission");
}
});
console.groupEnd("Data URLs");
})();
function serializeArray(form) {
var field, l, s = [];
if (typeof form == 'object' && form.nodeName == "FORM") {
var len = form.elements.length;
for (var i=0; i<len; i++) {
field = form.elements[i];
if (field.name && !field.disabled && field.type != 'file' && field.type != 'reset' && field.type != 'submit' && field.type != 'button') {
if (field.type == 'select-multiple') {
l = form.elements[i].options.length;
for (j=0; j<l; j++) {
if(field.options[j].selected)
s[s.length] = { name: field.name, value: field.options[j].value };
}
} else if ((field.type != 'checkbox' && field.type != 'radio') || field.checked) {
s[s.length] = { name: field.name, value: field.value };
}
}
}
}
return s;
}
var className = 'grid-link__title';
var htmlClassElements = document.getElementsByClassName(className);
var classElements = Array.from( htmlClassElements );
copy(classElements.map(ce => ce.innerText));
Combination of javascript and powershell
document.querySelectorAll(".js-lazy-image").length
var nodes = document.querySelectorAll(".js-lazy-image");
images = nodeArray.map( na => na.getAttribute('data-superjumboimage') )
copy(images) // export to clipboard
## $images | % { $uri = ( [System.Uri]$_ );}
$images | ? { $_.Length -gt 0 } | % { $uri = $_ ;$fileName = $uri.Substring($uri.LastIndexOf("/")+1) ;$fileName ; invoke-webrequest $uri -outfile $fileName }
// On a github pull request
(function(){
function GetNodesByQuery(querySelector) {
const nodes = document.querySelectorAll(querySelector);
return Array.from(nodes);
}
function AddCheckboxToElement(element) {
var checkbox = document.createElement('input');
checkbox.setAttribute('type', 'checkbox');
element.appendChild(checkbox);
}
function AddCheckboxesToGithubCommitMessages(){
var commitMessages = GetNodesByQuery('.commit-message') ;
commitMessages.forEach(cm => AddCheckboxToElement(cm));
}
AddCheckboxesToGithubCommitMessages();
})();
(function(){
function GetNodesByQuery(querySelector) {
const nodes = document.querySelectorAll(querySelector);
return Array.from(nodes);
}
function CreateCheckbox() {
var checkbox = document.createElement('input');
checkbox.setAttribute('type', 'checkbox');
return checkbox;
}
function CreateTextInput() {
var input = document.createElement('input');
input.setAttribute('type', 'text'); // input.type = 'text'
// input.setAttribute('name', 'somethingunique');
return input;
}
var elements = GetNodesByQuery('.file-info') ;
elements.forEach(el => {
el.appendChild(CreateCheckbox());
el.appendChild(CreateTextInput()); // .style.backgroundColor = "#f5360d"
});
})();
// [How to get a list of all JavaScript scripts used on each page of a site - Quora] (https://www.quora.com/How-can-I-get-a-list-of-all-JavaScript-scripts-used-on-each-page-of-a-site)
[].forEach.call( document.querySelectorAll("script[src]"), function( src ) {
console.log( src );
});
//# Serialize form data into an array <https://plainjs.com/javascript/ajax/serialize-form-data-into-an-array-46/>
function serializeArray(form) {
var field, l, s = [];
if (typeof form == 'object' && form.nodeName == "FORM") {
var len = form.elements.length;
for (i=0; i<len; i++) {
field = form.elements[i];
if (field.name && !field.disabled && field.type != 'file' && field.type != 'reset' && field.type != 'submit' && field.type != 'button') {
if (field.type == 'select-multiple') {
l = form.elements[i].options.length;
for (j=0; j<l; j++) {
if(field.options[j].selected)
s[s.length] = { name: field.name, value: field.options[j].value };
}
} else if ((field.type != 'checkbox' && field.type != 'radio') || field.checked) {
s[s.length] = { name: field.name, value: field.value };
}
}
}
}
return s;
}
var inputArray = serializeArray( document.getElementsByTagName("form")[0]);
/*
$.each(inputarray, function(i, val) { $('#'+val.name).val(val.value); });
*/
$inputs = document.querySelectorAll("input.currency");
$inputArray = Array.from($inputs)
$inputArray.forEach(inp => inp.value = "13.00");