ポモドーロするUserScript
code:import.js
import '/api/code/yosider-scripts/ポモドーロするUserScript/script.js';
code:script.js
const intervalPeriod = 25*60*1000;
let isRunning = false;
let interval = undefined;
let endTime = undefined;
scrapbox.PageMenu.addMenu({
title: 'Pomodoro Timer',
});
scrapbox.PageMenu('Pomodoro Timer').addItem({
title: () => (isRunning)? '⏹Stop timer' : '▶Start timer',
onClick: () => {
if (isRunning) {
isRunning = false;
clearInterval(interval);
startTime = undefined;
}
else {
isRunning = true;
interval = setInterval(TimeUp, intervalPeriod);
endTime = new Date();
endTime.setTime(endTime.getTime() + intervalPeriod);
}
},
});
scrapbox.PageMenu('Pomodoro Timer').addItem({
title: () => (isRunning)? restTime() : '---',
onClick: () => {},
});
function TimeUp() {
// タイマーをクリア
isRunning = false;
clearInterval(interval);
alert('🍅お時間です。');
}
const zero = n => String(n).padStart(2, '0');
function restTime() {
const now = new Date();
const rest = endTime.getTime() - now.getTime();
const seconds = Math.round((rest % (60 * 1000)) / 1000);
const minutes = Math.floor(rest / (60 * 1000));
return ${minutes}'${zero(seconds)}'' remain.;
}