Skip to content

Instantly share code, notes, and snippets.

View omrilotan's full-sized avatar

omrilotan omrilotan

View GitHub Profile
@omrilotan
omrilotan / Rails-status-codes-cheatsheet.md
Last active May 16, 2024 15:17
Rails status codes cheatsheet - A list of rails symbols for status codes
Code Message Symbol
1xx Informational
100 Continue :continue
101 Switching Protocols :switching_protocols
102 Processing :processing
2xx Success
200 OK :ok
@omrilotan
omrilotan / build-docs-to-gh-pages.sh
Last active January 13, 2019 20:56
Autometed pushing of new versions of docs to gh-pages
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# Continue only on 'master'
if [[ $CURRENT_BRANCH -ne 'master' ]]; then
exit;
fi
# Clone the branch and remove it's content
git clone -b gh-pages --single-branch https://github.com/<ORGENISATION>/<REPOSITORY>.git DOCS
rm -rf DOCS/*
@omrilotan
omrilotan / git-tag.js
Last active May 23, 2020 16:13
Create a tag by the last commit's author with it's message (example for async getters)
const asyncExec = require('util').promisify(require('child_process').exec);
/**
* Execute command line in a child process
* @param {...String} args Commands
* @return {String}
*/
async function exec (...args) {
const { stdout, stderr } = await asyncExec(...args);
@omrilotan
omrilotan / Example.js
Last active March 9, 2019 19:36
Memoize mixin for Javascript objects
class Person {
constructor(firstName, lastName) {
Object.assign(this, {firstName, lastName});
memoize.call(this, 'name', 'greeting', 'greet');
}
get name() {
return [this.firstName, this.lastName].filter(part => !!part).join(' ');
}
@omrilotan
omrilotan / Execution time monitoring.md
Last active March 30, 2019 12:25
Monitor performance of injected scripts (execution time)

Measure execution time of scripts injected to browser runtime

We override (monkey-patch) native Node insertion to the DOM, and wrap its content with performance marks. Later we send the duration of each execution along with the source domain of the script.

@omrilotan
omrilotan / contact-form-submit-override.js
Last active July 16, 2019 15:45
Override forms' submit and send the form here, instead (https://github.com/omrilotan/contact-form-example)
/**
* Send the form over http
* @param {HTMLFormElement} form
* @return {String|undefined} Error message or nothing
*
* Prevent form's organic submit, and send it here, instead
*/
export default async function send(target) {
try {
const url = target.getAttribute('action');
@omrilotan
omrilotan / monitorHeap.js
Created November 7, 2019 20:28
Monitor heap usage of a web page. Throw error when heap exceeds its limitations.
function monitorHeap({MAX_BYTES = 100, MAX_PERCENT = 90} = {}) {
if (!window.performance || !window.performance.memory || !window.requestAnimationFrame) { return; }
const MB = 1048576;
const MAX_MEMORY = MAX_BYTES * MB;
const MAX_PERCENTILE = MAX_PERCENT / 100;
function heapcop() {
const { usedJSHeapSize, jsHeapSizeLimit } = performance.memory;
const { onerror } = window; // Existing onerror handlers
// Trust others adhere to onerror handling rules
window.onerror = (...args) => {
let handled; // is someone else taking care this error?
try {
handled = onerror && onerror.apply(window, args);
} catch (error) {
// Catch others' onerror errors
const errorsHistory = [];
function abortErrorReport(message, file, line, column, error) {
// Close the log behind a rollout mechanism to protect your infrastructure
if (!errorLoggingEnabled) return true;
// Limit the amount of errors from one page
if (errorsHistory.length > 10) return true;
// Send the same error twice from the same page can create false multiplications
/**
* This is the signature of the browser's built in onerror handler
* @param {string} message Error message
* @param {string} file Source file where the script threw an error
* @param {number} line Line number
* @param {number} column Column number
* @param {Error} error Error object
* @return {Boolean} Should the default event handler fire?
*/
function myOnErrorHandler(message, file, line, column, error) {