Skip to content

Instantly share code, notes, and snippets.

View nsrau's full-sized avatar
🇧🇷

Newton Samin Urbanetz nsrau

🇧🇷
View GitHub Profile
@jaredwilli
jaredwilli / Attachment Uploader Metabox
Created February 10, 2011 07:03
Need to save or refresh post after adding new input group to make the ajax upload init
<?php
/**
* Name: Attachments Metabox Uploader
* Author: Jared Williams - http://new2wp.com
* Description: This is for adding a custom metabox to post types which enables you to add/remove
* post attachments that you can upload and enter the meta information for right on the edit page.
* Version: 0.1.0
*
* Notes: In order to add this to a post type you need to find the word 'product' and replace it with * whatever the post type is you want to use it on.
*/
@nathansmith
nathansmith / cache.js
Last active September 27, 2018 15:47
Example of self-clearing cache using window.localStorage
// Set up "namespace."
var APP = APP || {};
// `Shared settings.
//----------------------------------------------------------------------------------------------------
APP.config = APP.config || {};
// How long to keep cache, in minutes.
APP.config.cache_duration = APP.config.cache_duration || 15;
@souporserious
souporserious / getTextWidth.js
Created January 21, 2019 20:59
Get the width for a single line of text
/**
* Get the width for a single line of text
* @param {String} text - the string of text to be measured
* @param {Object|HTMLElement} font - optional font styles or node to pull font styles from
* @return {Number} the width of the text passed in
*/
const expando = 'text-width' + new Date().getTime()
const canvas =
typeof document === 'undefined' ? null : document.createElement('canvas')
const context = canvas && canvas.getContext('2d')
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<!-- Virtual Directory Setup
assign virtualDirPath below a value like '/{path}'
Add below to your app code and then prepend routes with virtualDirPath
var virtualDirPath = process.env.virtualDirPath || ''; -->
@nievesj
nievesj / caprover-develop.yml
Created November 18, 2020 20:40
CapRover Action example
on:
push:
branches:
- develop
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
@pbojinov
pbojinov / jquery.deferred.promise.js
Last active August 31, 2022 02:38
simple jQuery Deferred example
function getData() {
var deferred = $.Deferred();
$.ajax({
'url': 'http://google.com',
'success': function(data) {
deferred.resolve('yay');
},
'error': function(error) {
deferred.reject('boo');
@GuillaumeJasmin
GuillaumeJasmin / extract.js
Last active September 26, 2022 04:10
Extract substring between two strings
String.prototype.extract = function(prefix, suffix) {
s = this;
var i = s.indexOf(prefix);
if (i >= 0) {
s = s.substring(i + prefix.length);
}
else {
return '';
}
if (suffix) {
@austinhyde
austinhyde / js-observables-binding.md
Last active August 16, 2023 18:19
Vanilla JavaScript Data Binding

Observables

You don't really need a framework or fancy cutting-edge JavaScript features to do two-way data binding. Let's start basic - first and foremost, you need a way to tell when data changes. Traditionally, this is done via an Observer pattern, but a full-blown implementation of that is a little clunky for nice, lightweight JavaScript. So, if native getters/setters are out, the only mechanism we have are accessors:

var n = 5;
function getN() { return n; }
function setN(newN) { n = newN; }

console.log(getN()); // 5

setN(10);

@CatTail
CatTail / htmlentity.js
Created November 30, 2012 08:27
Javascript: encode(decode) html text into html entity
// encode(decode) html text into html entity
var decodeHtmlEntity = function(str) {
return str.replace(/&#(\d+);/g, function(match, dec) {
return String.fromCharCode(dec);
});
};
var encodeHtmlEntity = function(str) {
var buf = [];
for (var i=str.length-1;i>=0;i--) {
@wesleyegberto
wesleyegberto / convert-postman-to-insomnia.js
Created September 22, 2020 05:57
Script to convert a Postman backupt to Insomnia
/**
* Script to parse a Postman backupt to Insomnia keeping the same structure.
*
* It parses:
* - Folders
* - Requests
* - Environments
*
* Notes: Insomnia doesn't accept vars with dots, if you are using you must replace yours URLs manually (see ENVIRONMENTS_EXPORTS).
*/