Mouse Clock 1.1.0: online settings editor with copy/download, window.MouseClockOptions

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tangsongdayoandClaude Opus 5.5 committed 2026-11-20 15:45:56 +08:00
1 parent cfe6c1c3df
commit 3f9ac3e1de
3 files changed
+499 -19

No files matched your search

+13 -2
View File
@@ -10,14 +10,14 @@
- 分頁在背景時不計算;系統開啟「減少動態」的使用者預設不顯示。
- 可以用程式開關(`MouseClock.start()`、`MouseClock.stop()`)。
**線上範例:<https://toys.tsdylab.com/mouse-clock/>**
**線上編輯器:<https://toys.tsdylab.com/mouse-clock/>**——在網頁上調整顏色、大小、位置、速度,即時預覽,再一鍵複製或下載已經含設定的程式碼。不想手動改設定的話,從這裡開始最快。
## 檔案
| 檔案 | 用途 |
| --- | --- |
| `mouse-clock.js` | 時鐘本體,只需要這個檔案 |
| `index.html` | 範例頁,下載後用瀏覽器打開就能試 |
| `index.html` | 線上編輯器(調整設定、預覽、複製程式碼)。要用網站伺服器打開,「完整內嵌」與下載才讀得到 `mouse-clock.js` |
---
@@ -103,6 +103,17 @@ Medium、方格子、Notion、Facebook 這類平台不允許文章放自己的 J
| `data-z-index` | `9999` | 疊放順序;時鐘被網站的選單蓋住時調大 |
| `data-reduced-motion` | `hide` | 使用者在系統開啟「減少動態」時:`hide` 不顯示,`show` 照常顯示 |
### 用 `window.MouseClockOptions` 設定(1.1.0 起)
不方便加 `data-` 屬性時(例如用「完整內嵌」貼進文章),可以在載入 `mouse-clock.js` 之前先寫好設定,名稱用駝峰寫法(見下一節)。同時有 `data-` 屬性的話以 `data-` 屬性為準。線上編輯器產生的「完整內嵌」程式碼就是這種寫法。
```html
<script>
window.MouseClockOptions = { hourColor: '#b5532f', lang: 'en' };
</script>
<script src="/js/mouse-clock.js"></script>
```
### 用程式控制
在 `<script>` 加上 `data-manual`,時鐘就不會自動出現,改由你決定什麼時候啟動:
+479 -14
View File
@@ -3,24 +3,489 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>滑鼠立體時鐘 範例</title>
<title>滑鼠立體時鐘 線上編輯器</title>
<link rel="icon" href="data:,">
<meta name="description" content="調整顏色、大小、位置與速度,即時預覽會跟著滑鼠跑的立體時鐘,複製程式碼貼到自己的網站。">
<style>
body { font-family: system-ui, "Noto Sans TC", sans-serif; max-width: 640px; margin: 40px auto; padding: 0 16px; line-height: 1.7; color: #222; background: #fafafa; }
code { background: #eee; padding: 1px 5px; border-radius: 4px; }
button { font: inherit; padding: 6px 14px; margin: 4px 6px 4px 0; cursor: pointer; }
:root {
color-scheme: light dark;
--bg: #f6f5f1; --panel: #ffffff; --text: #222; --muted: #666; --line: #dcd8cf;
--accent: #9b452e; --accent-text: #fff; --code: #f1efe9; --focus: #2f6fd6; --ok: #2e7d4a;
}
@media (prefers-color-scheme: dark) {
:root:not([data-bg="light"]) {
--bg: #17181b; --panel: #222327; --text: #e9e7e2; --muted: #a5a29b; --line: #3a3b40;
--accent: #e08a6c; --accent-text: #1a1a1a; --code: #2b2c31; --focus: #7fb0ff; --ok: #7ccf97;
}
}
:root[data-bg="dark"] {
--bg: #17181b; --panel: #222327; --text: #e9e7e2; --muted: #a5a29b; --line: #3a3b40;
--accent: #e08a6c; --accent-text: #1a1a1a; --code: #2b2c31; --focus: #7fb0ff; --ok: #7ccf97;
color-scheme: dark;
}
:root[data-bg="light"] { color-scheme: light; }
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font-family: system-ui, "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif; background: var(--bg); color: var(--text); line-height: 1.6; overflow-x: hidden; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
header { padding: 20px 16px 4px; max-width: 1200px; margin: 0 auto; }
h1 { margin: 0; font-size: 1.6rem; }
header p { margin: 4px 0 0; color: var(--muted); }
.layout { display: grid; grid-template-columns: minmax(0, 380px) minmax(0, 1fr); gap: 20px; max-width: 1200px; margin: 0 auto; padding: 16px; align-items: start; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 4px 16px 16px; position: sticky; top: 12px; max-height: calc(100vh - 24px); overflow: auto; }
.panel > summary { list-style: none; cursor: pointer; font-weight: 700; padding: 12px 0 8px; display: flex; justify-content: space-between; align-items: center; }
.panel > summary::-webkit-details-marker { display: none; }
.panel > summary .hint { font-weight: 400; color: var(--muted); font-size: .85rem; }
fieldset { border: 0; border-top: 1px solid var(--line); margin: 0; padding: 10px 0 6px; min-width: 0; }
legend { font-weight: 700; padding: 0 6px 0 0; font-size: .95rem; }
.row { display: grid; grid-template-columns: 7.5em minmax(0, 1fr) 5.2em; gap: 8px; align-items: center; margin: 6px 0; }
.row label { font-size: .9rem; }
.row.color { grid-template-columns: 7.5em 44px minmax(0, 1fr); }
.row.select { grid-template-columns: 7.5em minmax(0, 1fr); }
input, select, button, textarea { font: inherit; color: inherit; }
input[type="number"], input[type="text"], select { width: 100%; min-width: 0; padding: 4px 6px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
input[type="range"] { width: 100%; min-width: 0; accent-color: var(--accent); }
input[type="color"] { width: 44px; height: 32px; padding: 2px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); cursor: pointer; }
input[aria-invalid="true"] { border-color: #d33; }
.presets { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 6px 12px; }
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 700; }
.swatch { display: inline-flex; gap: 2px; margin-right: 6px; vertical-align: -2px; }
.swatch i { width: 9px; height: 12px; border-radius: 2px; display: inline-block; box-shadow: inset 0 0 0 1px rgba(128,128,128,.45); }
.note { font-size: .85rem; color: var(--muted); margin: 6px 0; }
.warn { font-size: .85rem; color: var(--accent); margin: 6px 0; }
main { min-width: 0; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; margin-bottom: 20px; }
.card h2 { margin: 0 0 8px; font-size: 1.15rem; }
.play { min-height: 260px; display: grid; place-items: center; text-align: center; border: 2px dashed var(--line); border-radius: 12px; color: var(--muted); padding: 20px; }
.play strong { color: var(--text); font-size: 1.1rem; display: block; }
.bgswitch { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 10px; font-size: .9rem; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin: 4px 0 10px; }
.tabs [role="tab"][aria-selected="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 700; }
textarea { width: 100%; min-height: 150px; resize: vertical; font-family: ui-monospace, Consolas, "Cascadia Mono", monospace; font-size: .82rem; line-height: 1.5; background: var(--code); border: 1px solid var(--line); border-radius: 8px; padding: 10px; white-space: pre; overflow: auto; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; align-items: center; }
.status { color: var(--ok); font-size: .9rem; min-height: 1.4em; }
.card ol { padding-left: 1.4em; margin: 6px 0; }
code { background: var(--code); padding: 1px 5px; border-radius: 4px; font-size: .9em; }
footer { max-width: 1200px; margin: 0 auto; padding: 0 16px 40px; color: var(--muted); font-size: .85rem; }
@media (max-width: 760px) {
.layout { grid-template-columns: minmax(0, 1fr); padding: 12px 16px; }
.panel { position: static; max-height: none; }
.play { min-height: 220px; }
}
</style>
</head>
<body>
<h1>滑鼠立體時鐘</h1>
<p>移動滑鼠(手機上用手指滑動),時鐘會跟著你。</p>
<p>
<button type="button" onclick="MouseClock.start()">原版配色</button>
<button type="button" onclick="MouseClock.start({hourColor:'#b5532f',minuteColor:'#33568b',secondColor:'#3f8f57',faceColor:'#555',dateColor:'#999'})">柔和配色</button>
<button type="button" onclick="MouseClock.start({lang:'en'})">英文日期</button>
<button type="button" onclick="MouseClock.stop()">關閉時鐘</button>
</p>
<p>下載與放置教學:<a href="https://code.tsdylab.com/tangsongdayo/mouse-clock">code.tsdylab.com/tangsongdayo/mouse-clock</a></p>
<header>
<h1>滑鼠立體時鐘 線上編輯器</h1>
<p>調整設定,時鐘會馬上照新的樣子跟著你跑。滿意後複製程式碼,貼到自己的網站。</p>
</header>
<script src="mouse-clock.js"></script>
<div class="layout">
<details class="panel" id="panel" open>
<summary>設定 <span class="hint" id="panelHint">點這裡收合</span></summary>
<fieldset>
<legend>預設配色</legend>
<div class="presets" id="presets"></div>
<button type="button" id="reset">重設為預設</button>
</fieldset>
<fieldset>
<legend>顏色</legend>
<div id="colors"></div>
</fieldset>
<fieldset>
<legend>大小與速度</legend>
<div id="numbersMain"></div>
</fieldset>
<fieldset>
<legend>位置(電腦,相對滑鼠)</legend>
<div id="numbersMouse"></div>
</fieldset>
<fieldset>
<legend>位置(手機,相對手指)</legend>
<div id="numbersTouch"></div>
<p class="note">負數的垂直位置=在手指上方,避免被手掌擋住。</p>
</fieldset>
<fieldset>
<legend>其他</legend>
<div class="row select">
<label for="f-lang">日期語言</label>
<select id="f-lang" data-key="lang">
<option value="zh-TW">中文(2026年 十一月 20日 星期五)</option>
<option value="en">英文(FRIDAY 20 NOVEMBER 2026)</option>
</select>
</div>
<div class="row select">
<label for="f-reducedMotion">減少動態時</label>
<select id="f-reducedMotion" data-key="reducedMotion">
<option value="hide">不顯示(建議)</option>
<option value="show">照常顯示</option>
</select>
</div>
<div class="row select">
<label for="f-zIndex">疊放順序</label>
<input type="number" id="f-zIndex" data-key="zIndex" min="0" max="2147483647" step="1" inputmode="numeric">
</div>
<p class="note">疊放順序:時鐘被網站選單蓋住時調大。</p>
<p class="warn" id="rmNote" hidden>你的系統開了「減少動態」。這頁仍會顯示時鐘讓你預覽,但用「不顯示」設定時,在你的網站上不會出現。</p>
</fieldset>
</details>
<main>
<section class="card" aria-labelledby="playTitle">
<h2 id="playTitle">試玩區</h2>
<div class="play">
<div>
<strong>移動滑鼠,或用手指在畫面上滑動</strong>
時鐘會跟著你。電腦在游標右下方,手機在手指上方。
</div>
</div>
<div class="bgswitch" role="radiogroup" aria-label="預覽底色">
<span>預覽底色:</span>
<label><input type="radio" name="bg" value="auto" checked> 跟隨系統</label>
<label><input type="radio" name="bg" value="light"> 淺色網站</label>
<label><input type="radio" name="bg" value="dark"> 深色網站</label>
</div>
</section>
<section class="card" aria-labelledby="outTitle">
<h2 id="outTitle">取得程式碼</h2>
<div class="tabs" role="tablist" aria-label="程式碼形式">
<button type="button" role="tab" id="tab-tag" aria-controls="outPanel" aria-selected="true">一行引用(要上傳檔案)</button>
<button type="button" role="tab" id="tab-inline" aria-controls="outPanel" aria-selected="false" tabindex="-1">完整內嵌(直接貼上)</button>
</div>
<div id="outPanel" role="tabpanel" aria-labelledby="tab-tag">
<p class="note" id="outNote"></p>
<label for="code" class="note">程式碼(已含你的設定)</label>
<textarea id="code" readonly spellcheck="false"></textarea>
<div class="actions">
<button type="button" class="primary" id="copy">複製程式碼</button>
<button type="button" id="download">下載</button>
<button type="button" id="downloadJs" hidden>下載含設定的 .js</button>
<button type="button" id="share">複製分享連結</button>
<span class="status" id="status" role="status" aria-live="polite"></span>
</div>
</div>
</section>
<section class="card" aria-labelledby="howTitle">
<h2 id="howTitle">怎麼放到網站上</h2>
<ol>
<li>選上面的形式:能上傳檔案就用「一行引用」,先下載 <code>mouse-clock.js</code> 上傳到網站,再把那一行的 <code>src</code> 改成檔案位置;不能上傳就用「完整內嵌」。</li>
<li>把程式碼貼在網頁 <code>&lt;/body&gt;</code> 前面(或文章最後的 HTML 區塊)。</li>
<li>重新整理網頁,移動滑鼠就會看到時鐘。</li>
</ol>
<p class="note">WordPress、Typecho、Blogger、Hugo/Hexo 等平台的詳細放法,以及常見問題,請看 <a href="https://code.tsdylab.com/tangsongdayo/mouse-clock">Gitea 上的說明(README)</a>。Medium、方格子、Notion 這類不允許自訂 JavaScript 的平台無法使用。</p>
</section>
</main>
</div>
<footer>
原作:Silly Clock 3 by Kurt Grigg ・ 中文化:網頁建置百寶箱 ・ 改寫:<a href="https://tsdylab.com/">唐宋打油</a> ・
<a href="https://code.tsdylab.com/tangsongdayo/mouse-clock">原始碼與授權</a> ・ <span id="ver"></span>
</footer>
<script src="mouse-clock.js?v=1.1.0" data-manual></script>
<script>
(function () {
'use strict';
var MC = window.MouseClock;
var DEF = MC.defaults;
var STORE = 'mouse-clock-editor';
var KEYS = Object.keys(DEF);
var COLORS = [
['dateColor', '日期環'], ['faceColor', '鐘面數字'], ['hourColor', '時針'], ['minuteColor', '分針'], ['secondColor', '秒針']
];
var NUMS = {
numbersMain: [
['size', '大小(半徑)', 10, 150, 1, ''],
['speed', '跟隨速度', 0.05, 1, 0.05, ''],
['dateSpeed', '日期環轉速', 0, 0.1, 0.005, '']
],
numbersMouse: [
['offsetX', '水平', -300, 300, 1, 'px'],
['offsetY', '垂直', -300, 300, 1, 'px']
],
numbersTouch: [
['touchOffsetX', '水平', -300, 300, 1, 'px'],
['touchOffsetY', '垂直', -300, 300, 1, 'px']
]
};
var PRESETS = [
['原版', {}],
['柔和', { dateColor: '#999999', faceColor: '#555555', hourColor: '#b5532f', minuteColor: '#33568b', secondColor: '#3f8f57' }],
['夜光(深色網站)', { dateColor: '#8fa3b8', faceColor: '#e8e4d8', hourColor: '#ff8a65', minuteColor: '#7fb0ff', secondColor: '#9be38f' }],
['糖果', { dateColor: '#c58bd9', faceColor: '#ff7eb6', hourColor: '#ff5c8a', minuteColor: '#5cc8ff', secondColor: '#ffc24b' }],
['墨色', { dateColor: '#888888', faceColor: '#333333', hourColor: '#111111', minuteColor: '#444444', secondColor: '#9b452e' }]
];
var NUM_KEYS = ['size', 'offsetX', 'offsetY', 'touchOffsetX', 'touchOffsetY', 'speed', 'dateSpeed', 'zIndex'];
var state = {};
var mode = 'tag';
var source = null; // mouse-clock.js 原始碼(內嵌版用)
function $(id) { return document.getElementById(id); }
function clone(o) { var r = {}; for (var k in o) r[k] = o[k]; return r; }
function kebab(k) { return k.replace(/[A-Z]/g, function (c) { return '-' + c.toLowerCase(); }); }
function normColor(v) {
v = String(v || '').trim();
if (v.charAt(0) !== '#') v = '#' + v;
if (/^#[0-9a-f]{3}$/i.test(v)) v = '#' + v[1] + v[1] + v[2] + v[2] + v[3] + v[3];
return /^#[0-9a-f]{6}$/i.test(v) ? v.toLowerCase() : null;
}
function round(n) { return Math.round(n * 10000) / 10000; }
// 把任意來源的設定整理成合法值
function sanitize(input) {
var o = clone(DEF), k, v;
for (k in input || {}) {
if (!(k in DEF)) continue;
v = input[k];
if (/Color$/.test(k)) { v = normColor(v); if (v) o[k] = v; }
else if (NUM_KEYS.indexOf(k) >= 0) { v = Number(v); if (isFinite(v) && String(input[k]).trim() !== '') o[k] = k === 'zIndex' ? Math.max(0, Math.min(2147483647, Math.round(v))) : round(v); }
else if (k === 'lang') { if (v === 'en' || v === 'zh-TW') o[k] = v; }
else if (k === 'reducedMotion') { if (v === 'hide' || v === 'show') o[k] = v; }
}
return o;
}
function diff(o) {
var d = {};
KEYS.forEach(function (k) { if (o[k] !== DEF[k]) d[k] = o[k]; });
return d;
}
// ---- 建立表單 ----
COLORS.forEach(function (c) {
var row = document.createElement('div');
row.className = 'row color';
row.innerHTML = '<label for="f-' + c[0] + '">' + c[1] + '</label>' +
'<input type="color" id="f-' + c[0] + '-pick" aria-label="' + c[1] + '取色器" data-key="' + c[0] + '">' +
'<input type="text" id="f-' + c[0] + '" data-key="' + c[0] + '" maxlength="7" spellcheck="false" autocomplete="off">';
$('colors').appendChild(row);
});
Object.keys(NUMS).forEach(function (box) {
NUMS[box].forEach(function (n) {
var row = document.createElement('div');
row.className = 'row';
var unit = n[5] ? '(' + n[5] + ')' : '';
row.innerHTML = '<label for="f-' + n[0] + '">' + n[1] + unit + '</label>' +
'<input type="range" id="f-' + n[0] + '-range" aria-label="' + n[1] + '滑桿" data-key="' + n[0] + '" min="' + n[2] + '" max="' + n[3] + '" step="' + n[4] + '">' +
'<input type="number" id="f-' + n[0] + '" data-key="' + n[0] + '" min="' + n[2] + '" max="' + n[3] + '" step="' + n[4] + '" inputmode="decimal">';
$(box).appendChild(row);
});
});
PRESETS.forEach(function (p) {
var b = document.createElement('button');
b.type = 'button';
var c = sanitize(p[1]);
b.innerHTML = '<span class="swatch" aria-hidden="true">' + COLORS.map(function (x) { return '<i style="background:' + c[x[0]] + '"></i>'; }).join('') + '</span>' + p[0];
b.addEventListener('click', function () {
var next = clone(state);
COLORS.forEach(function (x) { next[x[0]] = c[x[0]]; });
apply(next, true);
say('已套用「' + p[0] + '」配色');
});
$('presets').appendChild(b);
});
function fill() {
document.querySelectorAll('[data-key]').forEach(function (el) {
var k = el.getAttribute('data-key');
if (el === document.activeElement && el.type !== 'color' && el.type !== 'range' && el.tagName !== 'SELECT') return;
el.value = state[k];
el.removeAttribute('aria-invalid');
});
}
var startTimer = null;
function apply(next, refill) {
state = sanitize(next);
if (refill) fill();
save();
render();
clearTimeout(startTimer);
startTimer = setTimeout(restart, 30);
}
function restart() {
var o = clone(state);
o.reducedMotion = 'show'; // 編輯器一律顯示預覽
MC.start(o);
}
document.querySelectorAll('[data-key]').forEach(function (el) {
var k = el.getAttribute('data-key');
el.addEventListener('input', function () {
var v = el.value, next = clone(state);
if (/Color$/.test(k)) {
var c = normColor(v);
if (!c) { el.setAttribute('aria-invalid', 'true'); return; }
el.removeAttribute('aria-invalid');
next[k] = c;
} else if (NUM_KEYS.indexOf(k) >= 0) {
if (v === '' || !isFinite(Number(v))) { el.setAttribute('aria-invalid', 'true'); return; }
el.removeAttribute('aria-invalid');
next[k] = Number(v);
} else next[k] = v;
state = sanitize(next);
// 同步同一個設定的另一個輸入框
document.querySelectorAll('[data-key="' + k + '"]').forEach(function (o) { if (o !== el) o.value = state[k]; });
save(); render();
clearTimeout(startTimer);
startTimer = setTimeout(restart, 30);
});
el.addEventListener('change', function () { fill(); });
});
$('reset').addEventListener('click', function () { apply({}, true); say('已重設為預設值'); });
// ---- 儲存與分享 ----
function save() { try { localStorage.setItem(STORE, JSON.stringify(diff(state))); } catch (e) {} }
function load() {
var fromHash = null;
if (location.hash.length > 1) {
try {
var p = new URLSearchParams(location.hash.slice(1)), o = {}, any = false;
KEYS.forEach(function (k) { if (p.has(kebab(k))) { o[k] = p.get(kebab(k)); any = true; } });
if (any) fromHash = o;
} catch (e) {}
}
if (fromHash) return fromHash;
try { return JSON.parse(localStorage.getItem(STORE) || '{}') || {}; } catch (e) { return {}; }
}
function shareUrl() {
var d = diff(state), p = new URLSearchParams();
Object.keys(d).forEach(function (k) { p.set(kebab(k), d[k]); });
var q = p.toString();
return location.href.split('#')[0] + (q ? '#' + q : '');
}
// ---- 輸出 ----
function tagCode() {
var d = diff(state), attrs = Object.keys(d).map(function (k) { return '\n data-' + kebab(k) + '="' + d[k] + '"'; }).join('');
return '<\!-- 滑鼠立體時鐘:先下載 mouse-clock.js 上傳到網站,再把 src 改成檔案位置 -->\n' +
'<script src="/js/mouse-clock.js"' + attrs + '><\/script>';
}
function optionsLine() {
var d = diff(state);
return 'window.MouseClockOptions = ' + JSON.stringify(d, null, 2) + ';\n';
}
function jsWithOptions() {
return '/* 你的設定(滑鼠立體時鐘線上編輯器產生)*/\n' + optionsLine() + '\n' + source;
}
function inlineCode() {
if (source === null) return '/* 正在讀取 mouse-clock.js… */';
if (source === false) return '/* 讀不到 mouse-clock.js,請改用「一行引用」,或從 Gitea 下載後自行貼上。 */';
return '<\!-- 滑鼠立體時鐘(完整內嵌版):貼在 </body> 前面 -->\n<script>\n' + jsWithOptions().replace(/<\/script/gi, '<\\/script') + '<\/script>';
}
function render() {
var code = mode === 'tag' ? tagCode() : inlineCode();
$('code').value = code;
$('outNote').textContent = mode === 'tag'
? '只寫出跟預設不同的設定。要先下載 mouse-clock.js(按「下載」)並上傳到你的網站,再把 src 改成實際位置。'
: '整份程式加上你的設定,不用上傳檔案,直接貼進網頁或文章的 HTML 區塊。開頭的原作者署名請保留。';
$('download').textContent = mode === 'tag' ? '下載 mouse-clock.js' : '下載 .html 片段';
$('downloadJs').hidden = mode !== 'inline';
}
function setMode(m, focus) {
mode = m;
['tag', 'inline'].forEach(function (x) {
var t = $('tab-' + x), on = x === m;
t.setAttribute('aria-selected', on ? 'true' : 'false');
t.tabIndex = on ? 0 : -1;
if (on && focus) t.focus();
});
$('outPanel').setAttribute('aria-labelledby', 'tab-' + m);
render();
}
$('tab-tag').addEventListener('click', function () { setMode('tag'); });
$('tab-inline').addEventListener('click', function () { setMode('inline'); });
document.querySelector('[role="tablist"]').addEventListener('keydown', function (e) {
if (e.key === 'ArrowRight' || e.key === 'ArrowLeft' || e.key === 'Home' || e.key === 'End') {
e.preventDefault();
setMode(mode === 'tag' ? 'inline' : 'tag', true);
}
});
var sayTimer = null;
function say(msg) {
$('status').textContent = msg;
clearTimeout(sayTimer);
sayTimer = setTimeout(function () { $('status').textContent = ''; }, 3000);
}
function copyText(text, okMsg) {
function fallback() {
var ta = $('code'), old = ta.value;
ta.value = text; ta.select();
var ok = false;
try { ok = document.execCommand('copy'); } catch (e) {}
ta.value = old;
say(ok ? okMsg : '無法自動複製,請手動選取複製');
}
if (navigator.clipboard && window.isSecureContext) navigator.clipboard.writeText(text).then(function () { say(okMsg); }, fallback);
else fallback();
}
function download(name, text, type) {
var blob = new Blob([text], { type: type + ';charset=utf-8' });
var a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = name;
document.body.appendChild(a); a.click(); a.remove();
setTimeout(function () { URL.revokeObjectURL(a.href); }, 1000);
say('已下載 ' + name);
}
$('copy').addEventListener('click', function () {
if (mode === 'inline' && !source) { say('還讀不到 mouse-clock.js'); return; }
copyText($('code').value, '已複製到剪貼簿');
});
$('download').addEventListener('click', function () {
if (!source) { say('還讀不到 mouse-clock.js'); return; }
if (mode === 'tag') download('mouse-clock.js', source, 'text/javascript');
else download('mouse-clock-snippet.html', inlineCode() + '\n', 'text/html');
});
$('downloadJs').addEventListener('click', function () {
if (!source) { say('還讀不到 mouse-clock.js'); return; }
download('mouse-clock.js', jsWithOptions(), 'text/javascript');
});
$('share').addEventListener('click', function () {
var url = shareUrl();
try { history.replaceState(null, '', url); } catch (e) {}
copyText(url, '已複製分享連結');
});
// ---- 預覽底色 ----
document.querySelectorAll('input[name="bg"]').forEach(function (r) {
r.addEventListener('change', function () {
if (r.value === 'auto') document.documentElement.removeAttribute('data-bg');
else document.documentElement.setAttribute('data-bg', r.value);
});
});
// ---- 面板收合提示 ----
$('panel').addEventListener('toggle', function () { $('panelHint').textContent = $('panel').open ? '點這裡收合' : '點這裡展開'; });
if (window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches) $('rmNote').hidden = false;
$('ver').textContent = '版本 ' + (MC.version || '');
state = sanitize(load());
fill();
render();
restart();
fetch('mouse-clock.js?v=' + encodeURIComponent(MC.version || ''))
.then(function (r) { if (!r.ok) throw new Error(r.status); return r.text(); })
.then(function (t) { source = t.replace(/\r\n/g, '\n'); if (!/\n$/.test(source)) source += '\n'; render(); })
.catch(function () { source = false; render(); });
})();
</script>
</body>
</html>
+7 -3
View File
@@ -1,5 +1,5 @@
/*!
* 滑鼠立體時鐘 Mouse Clock 1.0.0
* 滑鼠立體時鐘 Mouse Clock 1.1.0
* https://code.tsdylab.com/tangsongdayo/mouse-clock
*
* 原作:Silly Clock 3 by Kurt Grigg
@@ -177,11 +177,15 @@
}
var current = document.currentScript;
window.MouseClock = { start: start, stop: function () { if (running) running.stop(); }, defaults: DEFAULTS };
// 1.1.0:在載入前設定 window.MouseClockOptions = {...} 也可以調整(data-* 屬性優先)
var preset = window.MouseClockOptions;
window.MouseClock = { version: '1.1.0', start: start, stop: function () { if (running) running.stop(); }, defaults: DEFAULTS };
// 加上 data-manual 就不自動啟動,改由 MouseClock.start({...}) 啟動
if (!(current && current.dataset && current.dataset.manual !== undefined)) {
var opts = fromScriptTag(current);
var opts = {}, tagOpts = fromScriptTag(current), key;
if (preset && typeof preset === 'object') for (key in preset) opts[key] = preset[key];
for (key in tagOpts) opts[key] = tagOpts[key];
if (document.body) start(opts);
else document.addEventListener('DOMContentLoaded', function () { start(opts); });
}