Skip to content

Instantly share code, notes, and snippets.

View hungsu's full-sized avatar

Hung-Su Nguyen hungsu

View GitHub Profile
@hungsu
hungsu / dataview-random-note.js
Last active May 24, 2023 05:40
One random note from my to read list
var seedNotes = dv.pages('"Private/Urgent"').file.filter(function(note){return note.name !== "_Index"})
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
var randomNoteIndex = getRandomInt(0, seedNotes.length)
var randomNote = seedNotes.filter(function(note,index){return index == randomNoteIndex})
@hungsu
hungsu / git.sh
Created January 17, 2019 13:31
Git: Remove all branches except develop
git branch | grep -v "develop" | xargs git branch -D
@hungsu
hungsu / imgPreload.js
Created September 24, 2013 07:14
Preload an image into browser cache (but not the DOM)
function preload(arrayOfImages) {
$(arrayOfImages).each(function(){
(new Image()).src = this;
});
}
// Usage:
preload([
'img/imageName.jpg',
@hungsu
hungsu / jquery-loader.js
Last active December 21, 2015 01:38
jQuery requireJS loader
define(['path/to/jquery.min'], function () {
return jQuery.noConflict(true);
});
@hungsu
hungsu / scrollTop.js
Last active March 26, 2020 14:14
Scroll to top of page, cross browser
document.body.scrollTop = document.documentElement.scrollTop = 0;
@hungsu
hungsu / global-error.js
Last active December 20, 2015 16:39
Alert uncaught errors when they happen
window.onerror = function(msg, url, line) {
// You can view the information in an alert to see things working
// like so:
alert("Error: " + msg + "\nurl: " + url + "\nline #: " + line);
// TODO: Report this error via ajax so you can keep track
// of what pages have JS issues
var suppressErrorAlert = true;
// If you return true, then error alerts (like in older versions of
@hungsu
hungsu / customEvent.js
Last active December 20, 2015 13:09
Push a custom event into analytics
_gaq.push(['_trackEvent', 'category', 'action', 'label', 'val']);
function is_touch_device() {
return !!('ontouchstart' in window) // works on most browsers
|| !!('onmsgesturechange' in window); // works on ie10
};
@hungsu
hungsu / hyphenate.css
Last active December 19, 2015 02:29
Cross browser safe way to hyphenate words
-ms-word-break: break-all;
word-break: break-all;
// Non standard for webkit
word-break: break-word;
-webkit-hyphens: auto;
-moz-hyphens: auto;
hyphens: auto;