Skip to content

Instantly share code, notes, and snippets.

@ginpei
ginpei / getCpuTemperature.win.js
Last active February 4, 2024 20:47
Get CPU temperature for Windows by Node.js
Math.round((require('child_process').execSync('wmic /namespace:\\\\root\\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature').toString().split('\r\n')[1].trim()) / 10 - 273)
@ginpei
ginpei / example.tsx
Created August 7, 2023 00:46
History system using React Redux
import { PayloadAction, configureStore, createSlice } from "@reduxjs/toolkit";
import type { NextPage } from "next";
import { Provider, useDispatch, useSelector } from "react-redux";
import undoable, { StateWithHistory, UndoableOptions } from "redux-undo";
// partial state for a section (undo target)
interface NumberState {
id: string;
title: string;
value: number;
import type { NextPage } from "next";
import { useState } from "react";
import { HStack } from "../../src/lib/layout/HStack";
import { VStack } from "../../src/lib/layout/VStack";
import { Button } from "../../src/lib/style/Button";
import { H1 } from "../../src/lib/style/H1";
import { H2 } from "../../src/lib/style/H2";
interface Task {
done: boolean;
@ginpei
ginpei / CounterPage.tsx
Last active May 6, 2023 00:01
Use React context with reducer
import React, {
createContext, useReducer, useContext, useCallback,
} from 'react';
type CounterState = { count: number }
type CounterAction =
| { type: 'increment'; data: { amount: number } }
| { type: 'reset' }
@ginpei
ginpei / filter.js
Created December 29, 2022 11:40
Dim off items without keywords in Amazon search result
var words = twotabsearchtextbox.value.split(" ");
[...document.querySelectorAll(".s-result-item")]
.filter((v) => !words.every((w) => v.textContent.toLowerCase().includes(w)))
.forEach((v) => v.style.opacity = "0.3")
@ginpei
ginpei / .js
Last active October 6, 2022 21:18
Measure Express.js middleware runtime performance
const app = express();
const _use = app.use;
let numUseCalls = 0;
app.use = function(...args) {
const fn = args[args.length - 1];
// except `express.static()`
if (fn && fn.name === 'serveStatic') {
_use.apply(this, args);
// About API:
// https://www.dropbox.com/developers/core/docs#oa2-authorize
// https://www.dropbox.com/developers/core/docs#oa2-token
var config = require('./config.json');
// OR...
// var config = {
// 'appKey': 'xxxxxxxxxxxxxxx',
// 'secretKey': 'xxxxxxxxxxxxxxx'
// };
@ginpei
ginpei / Gruntfile.js
Created December 17, 2012 02:00
`grunt.loadTasks('foo')` loads all JS files under directory `foo`, not `tasks/foo.js` with grunt v0.4.0rc2.
// ./Gruntfile.js
module.exports = function(grunt) {
grunt.loadTasks('hoge');
};
((document, limit) => {
const data = Array.from(document.querySelectorAll('*'))
.map((el) => ({zIndex: Number(getComputedStyle(el).zIndex), element: el }))
.filter(({ zIndex }) => !isNaN(zIndex))
.sort((r1, r2) => r2.zIndex - r1.zIndex)
.slice(0, limit);
console.table(data);
})(document, 50);