Skip to content

Instantly share code, notes, and snippets.

@martianyi
martianyi / README.md
Last active May 16, 2018 04:04
SSH related commands

copy ssh key to server

ssh-copy-id -i ~/.ssh/mykey user@host

fix warning about ECDSA host key

ssh-keyscan -t ecdsa server.domain >> ~/.ssh/known_hosts

@martianyi
martianyi / bubbleSort.js
Created May 13, 2018 14:54
JS sort algorithm
function bubbleSort(arr) {
for (var i = 0; i < arr.length - 1; i++) {
for (var j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j+1]) {
var temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
@martianyi
martianyi / githack.js
Last active October 13, 2022 01:13
bookmarklet to view file in raw.githack.com
javascript:(function() {
"use strict";
var TEMPLATES = [
[/^(https?):\/\/gitlab\.com\/([^\/]+\/[^\/]+)\/(?:raw|blob)\/(.+\..+?)(?:\?.*)?$/i, '$1://gl.githack.com/$2/raw/$3'],
[/^(https?):\/\/bitbucket\.org\/([^\/]+\/[^\/]+)\/(?:raw|src)\/(.+\..+?)(?:\?.*)?$/i, '$1://bb.githack.com/$2/raw/$3'],
[/^(https?):\/\/bitbucket\.org\/snippets\/([^\/]+\/[^\/]+)\/revisions\/([^\/\#\?]+)(?:\?[^#]*)?(?:\#file-(.+\..+?))$/i, '$1://bb.githack.com/!api/2.0/snippets/$2/$3/files/$4'],
[/^(https?):\/\/bitbucket\.org\/snippets\/([^\/]+\/[^\/\#\?]+)(?:\?[^#]*)?(?:\#file-(.+\..+?))$/i, '$1://bb.githack.com/!api/2.0/snippets/$2/HEAD/files/$3'],
[/^(https?):\/\/bitbucket\.org\/\!api\/2.0\/snippets\/([^\/]+\/[^\/]+\/[^\/]+)\/files\/(.+\..+?)(?:\?.*)?$/i, '$1://bb.githack.com/!api/2.0/snippets/$2/files/$3'],
[/^(https?):\/\/api\.bitbucket\.org\/2.0\/snippets\/([^\/]+\/[^\/]+\/[^\/]+)\/files\/(.+\..+?)(?:\?.*)?$/i, '$1://bb.githack.com/!api/2.0/snippets/$2/files/$3'],
[/^(
@martianyi
martianyi / rawgit.js
Last active April 25, 2018 04:51
bookmarklet to view file in rawgit.com
javascript: (function() {
let devDomain = 'rawgit.com';
const GITHUB_API_URL = 'https://api.github.com';
const REGEX_GIST_URL = /^https?:\/\/gist\.github\.com\/.+?\/([0-9a-f]+)(?:\/([0-9a-f]+))?/i;
const REGEX_RAW_GIST_URL = /^https?:\/\/gist\.githubusercontent\.com\/(.+?\/[0-9a-f]+\/raw\/(?:[0-9a-f]+\/)?.+\..+)$/i;
const REGEX_RAW_REPO_URL = /^https?:\/\/raw\.github(?:usercontent)?\.com\/(.+?)\/(.+?)\/(.+?)\/(.+)/i;
const REGEX_REPO_URL = /^https?:\/\/github\.com\/(.+?)\/(.+?)\/(?!releases\/)(?:(?:blob|raw)\/)?(.+?)\/(.+)/i;
formatUrl();
function formatRawGistUrl(url) {
@martianyi
martianyi / browser-detect.js
Last active May 16, 2018 04:06
feature detect desktop browser
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
var isChrome = !!window.chrome && !!window.chrome.webstore;
var isFirefox = typeof InstallTrigger !== 'undefined';
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
@martianyi
martianyi / via-hypothesis.js
Created April 21, 2018 12:42
bookmarklet to open link in via.hypothes.is
javascript:(function () {location = 'https://via.hypothes.is/' + location.href;})()
@martianyi
martianyi / search-text.js
Created April 21, 2018 08:27
bookmarklet to search text in page
javascript: (function() {
var count = 0,
text;
text = prompt("Search phrase:", "");
if (text == null || text.length == 0) return;
if (!window.__obody) window.__obody = document.body.cloneNode(true);
function searchWithinNode(node, te, len) {
var pos, skip, spannode, middlebit, endbit, middleclone;
skip = 0;
@martianyi
martianyi / view-source-with-line-numbers.js
Last active September 10, 2020 18:54
bookmarklet to view source with syntax highlighting using highlight.js
javascript: function getSelSource() {
x = document.createElement('div');
x.appendChild(window.getSelection().getRangeAt(0).cloneContents());
return x.innerHTML;
}
function makePre(text) {
p = nd.createElement('pre');
c = nd.createElement('code');
c.setAttribute('class', 'language-html');
@martianyi
martianyi / vconsole.js
Last active September 10, 2020 18:57
bookmarklet to add vConsole in mobile browser
javascript: (function() {
if (document.getElementById('__vconsole')) return;
function callback() {
var vConsole = new VConsole();
}
var s = document.createElement("script");
s.src = "https://res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/vconsole/3.0.0/vconsole.min.js";
if (s.addEventListener) {
s.addEventListener("load", callback, false)
} else if (s.readyState) {
@martianyi
martianyi / daemon.md
Last active April 16, 2018 02:29
Simple way to run and stop a daemon in Linux

start a daemon

nohup COMMAND >/dev/null 2>&1 &

stop a daemon

ps -ef | grep COMMAND or ps aux | grep COMMAND

kill pid