Skip to content

Instantly share code, notes, and snippets.

View kumarasinghe's full-sized avatar

Naveen Kumarasinghe kumarasinghe

  • Colombo, Sri Lanka
View GitHub Profile
@kumarasinghe
kumarasinghe / elementor-template-organize.sh
Created September 16, 2023 21:32
Organize elementor template archives for previewing
# ORGANIZE ELEMENTOR TEMPLATE ARCHIVES FOR PREVIEWING
# RUN THIS BASH SCRIPT NEXT TO ZIP ARCHIVES
mkdir -p templates preview archives
i=100
# iterate archives
for archive in *.zip; do
archive_name=${archive%%.zip}
echo "processing $archive_name..."
@kumarasinghe
kumarasinghe / health.js
Created April 13, 2023 17:54
Node.js health check route
import os from "os";
import process from "process";
/* GET /api/health */
const stats = {
status: "OK",
system: {
hostname: os.hostname(),
free_memory_mb: Math.floor(os.freemem() / 1000000),
load_avg: os.loadavg(),
@kumarasinghe
kumarasinghe / tsconfig.json
Created April 11, 2023 20:00
tsconfig with sensible defaults
{
"compilerOptions": {
/***** BASIC OPTIONS *****/
/* target ECMAScript version */
"target": "esnext",
/* use ECMAScript module system */
"module": "esnext",
/* treat files as isolated independent modules */
"isolatedModules": true,
@kumarasinghe
kumarasinghe / html-css-switch.html
Last active December 5, 2020 09:55
Minimal HTML CSS Switch
<style>
.switch input{
display: none;
}
.switch label{
background-color: grey;
}
.switch input:checked + label{
background-color: red;
}
@kumarasinghe
kumarasinghe / getMacAddress.js
Last active October 27, 2020 12:44
Get a MAC address unique to the device in Node.js
getMACAddress() {
let vmMACPrefixes = [
"00:50:56",
"00:0C:29",
"00:05:69",
"00:1C:14",
"0A:00:27",
"00:03:FF",
"00:1C:42",
"00:0F:4B",
@kumarasinghe
kumarasinghe / urldownload.js
Created June 19, 2020 11:17
JavaScript download a URL as file in browser
// CORS policies apply
async function download(url, filename){
let res = await fetch(url)
let blob = await res.blob()
let a = document.createElement("a");
a.href = URL.createObjectURL(blob)
a.download = filename;
a.click();
}
@kumarasinghe
kumarasinghe / linkedin-course-downloader.js
Last active June 25, 2020 02:30
LinkedIn Course Downloader Script
// go to the course page, press F12 and run this code in Java Script console.
// once done you get a list of video download links. Right click and save them or,
// download all at once with a tool of your choice (e.g. Chrome Simple Mass Downloader)
(async () => {
let SELECTOR_CHAPTER_EXPANDERS = '.classroom-toc-chapter__toggle'
let SELECTOR_LESSONS = '[data-control-name="toc_item"]'
let VIDEO_LOAD_WAIT = 3000
// expand chapters on sidebar except the first
@kumarasinghe
kumarasinghe / sw.js
Last active June 11, 2020 19:21
Service Worker Template Example
const CACHE_VERSION = 'cache-v1' // increment this when you update the web site
const filesToCache = [
"./index.html",
"./path/an/image.png",
// add more static assets to cache
]
self.addEventListener(
'install',
@kumarasinghe
kumarasinghe / readFileBottomUp.js
Created May 30, 2020 09:50
NodeJS : Reading a part of a file bottom to up and return the lines
/*
reads a file upwards from the given 'startPosition' to the specified length and
returns an object with the lines and the final position read
if 'startPosition' is not specified it will be the very bottom of the file
*/
async function readFileBottomUp(filename, length, startPosition) {
return new Promise((resolve, reject) => {
// file does not exist
@kumarasinghe
kumarasinghe / torrentstatus.pl
Created April 30, 2020 10:40
Cron Script to email torrent download progress
#!/usr/bin/perl
my $path = "/folder/to/monitor";
my $mailCMD = "/usr/bin/msmtp";
my $mailTo = 'you@gmail.com';
chdir $path;
my @files = `du -hs * | cut -f2`;
my @actualSizes = `du -hs * | cut -f1`;
my @apparentSizes = `du -hs --apparent-size * | cut -f1`;