Last active
April 6, 2022 13:07
-
-
Save codenameyau/9f0a26a0f1be35a6784a to your computer and use it in GitHub Desktop.
Javascript Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************************ | |
* CLIENT ONE-LINERS | |
*************************************************************************/ | |
// Set top level domain cookie. | |
document.cookie = "token=12345; Max-Age=120; Secure; Domain=mozilla.org; Path=/;" | |
// Set a subdomain cookie. | |
document.cookie = "name=hello world; Max-Age=120; Secure; Domain=developer.mozilla.org; Path=/;" | |
// You can have cookies with the same name but with different paths and domains. | |
document.cookie = "name=1; Max-Age=60; Secure; Domain=mozilla.org;" | |
document.cookie = "name=2; Max-Age=60; Secure; Domain=mozilla.org; Path=/" | |
document.cookie = "name=3; Max-Age=60; Secure; Domain=developer.mozilla.org; Path=/;" | |
// Copies variable. | |
copy(temp1) | |
copy(JSON.stringify(temp1)) | |
// Capture regex groups with variable | |
const string = 'ayyyy lmao' | |
const rgx = /(?<firstVowel>[aeiou])/ | |
string.match(rgx).groups.firstVowel | |
// Copies current selection to clipboard. | |
// https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand | |
// https://hackernoon.com/copying-text-to-clipboard-with-javascript-df4d4988697f | |
document.execCommand('copy') | |
// Copies text content of click to clipboard. | |
navigator.clipboard && navigator.clipboard.writeText(event.target.textContent); | |
// Makes page text editable. | |
document.designMode = 'on' | |
// Access third party frames on window. | |
window.frames | |
// Create bookmark and set this to URL to skip ads. | |
javascript:void(document.querySelector('video').currentTime = document.querySelector('video').duration) | |
// See all event listeners. Remember to remove listener when unmounting. | |
getEventListeners(document) | |
// See Slack Emoji authors. | |
Array.from(document.querySelectorAll('.author_cell > a')).map((a) => a.text.trim()).sort() | |
// Use this to click all links. | |
Array.from(document.querySelectorAll('a.block')).forEach(a => a.click()) | |
// Grab all the images from a page (namely instagram) | |
Array.from(document.images).forEach(img => console.log(img.getAttribute('src'))) | |
// Like all facebook posts. | |
Array.from(document.body.querySelectorAll('.UFILikeLink')).forEach((a) => a.click()) | |
// Selecting wildcard classes. | |
Array.from(document.querySelectorAll('div[class^="ManageSection__column-left"] .dp-checkbox')).forEach((a) => a.click()) | |
/************************************************************************ | |
* SERVER ONE-LINERS | |
*************************************************************************/ | |
// Use Now to deploying static app serving a single page app. | |
ns ./build --cmd 'list ./content -s' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Checks if nested property exists. | |
db.drafts.find({"collection_type": "web_advertising", "email": "jorge@email.com", "rows.222": { $exists: true } }); | |
// Grabs the count. | |
db.drafts.find({"email": "jorge@email.com"}).count(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************** | |
* UTILS | |
*********************************************************************/ | |
// https://stackoverflow.com/q/779379 | |
setTimeout(function (callback) { | |
// Offload Heavy computation. | |
}, 0); | |
/******************************************************************** | |
* JQUERY serializeArray | |
*********************************************************************/ | |
// Converts form into Array of Objects. | |
// 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 (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; | |
} | |
// Nodejs check if module is called directly | |
const CALLED_DIRECTLY = require.main === module; | |
function splitTextarea(text) { | |
return text.trim().replace(/\s+/, '\n').replace(/\n{2,}/, '').split('\n'); | |
} | |
// Download using fetch API.. | |
import download from 'downloadjs'; | |
const handleBufferDownload = (filename) => { | |
res.arrayBuffer() | |
.then((buffer) => { | |
download(buffer, filename, contentType); | |
stopLoading(); | |
}); | |
}; | |
// http://stackoverflow.com/a/23842171 | |
encodeURIComparison = function() { | |
var arr = []; | |
for(var i=0; i<256; i++) { | |
var char=String.fromCharCode(i); | |
if(encodeURI(char) !== encodeURIComponent(char)) { | |
arr.push({ | |
character: char, | |
encodeURI: encodeURI(char), | |
encodeURIComponent: encodeURIComponent(char) | |
}); | |
} | |
} | |
console.table(arr); | |
}; | |
function encodeQueryParams (url, queryParams) { | |
return Object.keys(queryParams).reduce((a, key) => { | |
a.push(key + '=' + encodeURIComponent(queryParams[key])); | |
return a; | |
}, []).join('&'); | |
} | |
let queryString = encodeQueryParams(queryParams); | |
url += url.indexOf('?') > -1 ? '&' : '?' + queryString; | |
/******************************************************************** | |
* JQUERY COLOR CONTRAST | |
*********************************************************************/ | |
jQuery.Color.fn.contrastColor = function() { | |
var r = this._rgba[0], g = this._rgba[1], b = this._rgba[2]; | |
return (((r*299)+(g*587)+(b*144))/1000) >= 131.5 ? "black" : "white"; | |
}; | |
// usage examples: | |
jQuery.Color("#bada55").contrastColor(); // "black" | |
element.css( "color", jQuery.Color( element, "backgroundColor" ).contrastColor() ); | |
Opening chrome with CORS disabled
open -n -a /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --args --user-data-dir="/tmp/chrome_dev_test" --disable-web-security
Mongo DB data migration
Binary
# Import database
mongorestore -h ds141168.mlab.com:41168 -d heroku_nl79h610 -u <user> -p <password> <input db directory>
# Export database
mongodump -h ds141168.mlab.com:41168 -d heroku_nl79h610 -u <user> -p <password> -o <output directory>
# Import collection
mongorestore -h ds141168.mlab.com:41168 -d heroku_nl79h610 -u <user> -p <password> <input .bson file>
# Export collection
mongodump -h ds141168.mlab.com:41168 -d heroku_nl79h610 -c <collection> -u <user> -p <password> -o <output directory>
JSON
# Import collection
mongoimport -h ds141168.mlab.com:41168 -d heroku_nl79h610 -c <collection> -u <user> -p <password> --file <input file>
# Export collection
mongoexport -h ds141168.mlab.com:41168 -d heroku_nl79h610 -c <collection> -u <user> -p <password> -o <output file>
CSV
# Import collection
mongoimport -h ds141168.mlab.com:41168 -d heroku_nl79h610 -c <collection> -u <user> -p <password> --file <input .csv file> --type csv --headerline
# Export collection
mongoexport -h ds141168.mlab.com:41168 -d heroku_nl79h610 -c <collection> -u <user> -p <password> -o <output .csv file> --csv -f <comma-separated list of field names>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
VSCode debugger with CRA tests
Sell bots
https://botbroker.io/bots
Mocking modules