ai生成-计算器
script dog · v1.0.0 · 独立窗口 · 9 次安装
简介
此脚本有AI助手生成,以作演示。
匹配网址
*://*/*
脚本源码
(function () {
const rootId = 'dog-calculator-root';
const styleId = 'dog-calculator-style';
if (typeof window.__dogCalculatorCleanup === 'function') {
try {
window.__dogCalculatorCleanup();
} catch (e) {}
}
const oldRoot = document.getElementById(rootId);
if (oldRoot) oldRoot.remove();
const css = `
.dog-calculator *,
#dog-calculator-root * {
box-sizing: border-box;
}
.dog-calculator,
#dog-calculator-root.dog-calculator {
display: block !important;
visibility: visible !important;
opacity: 1 !important;
position: relative !important;
z-index: 999999 !important;
width: 340px;
max-width: calc(100vw - 24px);
margin: 28px auto !important;
padding: 20px !important;
border-radius: 28px !important;
background:
radial-gradient(120% 120% at 0% 0%, rgba(34, 211, 238, 0.13), transparent 42%),
radial-gradient(120% 120% at 100% 0%, rgba(129, 140, 248, 0.13), transparent 46%),
linear-gradient(145deg, rgba(15, 23, 42, 0.97), rgba(2, 6, 23, 0.96)) !important;
border: 1px solid rgba(148, 163, 184, 0.18) !important;
box-shadow:
0 24px 60px rgba(2, 6, 23, 0.52),
0 0 0 1px rgba(34, 211, 238, 0.04),
inset 0 1px rgba(255, 255, 255, 0.08) !important;
font-family: "SF Pro Display", "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif !important;
color: #e5e7eb !important;
user-select: none !important;
pointer-events: auto !important;
overflow: hidden;
}
.dog-calculator::before,
#dog-calculator-root.dog-calculator::before {
content: "";
position: absolute;
inset: 0;
pointer-events: none;
background:
linear-gradient(90deg, rgba(34, 211, 238, 0.08), transparent 38%),
linear-gradient(270deg, rgba(129, 140, 248, 0.08), transparent 38%);
opacity: 0.7;
}
.dog-calculator:focus-visible,
#dog-calculator-root.dog-calculator:focus-visible {
outline: none !important;
border-color: rgba(34, 211, 238, 0.58) !important;
box-shadow:
0 24px 60px rgba(2, 6, 23, 0.52),
0 0 0 1px rgba(34, 211, 238, 0.22),
inset 0 1px rgba(255, 255, 255, 0.08),
0 0 28px rgba(34, 211, 238, 0.16) !important;
}
.dog-calc-screen {
position: relative;
background:
radial-gradient(130% 100% at 85% 15%, rgba(34, 211, 238, 0.12), transparent 45%),
radial-gradient(120% 90% at 12% 88%, rgba(129, 140, 248, 0.10), transparent 42%),
linear-gradient(135deg, rgba(2, 6, 23, 0.82), rgba(15, 23, 42, 0.64)) !important;
border: 1px solid rgba(148, 163, 184, 0.16) !important;
border-radius: 22px !important;
padding: 16px 18px 20px !important;
margin-bottom: 18px !important;
min-height: 104px !important;
display: flex !important;
flex-direction: column !important;
justify-content: flex-end !important;
align-items: flex-end !important;
overflow: hidden !important;
box-shadow:
inset 0 0 28px rgba(34, 211, 238, 0.07),
0 12px 26px rgba(2, 6, 23, 0.22) !important;
transition: border-color 0.18s ease, box-shadow 0.18s ease, background 0.18s ease;
}
.dog-calc-screen::after {
content: "";
position: absolute;
top: -35%;
right: -20%;
width: 180px;
height: 180px;
background: radial-gradient(circle, rgba(34, 211, 238, 0.12), transparent 68%);
filter: blur(12px);
pointer-events: none;
opacity: 0.55;
}
.dog-calc-expression {
font-size: 14px !important;
line-height: 1.35 !important;
color: rgba(148, 163, 184, 0.86) !important;
min-height: 18px !important;
max-width: 100% !important;
white-space: nowrap !important;
overflow: hidden !important;
text-overflow: ellipsis !important;
letter-spacing: 0.03em !important;
text-align: right !important;
}
.dog-calc-current {
font-size: 42px !important;
font-weight: 600 !important;
line-height: 1.05 !important;
color: #e0f2fe !important;
max-width: 100% !important;
white-space: nowrap !important;
overflow: hidden !important;
text-overflow: ellipsis !important;
text-align: right !important;
text-shadow: 0 0 18px rgba(34, 211, 238, 0.28), 0 2px 8px rgba(2, 6, 23, 0.45) !important;
letter-spacing: -0.02em !important;
transition: color 0.18s ease, text-shadow 0.18s ease;
}
.dog-calc-error.dog-calc-screen {
border-color: rgba(248, 113, 113, 0.42) !important;
box-shadow:
inset 0 0 30px rgba(248, 113, 113, 0.10),
0 12px 26px rgba(2, 6, 23, 0.22) !important;
}
.dog-calc-error .dog-calc-expression {
color: rgba(253, 164, 175, 0.92) !important;
}
.dog-calc-error .dog-calc-current {
color: #fecaca !important;
text-shadow: 0 0 20px rgba(248, 113, 113, 0.36), 0 2px 8px rgba(2, 6, 23, 0.45) !important;
}
.dog-calc-keypad {
display: grid !important;
grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
gap: 12px !important;
}
.dog-calc-btn {
appearance: none !important;
-webkit-appearance: none !important;
position: relative;
width: 100%;
height: 60px;
border: 1px solid rgba(148, 163, 184, 0.16) !important;
border-radius: 16px !important;
background:
linear-gradient(180deg, rgba(51, 65, 85, 0.92), rgba(15, 23, 42, 0.96)) !important;
color: #f8fafc !important;
font-family: inherit !important;
font-size: 20px !important;
font-weight: 500 !important;
line-height: 1 !important;
cursor: pointer !important;
display: block;
padding: 0;
margin: 0;
box-shadow:
0 8px 20px rgba(2, 6, 23, 0.24),
inset 0 1px rgba(255, 255, 255, 0.07) !important;
transition:
transform 0.10s ease,
border-color 0.18s ease,
box-shadow 0.18s ease,
filter 0.18s ease,
background-color 0.18s ease;
outline: none !important;
z-index: 1;
}
.dog-calc-btn::before {
content: "";
position: absolute;
inset: -1px;
border-radius: inherit;
background: linear-gradient(135deg, rgba(34, 211, 238, 0.10), rgba(129, 140, 248, 0.06));
opacity: 0;
pointer-events: none;
transition: opacity 0.18s ease;
}
.dog-calc-btn:hover::before {
opacity: 1;
}
.dog-calc-btn:hover {
transform: translateY(-1px);
border-color: rgba(34, 211, 238, 0.28) !important;
box-shadow:
0 12px 26px rgba(2, 6, 23, 0.28),
0 0 18px rgba(34, 211, 238, 0.07),
inset 0 1px rgba(255, 255, 255, 0.10) !important;
filter: brightness(1.12);
}
.dog-calc-btn:active {
transform: translateY(1px) scale(0.98);
border-color: rgba(34, 211, 238, 0.38) !important;
box-shadow:
0 4px 12px rgba(2, 6, 23, 0.24),
inset 0 0 14px rgba(34, 211, 238, 0.08),
inset 0 1px rgba(255, 255, 255, 0.06) !important;
filter: brightness(1.05);
}
.dog-calc-btn:focus-visible {
border-color: rgba(34, 211, 238, 0.58) !important;
box-shadow:
0 0 0 2px rgba(34, 211, 238, 0.18),
0 10px 22px rgba(2, 6, 23, 0.28),
inset 0 1px rgba(255, 255, 255, 0.08) !important;
}
.dog-calc-btn-num {
background:
linear-gradient(180deg, rgba(15, 23, 42, 0.94), rgba(2, 6, 23, 0.98)) !important;
color: #e5e7eb !important;
}
.dog-calc-btn-num:hover {
background:
linear-gradient(180deg, rgba(30, 41, 59, 0.96), rgba(15, 23, 42, 0.98)) !important;
}
.dog-calc-btn-func {
background:
linear-gradient(180deg, rgba(51, 65, 85, 0.86), rgba(15, 23, 42, 0.94)) !important;
color: #a5f3fc !important;
}
.dog-calc-btn-func:hover {
background:
linear-gradient(180deg, rgba(71, 85, 105, 0.88), rgba(30, 41, 59, 0.96)) !important;
}
.dog-calc-btn-op {
background:
linear-gradient(180deg, #38bdf8, #0284c7) !important;
color: #082032 !important;
font-weight: 700 !important;
border-color: rgba(56, 189, 248, 0.32) !important;
box-shadow:
0 8px 22px rgba(2, 6, 23, 0.24),
0 0 18px rgba(56, 189, 248, 0.18),
inset 0 1px rgba(255, 255, 255, 0.18) !important;
}
.dog-calc-btn-op:hover {
background:
linear-gradient(180deg, #7dd3fc, #0ea5e9) !important;
border-color: rgba(125, 211, 252, 0.44) !important;
box-shadow:
0 12px 26px rgba(2, 6, 23, 0.28),
0 0 22px rgba(56, 189, 248, 0.24),
inset 0 1px rgba(255, 255, 255, 0.22) !important;
}
.dog-calc-btn-eq {
background:
linear-gradient(135deg, #22d3ee, #818cf8) !important;
color: #071018 !important;
font-weight: 800 !important;
border-color: rgba(129, 140, 248, 0.34) !important;
box-shadow:
0 10px 26px rgba(2, 6, 23, 0.30),
0 0 22px rgba(34, 211, 238, 0.18),
inset 0 1px rgba(255, 255, 255, 0.22) !important;
}
.dog-calc-btn-eq:hover {
background:
linear-gradient(135deg, #67e8f9, #a5b4fc) !important;
border-color: rgba(165, 180, 252, 0.46) !important;
box-shadow:
0 14px 30px rgba(2, 6, 23, 0.34),
0 0 26px rgba(34, 211, 238, 0.24),
inset 0 1px rgba(255, 255, 255, 0.26) !important;
}
@media (max-width: 420px) {
.dog-calculator,
#dog-calculator-root.dog-calculator {
margin: 14px auto !important;
padding: 16px !important;
border-radius: 24px !important;
}
.dog-calc-screen {
min-height: 96px !important;
padding: 14px 16px 18px !important;
margin-bottom: 14px !important;
}
.dog-calc-current {
font-size: 36px !important;
}
.dog-calc-btn {
height: 56px;
border-radius: 14px !important;
font-size: 19px !important;
}
}
`;
function injectDomStyle() {
if (document.getElementById(styleId)) return;
const style = document.createElement('style');
style.id = styleId;
style.textContent = css;
const target = document.head || document.body || document.documentElement;
if (target) {
target.appendChild(style);
} else {
setTimeout(injectDomStyle, 50);
}
}
injectDomStyle();
if (typeof Dog !== 'undefined' && typeof Dog.addStyle === 'function') {
if (!window.__dogCalculatorDogStyleInjected) {
try {
Dog.addStyle(css);
window.__dogCalculatorDogStyleInjected = true;
} catch (e) {}
}
}
const root = document.createElement('div');
root.id = rootId;
root.className = 'dog-calculator';
root.tabIndex = 0;
root.setAttribute('aria-label', 'Dog Calculator');
root.style.setProperty('display', 'block', 'important');
root.style.setProperty('visibility', 'visible', 'important');
root.style.setProperty('opacity', '1', 'important');
root.style.setProperty('position', 'relative', 'important');
root.style.setProperty('z-index', '999999', 'important');
root.style.setProperty('user-select', 'none', 'important');
root.style.setProperty('pointer-events', 'auto', 'important');
root.innerHTML = `
<div class="dog-calc-screen">
<div class="dog-calc-expression"></div>
<div class="dog-calc-current">0</div>
</div>
<div class="dog-calc-keypad"></div>
`;
function mount() {
if (document.body) {
if (document.getElementById(rootId) !== root) {
document.body.appendChild(root);
}
} else {
setTimeout(mount, 50);
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', mount, { once: true });
} else {
mount();
}
const screen = root.querySelector('.dog-calc-screen');
const expressionEl = root.querySelector('.dog-calc-expression');
const currentEl = root.querySelector('.dog-calc-current');
const keypad = root.querySelector('.dog-calc-keypad');
const buttons = [
{ label: 'AC', value: 'clear', className: 'dog-calc-btn-func' },
{ label: '⌫', value: 'backspace', className: 'dog-calc-btn-func' },
{ label: '%', value: 'percent', className: 'dog-calc-btn-func' },
{ label: '÷', value: '/', className: 'dog-calc-btn-op' },
{ label: '7', value: '7', className: 'dog-calc-btn-num' },
{ label: '8', value: '8', className: 'dog-calc-btn-num' },
{ label: '9', value: '9', className: 'dog-calc-btn-num' },
{ label: '×', value: '*', className: 'dog-calc-btn-op' },
{ label: '4', value: '4', className: 'dog-calc-btn-num' },
{ label: '5', value: '5', className: 'dog-calc-btn-num' },
{ label: '6', value: '6', className: 'dog-calc-btn-num' },
{ label: '−', value: '-', className: 'dog-calc-btn-op' },
{ label: '1', value: '1', className: 'dog-calc-btn-num' },
{ label: '2', value: '2', className: 'dog-calc-btn-num' },
{ label: '3', value: '3', className: 'dog-calc-btn-num' },
{ label: '+', value: '+', className: 'dog-calc-btn-op' },
{ label: '±', value: 'sign', className: 'dog-calc-btn-func' },
{ label: '0', value: '0', className: 'dog-calc-btn-num' },
{ label: '.', value: '.', className: 'dog-calc-btn-num' },
{ label: '=', value: 'equals', className: 'dog-calc-btn-eq' }
];
buttons.forEach(function (item) {
const btn = document.createElement('button');
btn.type = 'button';
btn.className = 'dog-calc-btn ' + item.className;
btn.textContent = item.label;
btn.dataset.value = item.value;
keypad.appendChild(btn);
});
let current = '0';
let previous = null;
let operator = null;
let resetNext = true;
let expression = '';
let error = false;
function opSymbol(op) {
if (op === '*') return '×';
if (op === '/') return '÷';
if (op === '-') return '−';
return op;
}
function formatNumber(value) {
if (!isFinite(value)) return 'Error';
if (value === 0) return '0';
let str = String(value);
if (str.length > 15) {
const rounded = Number(value.toPrecision(12));
str = String(rounded);
}
return str;
}
function updateDisplay() {
currentEl.textContent = current;
expressionEl.textContent = expression || '\u00A0';
screen.classList.toggle('dog-calc-error', error);
}
function resetAll() {
current = '0';
previous = null;
operator = null;
resetNext = true;
expression = '';
error = false;
updateDisplay();
}
function evaluate(a, b, op) {
const x = parseFloat(a);
const y = parseFloat(b);
if (isNaN(x) || isNaN(y)) {
return { ok: false };
}
let result;
switch (op) {
case '+':
result = x + y;
break;
case '-':
result = x - y;
break;
case '*':
result = x * y;
break;
case '/':
if (y === 0) {
return { ok: false, error: true };
}
result = x / y;
break;
default:
return { ok: false };
}
return {
ok: true,
result: formatNumber(result)
};
}
function calculate() {
if (operator === null || previous === null) return false;
const res = evaluate(previous, current, operator);
if (!res.ok) {
error = true;
current = 'Error';
expression = 'Error';
operator = null;
previous = null;
resetNext = true;
updateDisplay();
return false;
}
current = res.result;
previous = res.result;
operator = null;
resetNext = true;
return true;
}
function inputDigit(d) {
if (error) resetAll();
if (resetNext) {
current = d;
resetNext = false;
} else {
current = current === '0' ? d : current + d;
}
updateDisplay();
}
function inputDecimal() {
if (error) resetAll();
if (resetNext) {
current = '0.';
resetNext = false;
} else if (current.indexOf('.') === -1) {
current += '.';
}
updateDisplay();
}
function inputOperator(op) {
if (error) return;
if (operator !== null && previous !== null) {
if (!resetNext) {
if (!calculate()) return;
} else {
operator = op;
expression = previous + ' ' + opSymbol(op);
updateDisplay();
return;
}
}
previous = current;
operator = op;
resetNext = true;
expression = previous + ' ' + opSymbol(op);
updateDisplay();
}
function equals() {
if (error) return;
if (operator !== null && previous !== null) {
expression = previous + ' ' + opSymbol(operator) + ' ' + current + ' =';
if (!calculate()) {
updateDisplay();
return;
}
expression = current;
operator = null;
previous = null;
resetNext = true;
updateDisplay();
} else {
expression = current;
resetNext = true;
updateDisplay();
}
}
function backspace() {
if (error) {
resetAll();
return;
}
if (resetNext) {
current = '0';
resetNext = false;
} else if (current.length > 1) {
current = current.slice(0, -1);
if (current === '-' || current === '-0') {
current = '0';
}
} else {
current = '0';
resetNext = true;
}
updateDisplay();
}
function toggleSign() {
if (error) return;
if (resetNext) {
current = '0';
resetNext = false;
}
if (current === '0' || current === '0.') return;
if (current.charAt(0) === '-') {
current = current.slice(1);
} else {
current = '-' + current;
}
updateDisplay();
}
function percent() {
if (error) return;
const value = parseFloat(current);
if (isNaN(value)) return;
current = formatNumber(value / 100);
resetNext = false;
updateDisplay();
}
function handle(value) {
if (/^[0-9]$/.test(value)) {
inputDigit(value);
} else if (value === '.') {
inputDecimal();
} else if (value === '+' || value === '-' || value === '*' || value === '/') {
inputOperator(value);
} else if (value === 'equals') {
equals();
} else if (value === 'clear') {
resetAll();
} else if (value === 'backspace') {
backspace();
} else if (value === 'sign') {
toggleSign();
} else if (value === 'percent') {
percent();
}
}
keypad.addEventListener('click', function (e) {
const btn = e.target.closest('button');
if (!btn) return;
handle(btn.dataset.value);
});
root.addEventListener('keydown', function (e) {
if (e.ctrlKey || e.metaKey || e.altKey) return;
const ae = document.activeElement;
if (
ae &&
(
ae.tagName === 'INPUT' ||
ae.tagName === 'TEXTAREA' ||
ae.tagName === 'SELECT' ||
ae.isContentEditable
)
) {
return;
}
const key = e.key;
if (key >= '0' && key <= '9') {
inputDigit(key);
} else if (key === '.') {
inputDecimal();
} else if (key === '+' || key === '-' || key === '*' || key === '/') {
inputOperator(key);
} else if (key === 'Enter' || key === '=') {
e.preventDefault();
equals();
} else if (key === 'Backspace') {
e.preventDefault();
backspace();
} else if (key === 'Escape' || key === 'Delete') {
resetAll();
} else if (key === '%') {
percent();
}
});
window.__dogCalculatorCleanup = function () {
const r = document.getElementById(rootId);
if (r) r.remove();
const s = document.getElementById(styleId);
if (s) s.remove();
};
updateDisplay();
})();