Skip to content

Instantly share code, notes, and snippets.

View navdeepsingh's full-sized avatar
🎯
Focusing

Navdeep Singh navdeepsingh

🎯
Focusing
View GitHub Profile
@navdeepsingh
navdeepsingh / serviceworker.js
Last active May 11, 2018 05:27
Service Worker Script
// At index.html
if (navigator.serviceWorker) {
navigator.serviceWorker.register('/serviceworker.js')
}
// Main file serviceworker.js
const staticCacheName = 'cacheFile-v.0';
self.addEventListener('install', installEvent => {
installEvent.waitUntil(
caches.open(staticCacheName)
@navdeepsingh
navdeepsingh / ajax.js
Created June 5, 2018 08:19
XMLHttpRequest aka XHR
// Creating a XHR request
// Create new XHR Object
const xhrObject = new XMLHttpRequest();
xhrObject.open('GET', 'https://api.unsplash.com/search/photos/?page=1&query=nature');
xhrObject.setRequestHeader('Authorization', 'Client-ID xxxxx');
// Handle resposne
xhrObject.onload(handleResponse);
@navdeepsingh
navdeepsingh / serviceworker-v1.js
Last active June 21, 2018 08:17
Fetch Event for HTML/ Images/ all files
/*
1) When the user requests an HTML file,
a) fetch that page from the network;
i) and put a copy in the cache;
b) otherwise look for a cached version of the page;
c) otherwise show the fallback page.
2) When the user requests an image,
a) look for a cached version of the image;
i) fetch a fresh version from the network
(1) and update the cache;
@navdeepsingh
navdeepsingh / trim-cache.js
Created June 25, 2018 10:21
trim-cache.js
function trimCache(cacheName, maxItems) {
cacheName.open( cache => {
cache.keys()
.then( items => {
if (items.length > maxItems) {
cache.delete(items[0])
.then(
trimCache(cacheName, maxItems)
); // end delete then
} // end if
let $photoInput = document.getElementById("input");
let image = new Image();
let $editor = document.getElementById("editor");
let $editorCtx = $editor.getContext("2d");
function opacitor(op) {
let imgData = $editorCtx.getImageData(0, 0, $editor.width, $editor.height);
for (let x = 0; x < image.width; x++) {
for (let y = 0; y < image.height; y++) {
let index = (x + y * image.width) * 4;
let $photoInput = document.getElementById("input");
let fileReader = new FileReader();
let image = new Image();
let $editor = document.getElementById("editor");
let $editorCtx = $editor.getContext("2d");
//This is a performance test
function opacitor(op) {
let imgData = $editorCtx.getImageData(0, 0, $editor.width, $editor.height);
for (let x = 0; x < image.width; x++) {
@navdeepsingh
navdeepsingh / postToOmnisend.js
Last active February 21, 2019 06:07
Post to Omnisend
/*****************/
//@TODO Update Omnisend separately with list id
/*****************/
// An object of options to indicate where to post to
var options = {
"method": "POST",
"protocol": 'https:',
"hostname": 'api.omnisend.com',
"path": '/v3/contacts',
@navdeepsingh
navdeepsingh / pagination.php
Created March 29, 2019 11:35
Custom pagination code for shortcode wordpress
function sponsor_pagination($pages = '', $range = 4)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
@navdeepsingh
navdeepsingh / commands.md
Last active July 3, 2019 05:52
EC2 SSH Commands

Check Status of file permissions

sudo stat TARGETFOLDER

Change Owner of File/Folder

sudo chown bitnami TARGETFOLDER

Check Status of all services

sudo /opt/bitnami/ctlscript.sh status

Run Apache

@navdeepsingh
navdeepsingh / Random-string
Created July 9, 2019 08:18 — forked from 6174/Random-string
Generate a random string in JavaScript In a short and fast way!
//http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);