Skip to content

Instantly share code, notes, and snippets.

View Ciantic's full-sized avatar

Jari Pennanen Ciantic

View GitHub Profile
@Ciantic
Ciantic / sqlite3.html
Last active July 1, 2023 22:37
Example how to use official SQLite3 WASM with ESM modules
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
LOOK AT THE CONSOLE
@Ciantic
Ciantic / offsetRelativeTo.js
Created October 22, 2021 11:15
Get offset relative to selector or window
/**
* Get offset relative to selector (or window)
*
* @param {HTMLElement} el
* @param {string|undefined} selector if not give, assumes root most (window)
*/
function offsetRelativeTo(el, selector) {
let offsetTop = el.offsetTop;
let offsetLeft = el.offsetLeft;
let offsetParent = el.offsetParent;
// @deno-types="npm:@types/sql.js"
import { default as initSqlJs } from "npm:sql.js";
import { IDatabase, QueryResult } from "./Database.ts";
export class Database implements IDatabase {
private path: string;
private sqliteJs?: initSqlJs.SqlJsStatic;
private db?: initSqlJs.Database;
private inited = false;
@Ciantic
Ciantic / hhook.rs
Created March 23, 2023 22:57
Just code I haven't used yet
use std::sync::mpsc::Sender;
use windows::{
core::Result,
s,
Win32::{
Foundation::{HANDLE, HWND, LPARAM, LRESULT, WPARAM},
System::{
LibraryLoader::GetModuleHandleA,
Power::{
@Ciantic
Ciantic / simpleEventListener.ts
Last active March 8, 2023 15:39
Very simple type-safe addListener, removeListener, trigger - basically Event / EventEmitter implementation with typesafety
export function simpleMapEvent<TParam, TRes = void, TContext = unknown>(context?: TContext) {
type T = { (this: TContext, inputs: TParam): TRes };
return {
cbs: [] as [T, T][], // First value is used for equality check, second is the bound version
addListener(cb: T, bindThis?: unknown) {
return this.cbs.push([cb, bindThis ? cb.bind(bindThis as TContext) : cb]);
},
removeListener(cb: T) {
const i = this.cbs.findIndex(([v, _]) => v == cb);
if (i >= 0) return this.cbs.splice(i, 1);
@Ciantic
Ciantic / run-powershell-command-once-scheduled.ps1
Last active February 28, 2023 08:36
Run powershell command only once, scheduled
$t = New-ScheduledTaskTrigger -Once -At (get-date).AddSeconds(10); $t.EndBoundary = (get-date).AddSeconds(60).ToString('s'); Register-ScheduledTask -Force -TaskName JustTrying -Action (New-ScheduledTaskAction -Execute "powershell.exe" -Argument '-NoProfile -WindowStyle Hidden -command "& {dir}"') -Trigger $t -Settings (New-ScheduledTaskSettingsSet -DeleteExpiredTaskAfter 00:00:01)
@Ciantic
Ciantic / this-panics.rs
Last active February 23, 2023 21:13
Why this panics? "fatal runtime error: thread local panicked on drop"
struct MyThreadWrapper {
thread: Option<std::thread::JoinHandle<()>>,
}
impl Drop for MyThreadWrapper {
fn drop(&mut self) {
if let Some(thread) = self.thread.take() {
let _ = thread.join();
}
}
}
@Ciantic
Ciantic / wp-allow-svg.php
Last active February 18, 2023 05:19
WordPress allow uploading SVG, even without the XML declaration
<?php
// Mind you, this does not make SVG files safe. This script is meant for sites where only trusted people can upload.
add_action("init", function() {
// First line of defence defused
add_filter('upload_mimes', function ($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
});
@Ciantic
Ciantic / windows_thread.rs
Created February 18, 2023 00:01
Just simple CreateThread example with proper way to QUIT.
use windows::{
core::{GUID, HSTRING},
Win32::{
Foundation::HWND,
System::{
Com::{CoInitializeEx, COINIT_APARTMENTTHREADED},
Threading::{
CreateThread, GetCurrentThreadId, WaitForSingleObject, THREAD_CREATION_FLAGS,
},
},
@Ciantic
Ciantic / hstring.rs
Created February 17, 2023 23:47
Dependency free HSTRING implementation for Rust
// Dependency free HSTRING implementation
use std::ffi::OsStr;
use std::ffi::{c_void, OsString};
use std::os::windows::ffi::OsStrExt;
use std::os::windows::ffi::OsStringExt;
type LPCWSTR = *const u16;
type HRESULT = i32;