Skip to content

Instantly share code, notes, and snippets.

View hansamlin's full-sized avatar
🎯
Focusing

Sam Lin hansamlin

🎯
Focusing
View GitHub Profile
@hansamlin
hansamlin / search.css
Last active October 2, 2023 08:58
Google custom search by react.js
#search {
margin: auto;
}
#search-container {
width: 500px;
height: 300px;
margin: auto;
}
server {
listen 3000;
charset utf-8;
error_page 404 500 /404.html;
location / {
rewrite ^(.+)/$ $1 permanent;
index index.html;
root /my/project/path;
@hansamlin
hansamlin / nginx conf
Created December 3, 2020 08:26
filter sql query
set $block_sql_injections 0;
if ($query_string ~* "(select|concat|case|sleep|md5|count\()") {
set $block_sql_injections 1;
}
if ($query_string ~* "\(case") {
set $block_sql_injections 1;
}
@hansamlin
hansamlin / strip
Created January 6, 2021 06:08
Remove html tag via js
function strip(html){
let doc = new DOMParser().parseFromString(html, 'text/html');
return doc.body.textContent || "";
}
function bubbleSort(arr) {
for(var i = 0; i < arr.length; i++) {
if(arr[i] > arr[i + 1]) {
var tmp = arr[i + 1];
arr[i + 1] = arr[i];
arr[i] = tmp;
}
}
@hansamlin
hansamlin / nginx.conf
Created September 9, 2021 09:03
Example Nginx configuration for serving pre-rendered HTML from Javascript pages/apps using the Prerender Service (https://github.com/collectiveip/prerender).Instead of using try_files (which can cause unnecessary overhead on busy servers), you could check $uri for specific file extensions and set $prerender appropriately.
# Note (November 2016):
# This config is rather outdated and left here for historical reasons, please refer to prerender.io for the latest setup information
# Serving static html to Googlebot is now considered bad practice as you should be using the escaped fragment crawling protocol
server {
listen 80;
listen [::]:80;
server_name yourserver.com;
root /path/to/your/htdocs;
@hansamlin
hansamlin / Messages.js
Created September 13, 2021 08:49 — forked from Gpx/Messages.js
Check if user is logged in Express otherwise redirect to /login
// Set an error message and redirect
var redirectWithMessage = function (message, url, req, res) {
req.session.messages = message;
res.redirect(url);
};
// Return 'messages' value or null instead
var getMessages = function (req) {
var messages = req.session.messages || null;
delete req.session.messages;
import { useEffect } from 'react'
import Script from 'next/script'
import { useRouter } from 'next/router'
import * as gtag from './gtag'
const App = ({ Component, pageProps }) => {
const router = useRouter()
useEffect(() => {
const handleRouteChange = (url) => {
gtag.pageview(url)
function getArrayBuffer(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.addEventListener('load', () => {
resolve(reader.result);
});
reader.addEventListener('error', () => {
reject();
});
function _get(obj, path, _default) {
if (typeof obj !== "object") {
throw new Error("param 1 must be an object")
}
if (typeof path !== "string" && !Array.isArray(path)) {
throw new Error("param 2 must be an object or a string")
}
let _path = path