Skip to content

Instantly share code, notes, and snippets.

View nilsandrey's full-sized avatar
🏠

Nils nilsandrey

🏠
View GitHub Profile
@nilsandrey
nilsandrey / Layout1.tsx
Last active May 5, 2022 06:58
NextJS page layouts
import React from 'react';
function Layout1({ children }) {
return (
<>
<main>{children}</main>
<footer>
By you
</footer>
</>
@nilsandrey
nilsandrey / Lan.tsx
Created May 3, 2022 20:30
i18n useTranslation().t wrapper with placeholder loader from MUI
import { Skeleton } from '@mui/material';
import { useTranslation } from 'next-i18next';
import * as React from 'react';
export interface Props {
textId: string;
width?: string;
}
/**
@nilsandrey
nilsandrey / countTwitterShares.js
Last active May 1, 2022 03:11
How many times a web link has been shared on Twitter
// <https://www.barattalo.it/coding/how-many-times-a-web-link-has-been-shared-on-twitter/>
function readTwitterShares($url) {
$s = file_get_contents("http://urls.api.twitter.com/1/urls/count.json".
"?callback=?&url=".urlencode($url));
preg_match("#(\"count\"):([0-9]*)#",$s,$ar);
return isset($ar[2]) ? $ar[2] : 0;
}
echo readTwitterShares("https://www.nationalgeographic.com/science/article/a-psychedelic-surprise-may-be-thriving-in-your-local-garden");
@nilsandrey
nilsandrey / next.config.js
Last active April 24, 2022 03:58
Use of config.resolve.alias in NextJS config
// Copied for easier bookmarking from <https://github.com/vercel/next.js/issues/706#issuecomment-569041997> by @Lwdthe1
// next.config.js
const aliasPathsToResolve = [
{ name: 'components', path: path.resolve(__dirname, './components') },
{ name: 'Common', path: path.resolve(__dirname, '../../common/react/') },
]
module.exports = () => {
return {
webpack(config, { defaultLoaders }) {
config.module.rules.push({
@nilsandrey
nilsandrey / Date.ext.ts
Last active April 22, 2022 05:09
Extensions to the TypeScript Date class to work with diferences to a future date.
export {};
/**
* Extensions to the Typescript Date class.
*/
declare global {
/**
* Extension members declarations.
*/
interface Date {
@nilsandrey
nilsandrey / HN Avatars.js
Created April 20, 2022 05:29
User script to add avatars to HackerNews pages. Taken from "Show HN".
let observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry, i) => {
if (entry.isIntersecting) {
const p = 2;
const c = document.createElement('canvas');
const x = c.getContext('2d');
c.width = 18;
c.height = 14;
const s = entry.target.innerText;
@nilsandrey
nilsandrey / node-static-serve.js
Last active April 13, 2022 13:40
NodeJS based directory browsing and file serve without dependencies
// NodeJS based directory browsing and file serve without dependencies
// Joining code from <https://stackoverflow.com/questions/16333790/node-js-quick-file-server-static-files-over-http>
var fs = require("fs"),
http = require("http");
http
.createServer(function (req, res) {
// Website you wish to allow to connect
res.setHeader("Access-Control-Allow-Origin", "*");
@nilsandrey
nilsandrey / String.format.js
Last active May 17, 2022 15:55
string.format in Javascript.
// <https://stackoverflow.com/a/4673436/2100126>
// First, checks if it isn't implemented yet.
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
@nilsandrey
nilsandrey / SendToTypefull-bookmarklet.js
Last active February 9, 2022 12:17
Use this code on a new bookmark to send current page to a new Typefully draft.
javascript:(function(){f='https://typefully.com/?new='+encodeURIComponent(document.title)+'%250A'+encodeURIComponent(window.location.href);a=function(){if(!window.open(f))location.href=f};if(/Firefox/.test(navigator.userAgent)){setTimeout(a,0)}else{a()}})()