Skip to content

Instantly share code, notes, and snippets.

View dmail's full-sized avatar
πŸ’

Damien Maillard dmail

πŸ’
  • Antibes
View GitHub Profile
@dmail
dmail / commentGithubPullRequest.js
Created August 20, 2019 09:11
commentGithubPullRequest
// https://developer.github.com/v3/issues/comments/#create-a-comment
// https://developer.github.com/v3/issues/comments/#edit-a-comment
const fetch = require("node-fetch")
const commentGithubPullRequest = async ({
token,
repoOwner,
repoName,
issueNumber,
commentBody,
@dmail
dmail / generate-lighthouse-report.js
Created August 19, 2019 15:36
generate lighthouse report
const lighthouse = require("lighthouse")
const chromeLauncher = require("chrome-launcher")
const launchChromeAndRunLighthouse = async ({ url, opts, config = null }) => {
const chrome = await chromeLauncher.launch({ chromeFlags: opts.chromeFlags })
opts.port = chrome.port
const results = await lighthouse(url, opts, config)
// use results.lhr for the JS-consumeable output
// https://github.com/GoogleChrome/lighthouse/blob/master/types/lhr.d.ts
// use results.report for the HTML/JSON/CSV output as a string
export const listenActiveElement = (element, listener) => {
let { activeElement } = document
const check = (event) => {
const previousActiveElement = activeElement
activeElement = document.activeElement
if (previousActiveElement !== activeElement) {
listener({ previousActiveElement, activeElement, event })
}
}
@dmail
dmail / getRelativeBoundingClientRect.js
Created April 5, 2018 12:38
get element position relative to an other
// keep in mind element may be inside an iframe
// var childWindow = elem.document.frames.window;
// while (childWindow != parentWindow) {
// offset.left = offset.left + childWindow.frameElement.getBoundingClientRect().left;
// offset.top = offset.top + childWindow.frameElement.getBoundingClientRect().top;
// childWindow = childWindow.parent;
// }
const getRelativeBoundingClientRect = (element, relativeElement) => {
const { left, top, right, bottom } = element.getBoundingClientRect()
@dmail
dmail / delayFunction.js
Last active March 30, 2018 14:58
a more friendly setTimeout
export const delayFunction = ({ fn, ms = 0 } = {}) => {
let delayed = false
let delayedMs
let paused = false
let pausedMs
let id
const callback = () => {
delayed = false
fn()
@dmail
dmail / batch-ui-mutations.js
Last active March 28, 2018 12:29
Utility to ensure UI mutations are executed together
export const batchUIMutation = (fnMutatingUI) => {
if (typeof window !== 'undefined' && window.requestAnimationFrame) {
const id = requestAnimationFrame(() => {
cancelAnimationFrame(id)
fnMutatingUI()
})
return () => cancelAnimationFrame(id)
}
const id = setTimeout(() => {
@dmail
dmail / url.js
Last active January 5, 2024 11:09
URL parts naming. Inspired from web browsers API (new URL(), window.location) and rfc3986.
/*
href
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
origin β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ authority β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ host resource
β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”
β”‚ β”‚ hostname β”‚ pathname β”‚ β”‚
@dmail
dmail / trackFocusReason.js
Last active April 18, 2018 12:35
track focus reason
const isAncestorOf = (node, possibleDescendantNode) => {
let ancestor = possibleDescendantNode.parentNode
while (ancestor) {
if (ancestor === node) {
return true
}
ancestor = ancestor.parentNode
}
return false
}
@dmail
dmail / bubble-iframe.js
Created June 13, 2017 14:03
Bubbling iframe events
const bubbleIframeEvents = (iframe, document) => {
const iframeWindow = iframe.contentWindow
iframeWindow.addEventListener(
'mousemove',
(event) => {
const boundingClientRect = iframe.getBoundingClientRect()
const fakeEvent = new CustomEvent(
'mousemove',
{
@dmail
dmail / get-array-index-name-status.js
Created November 14, 2016 08:57
Function returning how a name match the naming requirements for an array index in JavaScript
const STRING = 0; // name is a string it cannot be an array index
const INFINITE = 1; // name is casted to Infinity, NaN or -Infinity, it cannot be an array index
const FLOATING = 2; // name is casted to a floating number, it cannot be an array index
const NEGATIVE = 3; // name is casted to a negative integer, it cannot be an array index
const TOO_BIG = 4; // name is casted to a integer above Math.pow(2, 32) - 1, it cannot be an array index
const VALID = 5; // name is a valid array index
const maxArrayIndexValue = Math.pow(2, 32) - 1;
function getArrayIndexStatusForString(name) {
if (isNaN(name)) {