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;
@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 / web-storage.ts
Created February 1, 2020 11:21
localStorage, sessionStorage のごく薄いラッパー。Cookie 無効環境でストレージにアクセスしたときに発生する例外を捕捉しスクリプトが止まらないようにする。
/**
* @fileOverview
* localStorage, sessionStorage のごく薄いラッパー。
* Cookie 無効環境でストレージにアクセスしたときに発生する例外を捕捉しスクリプトが止まらないようにする。
*/
type storageType = 'localStorage' | 'sessionStorage'
class WebStorage {
constructor(private storageType: storageType) {}
function rollFocus(backFlag) {
let focusIndex
const currentIndex = this.tabbableElements.indexOf(document.activeElement)
if (currentIndex < 0) {
focusIndex = 0
} else {
const tabbableLength = this.tabbableElements.length
const direction = backFlag ? -1 : 1
focusIndex = (currentIndex + direction + tabbableLength) % tabbableLength
}