Skip to content

Instantly share code, notes, and snippets.

@justforuse
justforuse / eventemitter.js
Created May 4, 2020 13:56 — forked from mudge/eventemitter.js
A very simple EventEmitter in pure JavaScript (suitable for both node.js and browsers).
/* Polyfill indexOf. */
var indexOf;
if (typeof Array.prototype.indexOf === 'function') {
indexOf = function (haystack, needle) {
return haystack.indexOf(needle);
};
} else {
indexOf = function (haystack, needle) {
var i = 0, length = haystack.length, idx = -1, found = false;
@justforuse
justforuse / .gitignore
Created July 7, 2020 04:07 — forked from markbrouch/.gitignore
.gitignore file for front-end projects
# Created by https://www.gitignore.io/api/node,bower,osx,linux,windows,dropbox,sass,less,grunt,sublimetext,code
### Node ###
# Logs
logs
*.log
npm-debug.log*
# Runtime data
@justforuse
justforuse / URLSearchParams.js
Created October 13, 2020 13:26
Parse url search params by vanilla js
// Sometimes this should use location.href
let params = (new URL(document.location)).searchParams;
let name = params.get('name'); // is the string "Jonathan Smith".
let age = parseInt(params.get('age')); // is the number 18
@justforuse
justforuse / a.js
Created October 21, 2020 09:53
JavaScript download canvas as image
const a = document.createElement('a');
const ele = document.querySelector('canvas');
a.download = 'img.png';
a.addEventListener('click', () => {
a.href = ele?.toDataURL("image/png");
})
a.click();
@justforuse
justforuse / text.css
Created December 1, 2020 07:54
text ellipsis
.truncate {
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
// your style, like max-width: 100px;
}
@justforuse
justforuse / copyToClipboard.js
Created December 3, 2020 06:45
copy string to your clipboard by javascript
function copyToClipboard(str) {
if (!str) {
return;
}
// input element cannot keep '\n', so you can turn to textarea
const inputEl = document.createElement('textarea');
inputEl.value = str;
inputEl.style.cssText = 'position: absolute; top: -9999px; left: -9999px;';
document.body.appendChild(inputEl);
inputEl.select();
@justforuse
justforuse / .gitconfig
Created May 24, 2021 09:09
Git global alias config
[user]
name = xxxx
email = [email protected]
[alias]
co = checkout
st = status
ci = commit
br = branch
ps = push
pl = pull
@justforuse
justforuse / main.js
Created May 25, 2021 09:04
Detect whether element scroll to the end
function detectWhetherElementScrollToEnd(element) {
const { scrollLeft, scrollTop, offsetWidth, offsetHeight, scrollWidth, scrollHeight } = element;
if (scrollTop === 0) {
return 'scroll to top';
}
if (scrollTop + offsetHeight >= scrollHeight) {
return 'scroll to end';
}
if (scrollLeft === 0) {
return 'scroll to left';
@justforuse
justforuse / promise-limit-all.js
Last active September 8, 2021 14:22
Promise async pool with limit
Promise.limitAll = function(promises, limit) {
return new Promise(resolve => {
let resolvedCount = 0;
let count = 0;
let res = [];
const len = promises.length;
function next(p, index) {
p().then(r => {
res[index] = r;
@justforuse
justforuse / lock.bat
Created November 15, 2021 14:27
Lock your windows computer by .bat file
rundll32.exe user32.dll LockWorkStation