Skip to content

Instantly share code, notes, and snippets.

View nc7s's full-sized avatar

Blair Noctis nc7s

View GitHub Profile
@nc7s
nc7s / isInternal.js
Created July 12, 2019 08:49
Decide if a given href is internal link
function isInternalLink(href) {
try {
/* full URL i.e. has protocol */
let url = new URL(href)
return url.origin === window.location.origin
} catch {
return !href.startsWith('//') ||
(href.startsWith('//') && (window.location.protocol + href).startsWith(window.location.origin))
}
}
@nc7s
nc7s / diff-then-copy-strategy-benchmarks.adoc
Last active August 15, 2019 17:34
diff-then-copy strategy benchmarks

diff-then-copy strategy benchmarks

methods

  • direct_copy direct copy using language standard library

  • shell_copy direct copy using shell copy command

  • sha1_diff calculate sha1 hashes of both files using language standard library

  • shell_sha1_diff calculate sha1 hashes of both files using /usr/bin/sha1sum

  • shell_diff using diff programs like GNU diff or git-diff to determine difference

  • read_and_compare_whole read files as a whole and compare in-memory

  • read_and_compare_chunk read chunks of files and compare in-memory until differ or end

@nc7s
nc7s / multiRequest.ts
Last active May 11, 2020 13:47
Run multiple requests with a limit on concurrent requests.
; const [multiRequest, makeRequestFunc] = (() => {
let globalRequestPool = [],
globalProcessingPool = [],
globalMaxConcurrentRequests = 5, /* Give it a default */
globalRequestFunc = null,
globalProcessPoolFunc = null
type RequestOptions = {
isGlobal: boolean,
requestFunc: Function
@nc7s
nc7s / Promise.all.js
Last active May 13, 2020 08:57
Implement Promise.all by hand.
function promiseAll(promises) {
return new Promise((resolve, reject) => {
let results = [],
position = 0,
fulfilled = 0,
resolved = false
for(let promise of promises) {
let promisePosition = position
if(promise instanceof Promise) {
promise.then(value => {
@nc7s
nc7s / extract.js
Last active May 2, 2022 23:49
Extract front page featured articles of SCP wiki, SCP-CN, and SCP-ZH-TR
// Main site, https://scp-wiki.wikidot.com
[...document.querySelectorAll('.feature-block .content-panel')].map(p => ({
feature: p.querySelector('.panel-heading').textContent.trim(),
title: p.querySelector('h4').textContent.trim(),
url: p.querySelector('h4 a').href,
authors: [...p.querySelectorAll('.printuser a')].map(a => ({
name: a.textContent.trim(),
url: a.href
})),
featuredBy: p.querySelector('.panel-footer').textContent.trim().replace('Featured by ', '').replace(/and /g, '').split(', ')
@nc7s
nc7s / dead-homeworld-and-lone-explorers.rle
Created May 2, 2022 23:53
Some Game of Life sources files. Import on https://copy.sh/life/ to see them in action.
#C Generated by copy.sh/life
x = 10, y = 10, rule = B3/S23
5bo2bo$3b5o$3bobo$b2o2bo$o3bob2obo$2bo2b2ob2o$o4bo3bo$4b2ob3o$3o4bo$2o
bob2o!
// ==UserScript==
// @name 苹果园 HTTPS 化脚本
// @description 将页面内所有链接和表单的指向改为 HTTPS,保证全站升级到 HTTPS 前的安全性
// @match https://www.goddessfantasy.net/*
// @match http://www.goddessfantasy.net/*
// @version 0.1.3
// @updateURL https://gist.github.com/nc7s/4d8782b75dce60e2a6110afc8997604a/raw/goddessfantasy-https.user.js
// @downloadURL https://gist.github.com/nc7s/4d8782b75dce60e2a6110afc8997604a/raw/goddessfantasy-https.user.js
// ==/UserScript==