Skip to content

Instantly share code, notes, and snippets.

View lewdev's full-sized avatar

Lewis Nakao lewdev

View GitHub Profile
@lewdev
lewdev / refresh-youtube
Created February 6, 2019 20:19
Allows you to completely reload the YouTube page and start where you left off. Create a shortcut and enter this into the "URL" field.
javascript: (function () {
var re = window.location.href.match(/v=([^\?&"'>]+)/g);
if (re && re.length > 0) {
window.location='https://youtu.be/' + re[0].split('=')[1] + "?t=" + Math.round(document.getElementById('movie_player').getCurrentTime());
}
})();
@lewdev
lewdev / getFactors.js
Last active July 24, 2021 08:58
Given a number, this method returns an ordered array of its factors.
const getFactors = num => {
const maxFactorNum = Math.floor(Math.sqrt(num));
const factorArr = [];
let count = 0; // count of factors found < maxFactorNum
for (let i = 1; i <= maxFactorNum; i++) {
//inserting new elements in the middle using splice
if (num % i === 0) {
//(index, how many to remove, inserted element)
factorArr.splice(count, 0, i);
@lewdev
lewdev / isPrime.js
Last active February 20, 2019 03:21
JavaScript Prime Number test
function isPrime(num) {
//check if value is a natural numbers (integer)
//without this check, it returns true
if (isNaN(num) || num % 1 !== 0) {
return false;
}
num = Math.abs(num); //*negative values can be primes
if (num === 0 || num === 1) {
return false;
}
@lewdev
lewdev / Timer.js
Last active February 20, 2019 00:38
Simple timer object for calculating run-time with option to display the time in seconds.
/**
Example JS:
const timer = new Timer();
timer.start();
timer.stop();
Example HTML:
<span id="timerDisplay"><!-- not required --></span>
<button onclick="timer.start();">start</button>
<button onclick="timer.stop();">stop</button
@lewdev
lewdev / css-only-tabs.html
Last active March 26, 2019 22:15
My simple and easy-to-understand implementation of CSS-only Tabs.
<style>
/* Hide radio buttons */
input[type="radio"][name="css-tab-rdo"] {
display: none;
}
/* Show selected tab contents */
#tab1Rdo:checked ~ #tab1 { display: block; }
#tab2Rdo:checked ~ #tab2 { display: block; }
@lewdev
lewdev / sqlite-query-column-names.sql
Created April 4, 2019 21:40
An SQLite query that looks for a column name and also provides some additional column information too.
SELECT
m.name AS table_name,
p.cid AS col_id,
p.name AS col_name,
p.type AS col_type,
p.pk AS col_is_pk,
p.dflt_value AS col_default_val,
p.[notnull] AS col_is_not_null
FROM sqlite_master m
LEFT OUTER JOIN pragma_table_info((m.name)) p
@lewdev
lewdev / sqlite-query-table-columns.sql
Created April 4, 2019 21:42
SQLite query to get column data by rows with additional information about each column.
SELECT
m.name AS table_name,
p.cid AS col_id,
p.name AS col_name,
p.type AS col_type,
p.pk AS col_is_pk,
p.dflt_value AS col_default_val,
p.[notnull] AS col_is_not_null
FROM sqlite_master m
LEFT OUTER JOIN pragma_table_info((m.name)) p
@lewdev
lewdev / minimal-js-html-template.html
Created May 28, 2020 22:22
A minimal JavaScript/HTML page for testing out some JavaScript. I always get warnings for the charset not being there.
<!doctype html><html><head><meta charset="utf-8"><title>Template</title></head>
<body>
<div id="output"></div>
<script>
window.onload = function() {
var i
, row
, output = document.getElementById("output")
, size = data.list.length
, arr = []
@lewdev
lewdev / extractHostname
Last active November 5, 2020 11:03
extractHostname & extractRootHostname extracts the domain name and the root domain name too for common URLs.
//See also my answer on StackOverflow: https://stackoverflow.com/a/23945027/1675237
const extractHostname = url => {
//find & remove protocol (http, ftp, etc.) and get hostname
let hostname = url.split('/')[url.indexOf("//") > -1 ? 2 : 0];
//find & remove port number
hostname = hostname.split(':')[0];
//find & remove "?"
return hostname.split('?')[0];
};
@lewdev
lewdev / KeyPressed.html
Created November 8, 2020 11:06
Which key has been pressed in JavaScript / HTML.
<!--
My StackOverflow answer to "How to find out what character key is pressed?":
https://stackoverflow.com/a/51955727/1675237
-->
<table>
<tr><td>Key:</td><td id="key"></td></tr>
<tr><td>Key Code:</td><td id="keyCode"></td></tr>
<tr><td>Event Code:</td><td id="eventCode"></td></tr>
</table>
<script type="text/javascript">