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 / ChromeExtensionContentScript.js
Created May 29, 2019 17:16
Chrome Extension Content Script Access Global Variables & manipulate DOM
function executeOnPageSpace(code){
// create a script tag
var script = document.createElement('script')
script.id = 'tmpScript'
// place the code inside the script. later replace it with execution result.
script.textContent =
'document.getElementById("tmpScript").textContent = JSON.stringify(' + code + ')'
// attach the script to page
document.documentElement.appendChild(script)
@kumarasinghe
kumarasinghe / ajax_form_post.js
Last active April 14, 2020 07:09
AJAX HTML form POST
/*
Invalid data will cause the server to silently reject your POST request.
Analyze the POST request on form submission via Chrome Dev tools.
*/
(async () => {
// create a form data object
let form = new FormData()
@kumarasinghe
kumarasinghe / wvdiald.pl
Last active August 30, 2022 16:15
wvdial daemon : Auto Connect USB modems / Mobile Broadbands on Linux / Raspberry Pi
#!/usr/bin/perl -l
# wvdiald.pl - Auto connect USB modems on Linux
# Author: naveenk <dndkumarasnghe@gmail.com>
# License: MIT
# Make sure you have wvdial and usb-modeswitch installed. Fill the connection parameters below.
# Run this as root. Add "perl /path/to/wvdiald.pl" to /etc/rc.local to make this a service
######################### CHANGE AS NEEDED ################################
# connection parameters
@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`;
@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 / 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 / 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 / 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 / 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 / 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;
}