Skip to content

Instantly share code, notes, and snippets.

View kdzwinel's full-sized avatar

Konrad Dzwinel kdzwinel

View GitHub Profile
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
(function(window) {
// Allow listeners to subscribe to localStorage events.
var isObject = function(obj) { return Object.prototype.toString.call(obj) === '[object Object]' },
listeners = [];
var postMessage = function(targetWindow, mesg, targetOrigin) {
@kdzwinel
kdzwinel / catch-globals.js
Last active February 11, 2020 17:21
Unfinished globals proxy
function evalCode(code) {
const func = new Function ('window', `with (window) { ${code} }`);
const obj = {};
const proxy = new Proxy(obj, {
get(target, propKey, receiver) {
console.log(`GET ${String(propKey)}`);
if (propKey === 'window') return proxy;
return Reflect.get(window, propKey, receiver);
},
set(target, propKey, value, receiver) {
@kdzwinel
kdzwinel / filter-out-ads.js
Last active March 22, 2018 09:01
TrackJS custom onError callback that tries to filter out noise from ads
// ES5 for compat
window._trackJs.onError = function(error) {
try {
var fileURL = new URL(error.file);
if (fileURL) {
// safelist includes current domain + some third-parties we want to track
var safe = domainSafelist.some(function (domain) {
return fileURL.host.endsWith(domain);
});
@kdzwinel
kdzwinel / lhjson.js
Created January 25, 2018 14:03
__LIGHTHOUSE_JSON__ analysis script
(function() {
function strSize(obj) {
const string = JSON.stringify(obj);
return string ? string.length : 0;
}
const lhj = __LIGHTHOUSE_JSON__;
const fullSize = strSize(lhj);
const maxLevels = 5;
@kdzwinel
kdzwinel / get-selector-simple.js
Created December 8, 2017 00:24
Simple alternative to the axe-core getSelector
const commonNodes = [
'div', 'span', 'p',
'b', 'i', 'u', 'strong', 'em',
'h2', 'h3',
];
/**
* @param {Array<string>} attributes
* @returns {Map<string, string>}
*/
@kdzwinel
kdzwinel / ps4devtools.js
Created October 8, 2017 21:31
Gamepad - Chrome DevTools integration
(function(){
let gamepad = null;
let loopInterval = null;
window.addEventListener("gamepadconnected", connectHandler);
window.addEventListener("gamepaddisconnected", disconnectHandler);
function connectHandler(e) {
if (!gamepad) {
@kdzwinel
kdzwinel / extension.js
Last active October 25, 2017 17:20
Quick and dirty implementation of runtime type profiling in Visual Studio Code
const vscode = require('vscode');
const path = require('path');
// puppeteer is great because it's easy to use, but it comes with 30MB headless Chrome that
// we can't use as it doesn't have `Profiler.startTypeProfile` yet. We have to point to a
// locally installed Chrome Canary instead. It's fine for a POC, but puppeteer isn't probably
// a good fit in a long run.
const puppeteer = require('puppeteer');
const PATH_TO_CANARY = '/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary';
@kdzwinel
kdzwinel / raf_throttler.js
Created July 7, 2017 08:12
Waiting for IntersectionObserver to get a better support…
function rafThrottler(callback) {
let rafId = null;
const flush = () => {
callback();
rafId = null;
};
return () => {
if (rafId) {
return;
@kdzwinel
kdzwinel / scroll_to.js
Created May 2, 2017 10:24
Scroll to element or scroll by value
let rafId;
function scrollToElement(el, offsetTop = 0) {
scrollByValue(el.getBoundingClientRect().top - offsetTop);
}
function scrollByValue(offsetTop = 0) {
if (offsetTop !== 0) {
if (rafId) {
cancelAnimationFrame(rafId);
@kdzwinel
kdzwinel / jsk.c
Created January 19, 2017 00:38
JSK -JPEG Scan Killer
/*
* jsk.c
*
* Copyright (C) 2013, Frederic Kayser.
*
*/
#include <stdio.h>
#include <stddef.h>