Skip to content

Instantly share code, notes, and snippets.

@Venryx
Venryx / Main.ts
Last active June 30, 2021 20:03
String.AsMultiline
declare global {
interface String {
/**
* Reformats a multi-line string to represent the actual intended "block" of text.
* @param desiredIndent How much to indent each line. (after removal of the first-line indent-length from each of them)
*/
AsMultiline(desiredIndent: number): string;
}
}
String.prototype.AsMultiline = function(this: string, desiredIndent: number = null) {
@Venryx
Venryx / WebpackConfig.js
Created September 8, 2019 03:10
Example inline-plugin to replace CopyWebpackPlugin
[...]
let fs = require("fs-extra");
webpackConfig.plugins.push({
apply(compiler) {
console.log(`Valid hook names: ${Object.keys(compiler.hooks)}`);
//compiler.hooks.beforeRun.tap("CopyPlugin_Custom", params=>{
compiler.hooks.shouldEmit.tap("CopyPlugin_Custom", params=>{
console.log(`Copying some difficult files (eg. *.wasm) from "node_modules/..." to "Resources/FromNodeModules/...".`);
@Venryx
Venryx / DialogFlowExample.ts
Last active July 14, 2020 09:56
How to get a Google auth-token for DialogFlow, entirely from front-end JavaScript
import {GetAccessToken, ClaimSet} from "./GoogleAuth";
const projectID = "XXXXXX";
// session-id can apparently be set to an arbitrary value, eg. "abcd1234-abcd-abcd-abcd-abcd12345678".
const sessionID = "XXXXXX";
const serviceAccountEmail = `XXXXXX@${projectID}.iam.gserviceaccount.com`;
const serviceAccountPrivateKey = `-----BEGIN PRIVATE KEY-----\nXXXXXX\n-----END PRIVATE KEY-----`;
export async function ConvertSpeechToText(audioData_str_opus: string): Promise<string> {
const claimSet = new ClaimSet(["https://www.googleapis.com/auth/cloud-platform"], serviceAccountEmail);
@Venryx
Venryx / Example.tsx
Last active February 9, 2023 22:36
Using "useImperativeHandle" in a React functional component, with automatic TypeScript typing
import {forwardRef, useImperativeHandle, ForwardRefExoticComponent, RefAttributes, Ref} from "react";
export type Handle<T> = T extends ForwardRefExoticComponent<RefAttributes<infer T2>> ? T2 : never;
export const Parent = (props: {})=> {
let childHandle: Handle<typeof Child>;
return (
<div onClick={()=>childHandle.SayHi()}>
<Child name="Bob" ref={c=>childHandle = c}/>
</div>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Test1</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>
@Venryx
Venryx / Example.kt
Last active February 17, 2020 23:07
Trigger SonicBomb shakes from your app
// Note: I'm not updating this anymore. For the latest version, see here: https://stackoverflow.com/a/60271550/2441655
val patterns = arrayOf(
"!WLALALALALALALALA;", // default
"!WTRG0TRG0TRG0TRG;", // pulse
"!WPJ0FPV7S1J600;" // sos
// you can add your own patterns by mixing and matching the vibration components
);
// strength is between 0 and 100; pattern follows the format seen above
@Venryx
Venryx / GearVRInput.ts
Created December 10, 2020 11:30
GearVR controller connection code, using NodeJS's "noble" bluetooth-stack
import {Characteristic, Peripheral, Service} from "@abandonware/noble";
export class ControllerData {
accel: number[];
gyro: number[];
magX: number;
magY: number;
magZ: number;
@Venryx
Venryx / Netflix.seek.js
Created December 12, 2020 00:56 — forked from dimapaloskin/Netflix.seek.js
Netflix seek
const videoPlayer = netflix
.appContext
.state
.playerApp
.getAPI()
.videoPlayer;
// Getting player id
const videoPlayer = videoPlayer
.getAllPlayerSessionIds()[0]
@Venryx
Venryx / coreos_prometheus->Tiltfile
Created August 14, 2021 13:27
coreos_prometheus, modified to work on Windows
# (C) Copyright 2020 Hewlett Packard Enterprise Development LP
def ToLPath(path):
return path.replace("\\", "/").replace("C:/", "/mnt/c/")
def ToWPath(path):
return path.replace("/", "\\").replace("\\mnt\\c\\", "C:\\")
def ListDir(path, recursive = False):
result = []
for file in listdir(ToWPath(path), recursive):
@Venryx
Venryx / rwlock_tracked.rs
Last active April 18, 2022 05:35
Example of wrapper for RwLock that includes tracking of open lock-guards (for easy debugging of deadlocks)
use std::{sync::{Mutex, Arc}, ops::{Deref, DerefMut}};
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
/// Wrapper around RwLock, which:
/// 1) Requires that anyone who takes a lock-guard must supply their "name".
/// 2) Provides a way for anyone to get a list of "current lock-guard holders". (without having to take a lock-guard themselves)
pub struct RwLock_Tracked<T> {
l: RwLock<T>,
live_guards: Arc<Mutex<Vec<String>>>,
}