Skip to content

Instantly share code, notes, and snippets.

View tsmd's full-sized avatar

Shimada Takayuki tsmd

View GitHub Profile
@tsmd
tsmd / prevent-history-back-dialog.html
Last active January 11, 2024 11:48
Prevent history back PoC
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form</title>
</head>
<body>
<p>
URL: <input id="url" type="text" readonly style="width:60em">
@tsmd
tsmd / lib.js
Created September 25, 2023 23:10
Optimize SVG Icons
const { XMLParser, XMLBuilder } = require('fast-xml-parser');
const presentationAttrs = [
'clip-path', 'clip-rule', 'color', 'color-interpolation', 'color-rendering',
'cursor', 'display', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'mask',
'opacity', 'pointer-events', 'shape-rendering', 'stroke', 'stroke-dasharray',
'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit',
'stroke-opacity', 'stroke-width', 'transform', 'vector-effect', 'visibility',
];
let pageVisible = true;
function updatePageVisibility(e) {
if (e.type === "visibilitychange") {
pageVisible = !document.hidden;
} else {
const typeVisibilityMap = { blur: false, focus: true, pagehide: false, pageshow: true };
pageVisible = typeVisibilityMap[e.type];
}
}
export function waitForGlobalVariable(objPath, interval, timeout, callback) {
function find(objPath) {
var found = window;
objPath = objPath.split(".");
while (objPath.length > 0) {
var objPart = objPath.shift();
if (objPart in found) {
found = found[objPart];
} else {
return null;
@tsmd
tsmd / debounce.js
Created March 6, 2021 07:47
simple throttle and debounce
/**
* @param {number} delay
* @param {Function} callback
* @returns {Function}
*/
function debounce(delay, callback) {
var timer = 0;
return function () {
var self = this;
var args = arguments;
@tsmd
tsmd / load-polyfills.html
Created December 29, 2020 21:28
Load polyfills conditionally from CDN
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Load polyfills conditionally from CDN</title>
<script>
function loadFromCdn(src, integrity) {
var s = document.createElement("script");
s.src = src;
s.integrity = integrity;
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no" id="meta-viewport">
<script>
!function() {
var viewport = document.getElementById('meta-viewport');
function onResize () {
var value = window.outerWidth < 375 ? "width=375" : "width=device-width,initial-scale=1,shrink-to-fit=no";
if (viewport.getAttribute('content') !== value) {
viewport.setAttribute('content', value);
}
}
@tsmd
tsmd / dom-utils.js
Created October 22, 2020 08:04
DOM utilities
/**
* ツリー上で最初に登場するテキストノードを深さ優先で探索し取得する
*/
function getFirstTextNode(root) {
return traverse(root, (node) => node.nodeType === Node.TEXT_NODE && node.textContent.trim() !== "");
}
/**
* ツリー上で最後に登場するテキストノードを深さ優先で探索し取得する
*/
@tsmd
tsmd / adjust-rem.js
Last active September 5, 2020 14:02
rem を 10/16 倍する
#!/usr/bin/env node
/**
* @fileoverview rem を 10/16 倍する
* @example cat styles.css | ./adjust-rem > styles.css
*/
const { readFileSync } - require("fs");
const postcss = require("postcss");
@tsmd
tsmd / is-pinch-zooming.js
Last active February 28, 2020 03:18
Detect pinch zooming
function isPinchZooming () {
const clientWidth = document.documentElement.clientWidth
const viewportWidth = window.visualViewport ? window.visualViewport.width : window.innerWidth
return clientWidth > viewportWidth
}