Skip to content

Instantly share code, notes, and snippets.

@sergioro9
Last active July 23, 2020 10:02
Show Gist options
  • Save sergioro9/b1e452d2b74b9735352589b525fea290 to your computer and use it in GitHub Desktop.
Save sergioro9/b1e452d2b74b9735352589b525fea290 to your computer and use it in GitHub Desktop.
generic
/*
* Dowload all images in a site. I use it to download the first images for legendario.com
* source: so/19830088/
* example: downlaod free images from pixabar.com
* output: directory in ~/Downloads
*/
function getAllImages() {
images = document.querySelectorAll("img");
for (i of images) {
var a = document.createElement('a');
a.href = i.src;
console.log(i);
a.download = i.src;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
}
/*
* general purpose functions
*/
/*
* find if an element exist in a page
*
* return: true if the element exists
* param elm: string
*/
isEmpty = (elm) => (elm.length ? true : false)
/*
* return an ordinal number as a string e.g. '2nd' or '3rd'
* source: https://leancrew.com/all-this/2020/06/ordinal-numerals-and-javascript/
* param n: integer
*/
function ordinal(n) {
var s = ["th", "st", "nd", "rd"];
var v = n%100;
return n + (s[(v-20)%10] || s[v] || s[0]);
}
/*
* display the number of characaters in a textarea element
* source: so/14086398
* usage: displayCharacterLimitCount('#some-elm', '#other-elm')
* param src: element for which the characters will be counted
* param dest: element in which the characters will be displayed
*/
function displayCharacterLimitCount(src, dest) {
var msg = () => 'Characters: ' + $(src).val().length;
$(dest).text(msg);
$(src).keyup(function(){ $(dest).text(msg); });
}
/*
* set multiple attributes
* source: so/12274748
* usage: setAttributes(elm, {"src": "example.com", "height": "100%", ...});
*/
function setAttributes(elm, attrs) {
for(var key in attrs) {
elm.setAttribute(key, attrs[key]);
}
}
/*
* save current website in disk
* source: so/27177661
*/
function saveSite() {
var htmlContent = ["your-content-here"];
var bl = new Blob(htmlContent, {type: "text/html"});
var a = document.createElement("a");
a.href = URL.createObjectURL(bl);
a.download = "your-download-name-here.html";
a.hidden = true;
document.body.appendChild(a);
a.innerHTML = "something random - nobody will see this, it doesn't matter what you put here";
a.click();
}
/*
* concatenate and return the text of matching elements
* usage: concatAll('.klass')
* source: so/18949817
*/
function concatAll(elm) {
var elms = document.getElementsByClassName(elm);
var result = "";
for (var i = 0; i < elms.length; i++) {
var price = result.concat(elms[i].value + "\n");
}
return result;
}
/*
* get the number of sentences in an element by counting the number of dots
* source: so/4009756
*/
numberOfSentences = (elm) => (elm.textContent.match(/([.?!]\s|.$)/g) || [] ).length
/*
* get average number from of an array of numbers
* source: so/29544371
* examples:
* average(elms.map(elm => getNumberOfSentences(elm))) # get average number of sentences per paragraph
*/
average = (arr) => arr.reduce((a, b) => a + b) / arr.length;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment