Skip to content

Instantly share code, notes, and snippets.

View dotproto's full-sized avatar

Simeon Vincent dotproto

View GitHub Profile
@dotproto
dotproto / singleton.js
Last active August 19, 2025 07:25
An attempt at using ES6 classes to implement the Singleton design pattern.
class Singleton {
static #instances = new WeakMap();
/**
* @param {Boolean} useSharedThis Specifies whether or not `this` should be the singleton itself.
* If `true`, the Singleton class' constructor will return the singleton associated with the current
* prototype being instantiated. As a direct result, instance fields on the inherting class will be
* re-set on the singleton every time the descendant class is instantiated. This may created
* unnecessary and avoidable work. The main advantage of selecting `true` is that the sub-class does
* not have to explicitly return the singleton, since class constructors implicitly return `this`.
@dotproto
dotproto / clear-cookies.js
Created August 14, 2025 16:44
Clear all cookies URLs matching a regular expression in a given cookie store
(async () => {
const stores = await browser.cookies.getAllCookieStores();
const store = stores.at(1);
const exp = /(firefox|mozilla).(org|com|auth0.com)/;
const allCookies = await browser.cookies.getAll({storeId: s.id});
console.log("All cookies:", allCookies);
const filteredCookies = allCookies.filter(c => c.domain.match(exp));
const removeCookies = filteredCookies.map(cookie => {
@dotproto
dotproto / _README.md
Last active March 10, 2025 01:32
Host permission patterns with ports

This gist contains a WebExtension that tests how the browser that runs it handles ports host permission patterns.

Running tests

  1. (optional) Create a new browser profile to ensure that this test runs in a clean environment.
  2. Load this extension in your browser.
  3. Start a debug session for the extension's background context.
  4. (optional) Diable "debug" or "verbose" log messages
  5. Check if your browser auto-upgrades HTTP requests to HTTPS by calling testHttpUpgrades() in the console. If you see a console message that says "Your browser auto-upgraded one or more HTTP requests to HTTPS!", consult the HTTPs Upgrades section below.
@dotproto
dotproto / _README.md
Created November 14, 2024 02:14
timeout()

timeout() is a promise-based version of setTimeout() with AbortSignal support.

  1. Save the files from this gist in a local directory.
  2. Load that directory in Firefox as a temporary extension.
  3. Visit https://example.com and open developer tools.
  4. Click the "Open port" button and note the logged messages.
  5. Click the "Send message" button and note the logged messages.
  6. Wait ~30 seconds. Observe that a "Port disconnected!" message is logged.
  7. Wait another 30 seconds as described in the issue (#36097).
  8. Click "Open port" again.

Result: Could not reproduce the issue. The backgroudn starts up, recieves the connection request, and opens a port as expected.

@dotproto
dotproto / _README.md
Last active July 17, 2024 23:36
`onAuthRequired` event handler test cases

This gist contains a browser extension that tests browser support for testing how onAuthRequired event handlers handle different resoultion patterns. It created in support of reviewing https://github.com/mdn/webextensions-examples/pull/564.

Test # extraInfoSpec Description Chrome Firefox
1 blocking Synchronous object response
2 blocking Synchronous Promise response with async resolution
3 asyncBlocking Synchronous object response
4 asyncBlocking Synchronous Promise response with async resolution
5 asyncBlocking Async callback call with object
@dotproto
dotproto / background.js
Last active May 27, 2024 14:05
CORS Test (Chrome, Firefox) - This extension will begin performing HTTP requests against a bespoke test server immediately after installation. To view the results, open a devtools session for the extension's background context and look through the console output.
globalThis.browser ??= globalThis.chrome;
let testCounter = 0;
let corsTest = async (
{
client: {
forcePreflight = false,
mode = undefined,
},
server: {
@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 / gist:8f0b3e39215f002a0908be3d3122ce88
Last active March 29, 2022 21:52
Calculate pixel height in inches/mm at a given distance
/**
* @param {number} srcPixels Number of pixels used when rendering on a standard desktop dispaly. Defaults to 1 pixel
* @param {number} distance Distance in inches at which the item is rendered. Defaults to 28 inches (distance specified in the CSS spec)
*
* https://www.w3.org/TR/css-values-4/#reference-pixel
*/
function getProjectedPixel({pixels = 1, distance = 28} = {}) {
const inToMm = (inch) => inch * 25.4;
const opposite = distance;
@dotproto
dotproto / devtools.html
Created September 8, 2021 03:33
DevTools network request monitor. Minimal demo to show how one could use `chrome.devtools.network.onRequestFinished` to monitor request traffic on a given page.
<!DOCTYPE html>
<script src="devtools.js"></script>