Skip to content

Instantly share code, notes, and snippets.

View cvializ's full-sized avatar
💭
🧙‍♂️

Carlos Vializ cvializ

💭
🧙‍♂️
View GitHub Profile
@cvializ
cvializ / index.html
Created July 29, 2019 19:42
Date.now vs Date.prototype.getTime (https://jsbench.github.io/#5fd7ea6a54047e726093e9c69f27d140) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Date.now vs Date.prototype.getTime</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
@cvializ
cvializ / index.html
Last active November 16, 2018 16:42 — forked from jridgewell/index.html
Iterating over collections of elements (https://jsbench.github.io/#f638cacc866a1b2d6e517e6cfa900d6b) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Iterating over collections of elements</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
@cvializ
cvializ / index.html
Last active August 10, 2018 14:07
Iterating with Dates #jsbench #jsperf (http://jsbench.github.io/#e104ebb4a49cb1b1c19237c9add27d1c) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Iterating with Dates #jsbench #jsperf</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
@cvializ
cvializ / index.html
Last active April 5, 2018 19:13
Mapping over collections of elements (http://jsbench.github.io/#1f0f0ac3a7c49d483a1009d3473788fe) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Mapping over collections of elements</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
@cvializ
cvializ / content-script.js
Created May 12, 2016 00:32
Check the DOM for background-image URLs and replace them with data-URIs
function withMutations(records) {
records.forEach(function (record) {
var target = record.target;
if (extensionContains(target)) {
var children = target.querySelectorAll('*');
Array.prototype.forEach.call(children, fixBackgroundImage);
}
});
}
@cvializ
cvializ / content-script.js
Created May 12, 2016 00:18
Reload an img tag's image data through an extension's background page.
function onExtensionImgError(e) {
var img = e.target;
var tagName = img.tagName.toLowerCase();
var wasReattempted = img.dataset.myExtensionReattempted;
var reattempt = (!wasReattempted && tagName === 'img' && isHttpUrl(img.src) && extensionContains(img));
if (reattempt) {
img.dataset.myExtensionReattempted = true;
api.port.emit('load_image', img.src, function onRespond(data) {
var err = data.error;
if (!err) {
@cvializ
cvializ / content-script.js
Created May 12, 2016 00:13
Check the DOM for images that need to be loaded from the background page
function onCspCheckLoaded(isCspBlocking) {
extension_module.csp = isCspBlocking; // cache the check value
if (isCspBlocking) {
// Capture error events triggered by img tags
document.addEventListener('error', onExtensionImgError, true);
// Check for background images
var observer = new MutationObserver(withMutations);
var whatToObserve = { childList: true, subtree: true };
observer.observe(document, whatToObserve);
}
@cvializ
cvializ / content-script.js
Created May 11, 2016 23:55
A function that checks for a Content Security Policy
/* ... */
// Loads an image on the host page to check for a CSP
function loadCspCheck(cb) {
var CSP_CHECK_URL = '//d34db33f.cloudfront.net/assets/tiny.png';
var cspCheckImage = new Image();
cspCheckImage.addEventListener('load', cb.bind(null, false));
cspCheckImage.addEventListener('error', cb.bind(null, true));
cspCheckImage.src = CSP_CHECK_URL;
}
/* ... */
@cvializ
cvializ / background.js
Created May 11, 2016 23:31
Loading images from the background page of a browser extension.
/* A map of functions listening for messages from the content scripts */
var listeners = {
/* ... */
load_image: function (path, respond, tab) {
// The Firefox worker XHR dislikes protocol-less URLs, so prefix with the tab's protocol.
path = (path.indexOf('//') === 0 ? tab.url.slice(0, tab.url.indexOf(':') + 1) + path : path);
var imageType = path.slice(path.lastIndexOf('.') + 1);
var xhr = new XMLHttpRequest();
xhr.open('GET', path, true);
xhr.responseType = 'arraybuffer';
@cvializ
cvializ / random_wikipedia_urls.sh
Created January 22, 2016 20:47
Grab a list of random Wikipedia URLs from the command line
for i in $(seq 1 20); do
curl -sIL http://en.wikipedia.org/wiki/Special:Random | perl -n -e '/^Location: (.*)$/ && print "$1\n"' | tail -n 1
done