Skip to content

Instantly share code, notes, and snippets.

View JamesTheAwesomeDude's full-sized avatar

James E. A. JamesTheAwesomeDude

View GitHub Profile
@JamesTheAwesomeDude
JamesTheAwesomeDude / getElementByXPath.js
Last active March 25, 2024 16:36
Get element(s) by xpath
/**
* Get an element by XPath.
* @param {string} expression - A string representing the XPath to be evaluated.
* @param {Element} [context] - The context node. Defaults to the current document.
* @param {Document} [document] - The document against which evaluate() will be called. Defaults to the context node's document.
* @param {boolean} [ordered=true] - Whether to return the "first" node selected by the expression. If false, allows the XPath engine to return an arbitrary node selected by the expression.
* @returns {Element|null}
*/
function getElementByXPath(expression, context = null, document = null, ordered = true) {
if (context === null) context = window.document;
@JamesTheAwesomeDude
JamesTheAwesomeDude / gamertag_to_xuid.sh
Last active March 21, 2024 20:14
Mostly self-contained bash+curl+sed+nc script to convert Xbox Live Gamertags into XUIDs (includes pseudo-UUID support for Floodgate-enabled Minecraft servers)
#!/bin/bash
set -e
set -o pipefail
# USAGE:
# bash gamertag_to_xuid.sh GAMERTAG [...] > out.csv
# DEPENDENCIES:
# - GNU Bash version 4 or newer (or any shell supporting pipefail, heredocs, read, readarray, and POSIX printf)
# - GNU sed version ?? or newer (or any sed implementation supporting, -n, -e, the "p" flag to "s", and capturing-group references in substitution)
@JamesTheAwesomeDude
JamesTheAwesomeDude / postparse.py
Last active March 16, 2024 22:40
parse POST data without cgi module, for Python 3.11+
import email.message, email.parser, email.policy
import urllib.parse
import re
from collections import namedtuple
try:
from resource import getpagesize
except ImportError:
import mmap
def getpagesize():
return mmap.PAGESIZE
@JamesTheAwesomeDude
JamesTheAwesomeDude / ts3h.ex
Last active March 15, 2024 16:37
[DRAFT] TeamSpeak 3 Hasher in Elixir
import Bitwise
require Logger
# "MEkDAgcAAgEgAh9n5rQmt3zJukGmwwvEVwfbZFS3NJ9oW7hgpJ0Db5ORAh8mOLmbP3+DfEQvEFbOcqymWoF6s9GHVcv8UegJBcWz" 3210790 1959110000000
defmodule TS3H do
def cmd_main(argv) do
[pk_str, ctr_best, ctr_cur] =
@JamesTheAwesomeDude
JamesTheAwesomeDude / bitstringstream.ex
Last active March 14, 2024 18:02
Elixir lazy iterate over byte string (aka "binary" type)
defmodule BitStringStream do
def binary_to_stream(s) do
Stream.unfold(s, fn
<<next::8, rem::binary>> -> {next, rem}
<<>> -> nil
end)
end
def bitstring_to_stream(s, n) do
@JamesTheAwesomeDude
JamesTheAwesomeDude / _cachedir.py
Last active March 11, 2024 19:40
Python get "user cache directory" on any OS
# Usage:
# import _cachedir
# CACHEDIR = _cachedir.cachedir('my_application@company.com')
# CACHEDIR = _cachedir.cachedir('my_packagename@pypi.org')
# NOTE:
# This does not create a secure enclave of any kind.
# Obviously, malicious applications running on the same
# user account could read or modify the cache directory.
@JamesTheAwesomeDude
JamesTheAwesomeDude / _maybe_open.py
Last active March 11, 2024 19:11
Safely open path if not already open, but don't mess with pre-openend files
from contextlib import contextmanager, ExitStack
import io
__all__ = ['maybe_open']
@contextmanager
def maybe_open(file_or_path, mode, *, **k, mode_strict=False):
with ExitStack() as _inner_ctx:
if isinstance(file_or_path, io.IOBase):
@JamesTheAwesomeDude
JamesTheAwesomeDude / _udp_listen.py
Last active March 8, 2024 17:21
Python socket.recv() work with KeyboardInterrupt example
from contextlib import contextmanager
import select
import signal
import socket
try:
from resource import getpagesize
except ImportError:
import mmap
def getpagesize():
@JamesTheAwesomeDude
JamesTheAwesomeDude / safe_tk.py
Last active March 7, 2024 15:27
Python tkinter make exceptions fatal
from contextlib import contextmanager
import tkinter as tk # https://stackoverflow.com/questions/66734619/no-module-named-tkinter#66735147
import tkinter.messagebox
# Two different approaches to handling mainloop errors in tkinter.
# 1. A context manager that will display errors in callbacks and initializers (RECOMMENDED)
# 2. A subclass of Tk that will only display errors in callbacks, not initializers
@contextmanager
@JamesTheAwesomeDude
JamesTheAwesomeDude / lockscreen.py
Last active February 15, 2024 19:56
Perfectly random Android lockscreen pattern
from itertools import combinations, permutations
import re
MIN_LENGTH = 4
ILLEGAL_PATTERN_RE = re.compile('(%s)' % '|'.join(
# Simulate negative lookbehind via negative lookahead
# h/t https://learnbyexample.github.io/py_regular_expressions/lookarounds.html#negated-groups
f'^(?!.*{b}.*{a}{c}).*{a}{c}' for a,b,c, in
['123', '159', '147', '258', '369', '357', '321', '456', '654', '789', '741', '753', '852', '987', '951', '963']