Skip to content

Instantly share code, notes, and snippets.

@z11i
z11i / check-aws-sso.sh
Last active June 6, 2023 01:28
Auto check AWS SSO credentials validity, and login if invalid. You can set this as a cronjob.
#!/usr/bin/env bash
PROFILE=${PROFILE:-my-profile-to-use}
AWS=/opt/homebrew/bin/aws
SSO_ACCOUNT=$($AWS sts get-caller-identity --query "Account" --profile "$PROFILE")
# kill existing pending sessions
pgrep -f 'aws sso login' | xargs kill
if [ ${#SSO_ACCOUNT} -eq 14 ]; then # 14 because 2 quotes + 12-digit account number
exit 0
@z11i
z11i / waitForKeyElements.js
Last active June 11, 2021 12:51 — forked from BrockA/waitForKeyElements.js
A utility function, for Greasemonkey scripts, that detects and handles AJAXed content.
/*--- waitForKeyElements(): A utility function, for Greasemonkey scripts,
that detects and handles AJAXed content.
Usage example:
waitForKeyElements (
"div.comments"
, commentCallbackFunction
);
@z11i
z11i / upload.sh
Last active November 16, 2024 22:05
Use curl to upload a file in a multipart/form-data request, with custom content-type for the file (not the request)
filename='yourfilename'
filetype='text/csv'
token='my oauth token'
url='http://localhost/upload'
curl "$url" \
--form "data=@$filename;type=$filetype" \
--form "name=somename" \
-H "Authorization: Bearer $token"
@z11i
z11i / replacer.js
Last active November 28, 2020 07:26
Replaces all regular expression patterns in a string with dynamic data from a map.
function makeReplacer(str) {
const regex = /\$\w+\.(\w+)/g;
let strFragments = []; // stores static strings, or a function to get value from map
let m; // regex match object
let idx; // index tracking the position of one match in `str`
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
@z11i
z11i / strip_html_tags.go
Created June 4, 2020 08:30
Strip HTML tags in Golang
import (
"html"
"reflect"
"strings"
"github.com/microcosm-cc/bluemonday"
)
var p = bluemonday.UGCPolicy()
@z11i
z11i / karabiner-command-escape.json
Last active December 10, 2022 10:20
Karabiner complex modification: Post command+backtick if command+escape is pressed. This is useful to those who use 60% compact keyboards on which backtick/grave keys are replaced with the escape key.
{
"title": "Emit Command+Backtick if Command and Escape are pressed",
"rules": [
{
"description": "Change escape to backtick if pressed with command",
"manipulators": [
{
"type": "basic",
"from": {
"key_code": "escape",
@z11i
z11i / unfollow.js
Created November 25, 2017 04:48
批量取关知乎关注问题。去 https://www.zhihu.com/question/following 页面执行脚本。会取消所有关注的问题。
function unfollow() {
window.scrollTo(0, document.body.scrollHeight);
setTimeout(function() {
console.log('unfollowed');
let list = document.querySelectorAll('.follow-link.zg-unfollow.meta-item');
list[0].click();
unfollow()
}, 1000)
}
unfollow()
@z11i
z11i / UniqueArray.java
Created February 6, 2016 13:08
Remove duplicates in an array while maintaining the original order
import java.util.*;
public class UniqueArray {
public static int[] unique(int[] integers) {
return Arrays.stream(integers).distinct().toArray();
}
}
@z11i
z11i / coding-standards-for-cpp.md
Last active May 16, 2024 10:00
Coding Standards for C++ (Extract)