Skip to content

Instantly share code, notes, and snippets.

View dotproto's full-sized avatar

Simeon Vincent dotproto

View GitHub Profile
@dotproto
dotproto / defaults.js
Created July 10, 2017 22:20
Ways of handling parameter defaults in JS
/*
I was just reading through Eric Elliott's ["10 Tips for Better Redux Architecture"](https://medium.com/javascript-scene/10-tips-for-better-redux-architecture-69250425af44) and found myself a little thrown by some of his sample code. Rather than justshrug it off, I took a few minutes to break down the pros/cons of his appoach and a couple other implimentations I was kicking around.
*/
// Eric's original implimentation
const createChat_v1 = ({
id = 0,
msg = '',
user = 'Anonymous',
timeStamp = 1472322852680
@dotproto
dotproto / silver.js
Created September 7, 2017 03:34
Reflect + some customizations
export default var Silver = Object.create(Reflect, {
// ownEntries has a similar API to Object.entries, but it uses the same
// enumeration logic as Reflect.keys
ownEntries: {
writable: true,
enumerable: false,
configurable: true,
value: function(obj) {
return Reflect.ownKeys(obj).map(key => [key, obj[key]]);
}
@dotproto
dotproto / default-params.js
Created November 22, 2017 01:03
Short collection of thoughts on a couple different approaches for default "options" values in a function
// Static defaults
function fn({a = 1, b = 2} = {}) {
return `${a}, ${b}`
}
fn() // 1, 2
fn({b: 'beta'}) // 1, beta
/**
@dotproto
dotproto / async_playground.js
Created November 15, 2017 17:50
An async iteration experiment. I was thinking about RxJS-like observables.
function clickPromise(el) {
// Create pointers for proimse chain methods
let _resolve = null
let _reject = null
const p = new Promise((resolve, reject) => {
// Cache the promise chain methods for future use
_resolve = resolve
_reject = reject
@dotproto
dotproto / assignProps.js
Created November 27, 2017 18:50
Copy the specified enumerable properties (strings or symbols) from the source object to the destination object
function assignProps(source, dest, props) {
const descriptors = {};
props.forEach(name => {
let desc = Object.getOwnPropertyDescriptor(source, name);
if (desc && desc.enumerable) {
descriptors[name] = desc;
}
});
Object.defineProperties(dest, descriptors);
}
@dotproto
dotproto / LICENSE
Created June 13, 2019 17:09
Simple extension that injects a script before the page loads
Copyright 2019 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
@dotproto
dotproto / yt-translation.js
Created October 4, 2019 18:00
Open transcription suggestion for current YT video
javascript:(function(){
var log = (...a) => console.log(...a);
const url = new URL(document.location.href);
if (url.hostname.endsWith('youtube.com')) {
const id = url.searchParams.get('v');
window.location = `https://www.youtube.com/timedtext_video?v=${id}`;
}
})()
@dotproto
dotproto / background.js
Created April 12, 2022 20:40
Double click action in Manifest V3. Heavily based on Turn Off the Lights.
let alreadyClicked = false;
let timer;
chrome.action.onClicked.addListener(function(tab) {
let wasClicked = alreadyClicked;
// Reset state
if (wasClicked) {
clearTimeout(timer);
alreadyClicked = false;
@dotproto
dotproto / background.js
Last active May 5, 2023 02:20
Inject video on page that has a CSP restrict that would normally prevent the video from being injected. To see this demo in action, load the extension unpacked and trigger the extension's action on the desired page.
// Copyright 2021 Google LLC.
// SPDX-License-Identifier: Apache-2.0
let videos = [
{
url: 'https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4',
type: 'video/mp4',
}, {
url: 'https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm',
type: 'video/webm',
@dotproto
dotproto / background.js
Created July 23, 2021 18:14
Manifest V3 module service worker demo
// Copyright 2021 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// Use importScripts to load other JS files
import { demo } from './lib.js';
console.log(demo.greeting);