Skip to content

Instantly share code, notes, and snippets.

View samuelmaddock's full-sized avatar
:electron:

Sam Maddock samuelmaddock

:electron:
View GitHub Profile
@samuelmaddock
samuelmaddock / preload-custom-element.ts
Last active September 7, 2023 03:00
Define a custom element in an Electron preload script.
contextBridge.exposeInMainWorld('api', {
click() {
console.log('clicked');
},
});
function mainWorldScript() {
const api: any = (window as any).api;
window.customElements.define(
@samuelmaddock
samuelmaddock / extension-background-apis-enumerated.json
Last active February 29, 2020 16:06
Enumeration of available Chrome APIs in extension background pages using Electron v9.0.0-beta.1
// generated using
// https://gist.github.com/samuelmaddock/fb54ddc47b7a958ded1f799861239a87
{
"alarms": {
"onAlarm": {
"addListener": "() => any",
"removeListener": "() => any",
"hasListener": "() => any",
"hasListeners": "() => any",
"dispatch": "() => any"
@samuelmaddock
samuelmaddock / electron-extension-projects.md
Last active December 10, 2019 02:44
Electron Chrome extension implementations

Metastream Extension Installation

With Google Chrome installed

  1. Find and install extension on the Chrome Webstore. Remember the ID at the end of the URL (e.g. https://chrome.google.com/webstore/detail/ublock-origin/cjpalhdlnbpafiamejdnhcphjbkeiagm).
  2. Navigate to Chrome's extensions directory.
  3. Copy the extension directory with the same name as the ID from step 1.
  4. Paste extension into Metastream's extension directory.

Without Google Chrome installed

  1. Find Chrome extension on the Chrome Webstore and copy the URL.
@samuelmaddock
samuelmaddock / js-runtime-json-interface.js
Last active February 29, 2020 16:07
Dumps a TypeScript-like JSON interface of runtime JavaScript objects.
; (function (targetObj) {
const STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm
const ARGUMENT_NAMES = /([^\s,]+)/g
const getParamNames = func => {
var fnStr = func.toString().replace(STRIP_COMMENTS, '')
var result = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).match(ARGUMENT_NAMES)
if (result === null) result = []
return result
}
@samuelmaddock
samuelmaddock / compile-loader.js
Created April 19, 2018 23:38
webpack code to string loader with child compiler
import loaderUtils from 'loader-utils';
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
/** StringLoader */
export default function loader() {}
export function pitch(request) {
const options = loaderUtils.getOptions(this) || {};
if (!this.webpack) {
@samuelmaddock
samuelmaddock / archive-exports.sh
Last active January 14, 2018 03:33
Swarm remote connection test
export ARCHIVE_KEY='deadbeafdeadbeafdeadbeafdeadbeafdeadbeafdeadbeafdeadbeafdeadbeaf'
@samuelmaddock
samuelmaddock / dat-libsodium-seal.js
Last active November 16, 2017 02:49
Dat crypto
const sodium = require('sodium-universal')
function keyPair(seed) {
var publicKey = new Buffer(sodium.crypto_sign_PUBLICKEYBYTES)
var secretKey = new Buffer(sodium.crypto_sign_SECRETKEYBYTES)
sodium.crypto_sign_keypair(publicKey, secretKey)
return {
publicKey: publicKey,
@samuelmaddock
samuelmaddock / proposal.md
Last active November 4, 2017 18:09
Application user networking using Dat protocol

Application user networking using Dat protocol

This document outlines a structure to connect application users using Dat protocol. Users can identify themselves with a dat archive and publish session metadata to coordinate WebRTC connections.

Forewarning: I have no idea what I'm talking about.

Social peer dat archive

A user's identity resides in a dat archive which can be shared to create a connection.

@samuelmaddock
samuelmaddock / rgba2url.ts
Last active September 23, 2017 19:18
Converts RGBA data to a URL using an HTML canvas element
export const rgba2url = async (
rgba: Uint8Array,
width: number,
height: number
): Promise<string | void> => {
let canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
let ctx = canvas.getContext('2d');