// ==UserScript== // @name 内容信息检查 // @description 网页元素检查 // @namespace content-info-check // @version 1.0.0 // @match *://*/* // @run-at document-idle // @noframes // @grant GM_getValue // @grant GM_setValue // @grant GM_registerMenuCommand // @grant GM_addValueChangeListener // @license MIT // ==/UserScript== /* * 内容信息检查 — ScriptCat 用户脚本 * 版本:1.0.0 * 用途:在网页右下角挂载「捕获」按钮,查看并复制元素信息;脚本内提供设置与版本履历。 */ (function () { 'use strict'; if (window.__ciiLoaded) return; window.__ciiLoaded = true; var VERSION = '1.0.0'; var STORAGE_KEY = 'cii_enabled'; var HOST_ID = 'cii-shadow-host'; var MAX_TEXT = 500; var HISTORY = [ { version: '1.0.0', date: '2026-11-30', latest: true, added: [ 'ScriptCat 用户脚本版本', '脚本内设置页面', '脚本内版本履历页面' ], fixed: [ '若干已知问题' ] }, { version: '1.0.0-beta1', date: '2026-11-29', latest: false, added: [ '元素信息捕获功能', '页面右下角“捕获”按钮', '元素信息查看面板', '一键复制全部信息', '设置页面开关', '版本履历页面' ], fixed: [ '无(首个测试版本)' ] } ]; /* ---------------- 设置项存储 ---------------- */ function readEnabled() { try { if (typeof GM_getValue === 'function') return GM_getValue(STORAGE_KEY, false) === true; } catch (e) {} try { return window.localStorage.getItem(STORAGE_KEY) === '1'; } catch (e) {} return false; } function persistEnabled(value) { var saved = false; try { if (typeof GM_setValue === 'function') { GM_setValue(STORAGE_KEY, !!value); saved = true; } } catch (e) {} if (!saved) { try { window.localStorage.setItem(STORAGE_KEY, value ? '1' : '0'); } catch (e) {} } } function watchEnabled(callback) { try { if (typeof GM_addValueChangeListener === 'function') { GM_addValueChangeListener(STORAGE_KEY, function (name, oldValue, newValue) { callback(!!newValue); }); return; } } catch (e) {} window.addEventListener('storage', function (event) { if (event.key !== STORAGE_KEY) return; callback(event.newValue === '1'); }); } /* ---------------- 样式 ---------------- */ var STYLE = [ ':host { all: initial; }', '* { box-sizing: border-box; }', '.cii-root, .cii-fab, .cii-hint, .cii-panel, .cii-toast, .cii-set {', ' font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;', ' color: #111827;', '}', '.cii-fab {', ' position: fixed; right: calc(14px + env(safe-area-inset-right, 0px)); bottom: calc(16px + env(safe-area-inset-bottom, 0px));', ' display: inline-flex; align-items: center; justify-content: center;', ' height: 44px; min-width: 44px; padding: 0 18px; border: 0; border-radius: 999px;', ' background: #111827; color: #ffffff; font-size: 14px; font-weight: 600; letter-spacing: .02em;', ' box-shadow: 0 6px 18px rgba(15, 23, 42, .30); cursor: pointer;', ' z-index: 2147483646; -webkit-tap-highlight-color: transparent;', ' transition: background .15s ease, transform .12s ease;', '}', '.cii-fab:hover { background: #1f2937; }', '.cii-fab:active { transform: scale(.96); }', '.cii-fab[data-mode="picking"] { background: #b91c1c; }', '.cii-fab[data-mode="picking"]:hover { background: #991b1b; }', '.cii-hint {', ' position: fixed; top: calc(12px + env(safe-area-inset-top, 0px)); left: 50%; transform: translateX(-50%);', ' max-width: calc(100vw - 28px); padding: 9px 14px; border-radius: 999px;', ' background: rgba(17, 24, 39, .92); color: #ffffff; font-size: 12px; line-height: 1.2;', ' white-space: nowrap; overflow: hidden; text-overflow: ellipsis; pointer-events: none;', ' box-shadow: 0 6px 16px rgba(15, 23, 42, .25); z-index: 2147483646; display: none;', '}', '.cii-hl {', ' position: fixed; border: 2px solid #2563eb; background: rgba(37, 99, 235, .10);', ' border-radius: 3px; pointer-events: none; z-index: 2147483645; display: none;', '}', '.cii-hl-tag {', ' position: absolute; left: -2px; max-width: 60vw; padding: 3px 6px; border-radius: 4px;', ' background: #2563eb; color: #ffffff; font-size: 11px; line-height: 1.2;', ' white-space: nowrap; overflow: hidden; text-overflow: ellipsis;', '}', '.cii-mask {', ' position: fixed; inset: 0; background: rgba(15, 23, 42, .32);', ' z-index: 2147483647; display: none;', '}', '.cii-panel {', ' position: fixed; left: 50%; bottom: 0; transform: translateX(-50%);', ' width: 100%; max-width: 560px; max-height: 78vh; display: none; flex-direction: column;', ' background: #ffffff; color: #111827; border-radius: 18px 18px 0 0;', ' box-shadow: 0 -10px 40px rgba(15, 23, 42, .26); z-index: 2147483647; overflow: hidden;', '}', '.cii-panel.open { display: flex; }', '.cii-panel-head {', ' display: flex; align-items: center; justify-content: space-between; gap: 12px;', ' padding: 12px 16px; border-bottom: 1px solid #eef0f3;', '}', '.cii-panel-title { font-size: 15px; font-weight: 600; }', '.cii-close {', ' display: inline-flex; align-items: center; justify-content: center;', ' min-height: 40px; padding: 0 10px; border: 0; border-radius: 10px; background: transparent;', ' color: #6b7280; font-size: 13px; cursor: pointer; -webkit-tap-highlight-color: transparent;', '}', '.cii-close:hover { background: #f3f4f6; color: #111827; }', '.cii-panel-body { padding: 4px 16px 8px; overflow-y: auto; -webkit-overflow-scrolling: touch; min-height: 0; }', '.cii-row { padding: 10px 0; border-bottom: 1px dashed #eef0f3; }', '.cii-row:last-child { border-bottom: 0; }', '.cii-row-k { margin-bottom: 4px; font-size: 11px; font-weight: 600; letter-spacing: .06em; color: #9aa1ad; }', '.cii-row-v {', ' font-size: 13px; line-height: 1.6; color: #111827; word-break: break-word; overflow-wrap: anywhere;', ' user-select: text; -webkit-user-select: text;', '}', '.cii-row-v.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-size: 12px; }', '.cii-panel-foot {', ' display: flex; align-items: center; justify-content: space-between; gap: 10px;', ' padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px));', ' border-top: 1px solid #eef0f3; background: #ffffff;', '}', '.cii-copy {', ' height: 44px; padding: 0 20px; border: 0; border-radius: 999px; background: #2563eb;', ' color: #ffffff; font-size: 14px; font-weight: 600; cursor: pointer; -webkit-tap-highlight-color: transparent;', '}', '.cii-copy:active { transform: scale(.97); }', '.cii-again {', ' height: 44px; padding: 0 18px; border: 1px solid #d7dbe0; border-radius: 999px;', ' background: #ffffff; color: #374151; font-size: 14px; cursor: pointer; -webkit-tap-highlight-color: transparent;', '}', '.cii-again:hover { background: #f9fafb; }', '.cii-toast {', ' position: fixed; left: 50%; top: calc(14px + env(safe-area-inset-top, 0px));', ' transform: translate(-50%, -8px); padding: 9px 16px; border-radius: 999px;', ' background: rgba(17, 24, 39, .94); color: #ffffff; font-size: 13px;', ' opacity: 0; pointer-events: none; transition: opacity .18s ease, transform .18s ease; z-index: 2147483647;', '}', '.cii-toast.show { opacity: 1; transform: translate(-50%, 0); }', '@media (min-width: 720px) {', ' .cii-panel { bottom: 24px; border-radius: 18px; max-height: 70vh; }', ' .cii-panel-foot { padding-bottom: 12px; }', '}', /* 设置层 */ '.cii-set {', ' position: fixed; inset: 0; display: none; align-items: center; justify-content: center;', ' padding: 18px; padding-top: calc(18px + env(safe-area-inset-top, 0px));', ' padding-bottom: calc(18px + env(safe-area-inset-bottom, 0px));', ' background: rgba(15, 23, 42, .45); z-index: 2147483647;', '}', '.cii-set.open { display: flex; }', '.cii-set-card {', ' display: flex; flex-direction: column; width: 100%; max-width: 560px; max-height: 88vh;', ' background: #ffffff; border-radius: 20px; box-shadow: 0 24px 60px rgba(15, 23, 42, .35); overflow: hidden;', '}', '.cii-set-head {', ' display: flex; align-items: center; justify-content: space-between; gap: 12px;', ' padding: 14px 18px; border-bottom: 1px solid #eef0f3; flex: none;', '}', '.cii-set-title { font-size: 16px; font-weight: 650; letter-spacing: .01em; }', '.cii-set-body { padding: 16px 18px calc(18px + env(safe-area-inset-bottom, 0px)); overflow-y: auto; min-height: 0; }', '.cii-set-ver { margin: 0 0 13px; font-size: 12px; color: #9aa1ad; letter-spacing: .04em; }', '.cii-set-row {', ' display: flex; align-items: center; justify-content: space-between; gap: 14px; width: 100%;', ' padding: 16px; margin-bottom: 12px; background: #ffffff; border: 1px solid #e6e8eb; border-radius: 16px;', ' color: #111827; font: inherit; text-align: left; cursor: pointer; -webkit-tap-highlight-color: transparent;', ' transition: border-color .15s ease;', '}', '.cii-set-row:hover { border-color: #d5d9df; }', '.cii-set-main { display: flex; flex-direction: column; gap: 4px; min-width: 0; }', '.cii-set-row-title { font-size: 15px; font-weight: 600; }', '.cii-set-row-sub { font-size: 12px; color: #6b7280; }', '.cii-switch { position: relative; width: 48px; height: 28px; border-radius: 999px; background: #d5d9df; flex: none; transition: background .18s ease; }', '.cii-knob { position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #ffffff; box-shadow: 0 1px 3px rgba(0, 0, 0, .25); transition: transform .18s ease; }', '.cii-switch-row[aria-checked="true"] .cii-switch { background: #2563eb; }', '.cii-switch-row[aria-checked="true"] .cii-knob { transform: translateX(20px); }', '.cii-chev { font-size: 20px; line-height: 1; color: #9aa1ad; flex: none; }', '.cii-set-note { margin: 14px 2px 0; font-size: 12px; line-height: 1.75; color: #6b7280; }', '.cii-set-history { display: none; padding: 14px 18px calc(18px + env(safe-area-inset-bottom, 0px)); overflow-y: auto; min-height: 0; }', '.cii-set-history.open { display: block; }', '.cii-set-back { border: 0; background: none; color: #2563eb; font-size: 13px; padding: 6px 0; cursor: pointer; -webkit-tap-highlight-color: transparent; }', '.cii-set-htitle { margin: 8px 0 5px; font-size: 19px; font-weight: 650; }', '.cii-set-hnote { margin: 0 0 14px; font-size: 12px; line-height: 1.7; color: #6b7280; }', '.cii-ver { background: #ffffff; border: 1px solid #e6e8eb; border-radius: 14px; overflow: hidden; margin-bottom: 10px; }', '.cii-ver-head {', ' display: flex; align-items: center; justify-content: space-between; gap: 12px; width: 100%;', ' padding: 14px; background: transparent; border: 0; color: #111827; font: inherit; text-align: left; cursor: pointer;', ' -webkit-tap-highlight-color: transparent;', '}', '.cii-ver-title { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; min-width: 0; }', '.cii-ver-no { font-size: 14px; font-weight: 650; }', '.cii-tag { padding: 2px 7px; border-radius: 999px; background: #eaf0ff; color: #2563eb; font-size: 11px; font-weight: 600; }', '.cii-ver-date { font-size: 12px; color: #6b7280; }', '.cii-ver-chev { color: #9aa1ad; font-size: 16px; line-height: 1; flex: none; transition: transform .18s ease; }', '.cii-ver.open .cii-ver-chev { transform: rotate(90deg); }', '.cii-ver-body { display: none; padding: 2px 14px 14px; border-top: 1px dashed #eef0f3; }', '.cii-ver.open .cii-ver-body { display: block; }', '.cii-grp { margin-top: 12px; }', '.cii-grp h4 { margin: 0 0 6px; font-size: 11px; font-weight: 600; letter-spacing: .08em; color: #9aa1ad; }', '.cii-grp ul { margin: 0; padding-left: 18px; }', '.cii-grp li { font-size: 13px; line-height: 1.75; }', '.cii-grp li::marker { color: #2563eb; }', '@media (max-width: 560px) {', ' .cii-set { padding: 10px; align-items: flex-start; }', ' .cii-set-card { max-height: 94vh; }', '}' ].join('\n'); /* ---------------- 状态 ---------------- */ var host = null; var shadow = null; var ui = null; var enabled = false; var picking = false; var settingsOpen = false; var currentInfo = null; var lastHover = null; var toastTimer = 0; var observer = null; var rafPending = false; function canRun() { var root = document.documentElement; if (!root) return false; if (root.namespaceURI && root.namespaceURI !== 'http://www.w3.org/1999/xhtml') return false; return true; } function makeEl(tag, className, text) { var el = document.createElement(tag); if (className) el.className = className; if (text != null) el.textContent = text; return el; } /* ---------------- 构建界面 ---------------- */ function buildSettings() { var set = makeEl('div', 'cii-set'); set.setAttribute('tabindex', '-1'); var card = makeEl('div', 'cii-set-card'); var head = makeEl('div', 'cii-set-head'); var close = makeEl('button', 'cii-close', '关闭'); close.type = 'button'; head.appendChild(makeEl('div', 'cii-set-title', '内容信息检查')); head.appendChild(close); var body = makeEl('div', 'cii-set-body'); body.appendChild(makeEl('p', 'cii-set-ver', 'v' + VERSION)); var switchRow = makeEl('button', 'cii-set-row cii-switch-row'); switchRow.type = 'button'; switchRow.setAttribute('role', 'switch'); switchRow.setAttribute('aria-checked', 'false'); switchRow.setAttribute('aria-label', '页面捕获按钮开关'); var switchMain = makeEl('span', 'cii-set-main'); switchMain.appendChild(makeEl('span', 'cii-set-row-title', '页面捕获按钮')); var switchState = makeEl('span', 'cii-set-row-sub', '已关闭'); switchMain.appendChild(switchState); var switchBox = makeEl('span', 'cii-switch'); switchBox.appendChild(makeEl('span', 'cii-knob')); switchRow.appendChild(switchMain); switchRow.appendChild(switchBox); var historyRow = makeEl('button', 'cii-set-row'); historyRow.type = 'button'; var historyMain = makeEl('span', 'cii-set-main'); historyMain.appendChild(makeEl('span', 'cii-set-row-title', '版本履历')); historyMain.appendChild(makeEl('span', 'cii-set-row-sub', '按版本查看新增与修复')); historyRow.appendChild(historyMain); historyRow.appendChild(makeEl('span', 'cii-chev', '›')); var note = makeEl('p', 'cii-set-note', '开启后,支持的网页右下角会出现圆角“捕获”按钮;浏览器内置页面与受限页面无法使用。'); body.appendChild(switchRow); body.appendChild(historyRow); body.appendChild(note); var historyView = makeEl('div', 'cii-set-history'); var historyHead = makeEl('div'); var back = makeEl('button', 'cii-set-back', '‹ 返回'); back.type = 'button'; historyHead.appendChild(back); historyHead.appendChild(makeEl('div', 'cii-set-htitle', '版本履历')); historyHead.appendChild(makeEl('p', 'cii-set-hnote', '仅记录每个版本的新增与修复概览,不含详细说明。')); var historyList = makeEl('div', 'cii-set-list'); historyView.appendChild(historyHead); historyView.appendChild(historyList); card.appendChild(head); card.appendChild(body); card.appendChild(historyView); set.appendChild(card); return { set: set, setClose: close, body: body, switchRow: switchRow, switchState: switchState, historyRow: historyRow, historyView: historyView, historyBack: back, historyList: historyList }; } function buildUI() { var root = makeEl('div', 'cii-root'); var fab = makeEl('button', 'cii-fab', '捕获'); fab.type = 'button'; fab.setAttribute('data-mode', 'idle'); fab.setAttribute('aria-label', '捕获元素'); var hint = makeEl('div', 'cii-hint', '轻点页面元素进行捕获,点“取消”退出'); hint.setAttribute('role', 'status'); var hl = makeEl('div', 'cii-hl'); var hlTag = makeEl('span', 'cii-hl-tag'); hl.appendChild(hlTag); var mask = makeEl('div', 'cii-mask'); var panel = makeEl('section', 'cii-panel'); panel.setAttribute('role', 'dialog'); panel.setAttribute('aria-modal', 'true'); panel.setAttribute('aria-label', '元素信息'); var panelHead = makeEl('header', 'cii-panel-head'); var close = makeEl('button', 'cii-close', '关闭'); close.type = 'button'; panelHead.appendChild(makeEl('div', 'cii-panel-title', '元素信息')); panelHead.appendChild(close); var panelBody = makeEl('div', 'cii-panel-body'); var foot = makeEl('footer', 'cii-panel-foot'); var copy = makeEl('button', 'cii-copy', '复制内容'); copy.type = 'button'; var again = makeEl('button', 'cii-again', '重新选择'); again.type = 'button'; foot.appendChild(copy); foot.appendChild(again); panel.appendChild(panelHead); panel.appendChild(panelBody); panel.appendChild(foot); var toast = makeEl('div', 'cii-toast'); toast.setAttribute('role', 'status'); var settings = buildSettings(); root.appendChild(fab); root.appendChild(hint); root.appendChild(hl); root.appendChild(mask); root.appendChild(panel); root.appendChild(toast); root.appendChild(settings.set); return { root: root, refs: { fab: fab, hint: hint, hl: hl, hlTag: hlTag, mask: mask, panel: panel, panelBody: panelBody, panelClose: close, copy: copy, again: again, toast: toast, set: settings.set, setClose: settings.setClose, setBody: settings.body, switchRow: settings.switchRow, switchState: settings.switchState, historyRow: settings.historyRow, historyView: settings.historyView, historyBack: settings.historyBack, historyList: settings.historyList } }; } function applyStyle(target) { try { if (typeof CSSStyleSheet === 'function') { var sheet = new CSSStyleSheet(); sheet.replaceSync(STYLE); target.adoptedStyleSheets = [sheet]; return; } } catch (e) {} var styleEl = document.createElement('style'); styleEl.textContent = STYLE; target.appendChild(styleEl); } /* ---------------- 挂载 / 卸载 ---------------- */ function mount() { if (host) { if (!host.isConnected && document.documentElement) document.documentElement.appendChild(host); return; } host = document.createElement('div'); host.id = HOST_ID; host.style.cssText = 'all: initial;'; shadow = host.attachShadow({ mode: 'open' }); applyStyle(shadow); var built = buildUI(); shadow.appendChild(built.root); ui = built.refs; ui.fab.addEventListener('click', onFabClick); ui.panelClose.addEventListener('click', closePanel); ui.mask.addEventListener('click', closePanel); ui.copy.addEventListener('click', onCopyClick); ui.again.addEventListener('click', onAgainClick); ui.setClose.addEventListener('click', closeSettings); ui.set.addEventListener('click', function (event) { if (event.target === ui.set) closeSettings(); }); ui.switchRow.addEventListener('click', function () { setEnabled(!enabled); }); ui.historyRow.addEventListener('click', function () { showSettingsView('history'); }); ui.historyBack.addEventListener('click', function () { showSettingsView('main'); }); shadow.addEventListener('keydown', onShadowKeydown, true); document.documentElement.appendChild(host); startObserver(); } function destroy() { stopPicking(); closePanel(); if (host) { host.remove(); host = null; shadow = null; ui = null; } currentInfo = null; settingsOpen = false; if (observer) { observer.disconnect(); observer = null; } } function startObserver() { if (observer || !window.MutationObserver || !document.documentElement) return; observer = new MutationObserver(function () { if (host && !host.isConnected && document.documentElement) { document.documentElement.appendChild(host); } }); observer.observe(document.documentElement, { childList: true }); } /* ---------------- 设置 ---------------- */ function refreshSwitch() { if (!ui) return; ui.switchRow.setAttribute('aria-checked', enabled ? 'true' : 'false'); ui.switchState.textContent = enabled ? '已开启' : '已关闭'; } function showSettingsView(name) { if (!ui) return; var isHistory = name === 'history'; ui.setBody.style.display = isHistory ? 'none' : 'block'; if (isHistory) { renderHistory(); ui.historyView.classList.add('open'); } else { ui.historyView.classList.remove('open'); } if (isHistory) ui.historyView.scrollTop = 0; } function openSettings(view) { if (!canRun()) return; mount(); settingsOpen = true; stopPicking(); closePanel(); refreshSwitch(); if (ui) { ui.fab.style.display = 'none'; ui.set.classList.add('open'); showSettingsView(view === 'history' ? 'history' : 'main'); try { ui.set.focus(); } catch (e) {} } } function closeSettings() { settingsOpen = false; if (ui) { ui.set.classList.remove('open'); ui.fab.style.display = enabled ? '' : 'none'; } if (!enabled) destroy(); } function buildGroup(title, items) { var wrap = makeEl('div', 'cii-grp'); wrap.appendChild(makeEl('h4', null, title)); var ul = document.createElement('ul'); var rows = (items && items.length) ? items : ['无']; rows.forEach(function (text) { ul.appendChild(makeEl('li', null, text)); }); wrap.appendChild(ul); return wrap; } function renderHistory() { if (!ui || ui.historyList.childElementCount) return; HISTORY.forEach(function (entry, index) { var open = entry.latest === true || index === 0; var item = makeEl('section', 'cii-ver' + (open ? ' open' : '')); var head = makeEl('button', 'cii-ver-head'); head.type = 'button'; head.setAttribute('aria-expanded', open ? 'true' : 'false'); var title = makeEl('span', 'cii-ver-title'); title.appendChild(makeEl('span', 'cii-ver-no', 'v' + entry.version)); if (entry.latest) title.appendChild(makeEl('span', 'cii-tag', '最新')); title.appendChild(makeEl('span', 'cii-ver-date', entry.date)); head.appendChild(title); head.appendChild(makeEl('span', 'cii-ver-chev', '›')); var body = makeEl('div', 'cii-ver-body'); body.appendChild(buildGroup('新增', entry.added)); body.appendChild(buildGroup('修复', entry.fixed)); head.addEventListener('click', function () { var isOpen = item.classList.toggle('open'); head.setAttribute('aria-expanded', isOpen ? 'true' : 'false'); }); item.appendChild(head); item.appendChild(body); ui.historyList.appendChild(item); }); } /* ---------------- 开关 ---------------- */ function setEnabled(value, silent) { enabled = !!value; if (!silent) persistEnabled(enabled); if (enabled) { mount(); if (ui) ui.fab.style.display = settingsOpen ? 'none' : ''; startObserver(); } else { stopPicking(); closePanel(); if (ui) ui.fab.style.display = 'none'; if (!settingsOpen) destroy(); } refreshSwitch(); } /* ---------------- 捕获流程 ---------------- */ function onFabClick(event) { event.preventDefault(); event.stopPropagation(); if (picking) stopPicking(); else startPicking(); } function startPicking() { if (!ui) return; closePanel(); picking = true; ui.fab.textContent = '取消'; ui.fab.setAttribute('data-mode', 'picking'); ui.fab.setAttribute('aria-label', '退出捕获'); ui.hint.style.display = 'block'; document.addEventListener('pointermove', onPointerMove, true); document.addEventListener('click', onCaptureClick, true); document.addEventListener('keydown', onKeydown, true); document.addEventListener('scroll', onViewportChange, { capture: true, passive: true }); window.addEventListener('resize', onViewportChange, true); } function stopPicking() { picking = false; if (ui) { ui.fab.textContent = '捕获'; ui.fab.setAttribute('data-mode', 'idle'); ui.fab.setAttribute('aria-label', '捕获元素'); ui.hint.style.display = 'none'; } hideHighlight(); document.removeEventListener('pointermove', onPointerMove, true); document.removeEventListener('click', onCaptureClick, true); document.removeEventListener('keydown', onKeydown, true); document.removeEventListener('scroll', onViewportChange, true); window.removeEventListener('resize', onViewportChange, true); } function isOwnUI(event) { try { var path = typeof event.composedPath === 'function' ? event.composedPath() : []; return path.indexOf(host) !== -1; } catch (e) { return false; } } function onPointerMove(event) { if (!picking) return; if (event.pointerType === 'touch') return; if (isOwnUI(event)) { hideHighlight(); return; } highlight(event.target); } function onCaptureClick(event) { if (!picking) return; if (isOwnUI(event)) return; event.preventDefault(); event.stopPropagation(); if (event.stopImmediatePropagation) event.stopImmediatePropagation(); var el = event.target; if (!el || el.nodeType !== 1) { el = document.elementFromPoint(event.clientX, event.clientY); } if (!el || el.nodeType !== 1) return; stopPicking(); showPanel(el); } function onKeydown(event) { if (!picking) return; if (event.key === 'Escape') stopPicking(); } function onViewportChange() { if (rafPending) return; rafPending = true; requestAnimationFrame(function () { rafPending = false; if (!picking) return; if (lastHover && lastHover.isConnected) highlight(lastHover); else hideHighlight(); }); } function highlight(el) { if (!ui) return; if (!el || el.nodeType !== 1 || el === host) { hideHighlight(); return; } var rect = el.getBoundingClientRect(); if (!rect || (!rect.width && !rect.height)) { hideHighlight(); return; } var vw = window.innerWidth || 0; var vh = window.innerHeight || 0; if (rect.bottom < 0 || rect.right < 0 || rect.top > vh || rect.left > vw) { hideHighlight(); return; } lastHover = el; ui.hl.style.display = 'block'; ui.hl.style.left = rect.left + 'px'; ui.hl.style.top = rect.top + 'px'; ui.hl.style.width = rect.width + 'px'; ui.hl.style.height = rect.height + 'px'; ui.hlTag.textContent = describeTag(el) + ' ' + Math.round(rect.width) + ' × ' + Math.round(rect.height); ui.hlTag.style.top = rect.top > 30 ? '-24px' : '2px'; } function hideHighlight() { lastHover = null; if (ui) ui.hl.style.display = 'none'; } /* ---------------- 信息面板 ---------------- */ function showPanel(el) { if (!ui) return; var info = collect(el); currentInfo = info; renderRows(info); ui.panelBody.scrollTop = 0; ui.fab.style.display = 'none'; ui.mask.style.display = 'block'; ui.panel.classList.add('open'); } function closePanel() { if (!ui) return; ui.panel.classList.remove('open'); ui.mask.style.display = 'none'; ui.fab.style.display = (!settingsOpen && enabled) ? '' : 'none'; } function onAgainClick(event) { event.preventDefault(); closePanel(); startPicking(); } function onCopyClick(event) { event.preventDefault(); if (!currentInfo) return; copyText(buildCopyText(currentInfo)).then(function (ok) { toast(ok ? '已复制全部内容' : '复制失败,请长按文字手动复制'); }); } function onShadowKeydown(event) { if (event.key !== 'Escape') return; if (settingsOpen) closeSettings(); else closePanel(); } function renderRows(info) { var rows = [ ['元素', info.tag, false], ['目录', info.path, true], ['选择器', info.selector, true], ['位置', info.position, false], ['大小', info.size, false], ['颜色', info.color, false], ['文本', info.text, false] ]; var frag = document.createDocumentFragment(); rows.forEach(function (row) { var wrap = makeEl('div', 'cii-row'); wrap.appendChild(makeEl('div', 'cii-row-k', row[0])); wrap.appendChild(makeEl('div', 'cii-row-v' + (row[2] ? ' mono' : ''), row[1] || '不可用')); frag.appendChild(wrap); }); ui.panelBody.textContent = ''; ui.panelBody.appendChild(frag); } function toast(message) { if (!ui) return; ui.toast.textContent = message; ui.toast.classList.add('show'); clearTimeout(toastTimer); toastTimer = setTimeout(function () { if (ui) ui.toast.classList.remove('show'); }, 1800); } /* ---------------- 信息采集 ---------------- */ function collect(el) { return { tag: describeTag(el), path: buildPath(el), selector: buildSelector(el), position: describePosition(el), size: describeSize(el), color: describeColor(el), text: describeText(el) }; } function describeTag(el) { var tag = el.tagName ? el.tagName.toLowerCase() : '未知'; if (el.id) tag += '#' + el.id; if (el.classList && el.classList.length) { var names = Array.prototype.slice.call(el.classList, 0, 3); if (names.length) tag += '.' + names.join('.'); } return tag; } function buildPath(el) { var parts = []; var node = el; var guard = 0; var reachedRoot = false; while (node && node.nodeType === 1 && guard < 40) { guard += 1; var part = node.tagName.toLowerCase(); if (node.id) { part += '#' + node.id; } else if (node.classList && node.classList.length) { part += '.' + Array.prototype.slice.call(node.classList, 0, 2).join('.'); } parts.unshift(part); if (node === document.documentElement) { reachedRoot = true; break; } node = node.parentElement; } if (!parts.length) return '不可用'; if (!reachedRoot) parts.unshift('…'); return parts.join(' > '); } function buildSelector(el) { if (!el || el.nodeType !== 1) return '不可用'; var parts = []; var node = el; var guard = 0; while (node && node.nodeType === 1 && guard < 40) { guard += 1; if (node === document.documentElement) { parts.unshift('html'); break; } if (node.id) { parts.unshift('#' + cssEscape(node.id)); break; } var part = node.tagName.toLowerCase(); var parent = node.parentElement; if (parent) { var same = []; for (var i = 0; i < parent.children.length; i += 1) { if (parent.children[i].tagName === node.tagName) same.push(parent.children[i]); } if (same.length > 1) part += ':nth-of-type(' + (same.indexOf(node) + 1) + ')'; } parts.unshift(part); node = node.parentElement; } return parts.length ? parts.join(' > ') : '不可用'; } function cssEscape(value) { try { if (window.CSS && typeof window.CSS.escape === 'function') { return window.CSS.escape(String(value)); } } catch (e) {} return String(value).replace(/([^\w-])/g, '\\$1'); } function describePosition(el) { var rect = el.getBoundingClientRect(); if (!rect) return '不可用'; var left = rect.left + (window.scrollX || window.pageXOffset || 0); var top = rect.top + (window.scrollY || window.pageYOffset || 0); var text = '页面坐标 左 ' + roundPx(left) + 'px,上 ' + roundPx(top) + 'px' + ';视口内 左 ' + roundPx(rect.left) + 'px,上 ' + roundPx(rect.top) + 'px'; var vw = window.innerWidth || 0; var vh = window.innerHeight || 0; var visible = rect.bottom > 0 && rect.right > 0 && rect.top < vh && rect.left < vw; if (!visible) text += '(当前不在视口内)'; return text; } function describeSize(el) { var rect = el.getBoundingClientRect(); if (!rect) return '不可用'; var text = roundPx(rect.width) + ' × ' + roundPx(rect.height) + ' px'; var contentW = el.scrollWidth || 0; var contentH = el.scrollHeight || 0; if (contentW > rect.width + 1 || contentH > rect.height + 1) { text += '(内容 ' + Math.round(contentW) + ' × ' + Math.round(contentH) + ' px)'; } return text; } function describeColor(el) { var computed = window.getComputedStyle ? window.getComputedStyle(el) : null; if (!computed) return '不可用'; var parts = []; parts.push('文字 ' + (parseColor(computed.color) || '未知')); parts.push('背景 ' + (parseColor(computed.backgroundColor) || '未知')); var borderWidth = num(computed.borderTopWidth) + num(computed.borderRightWidth) + num(computed.borderBottomWidth) + num(computed.borderLeftWidth); if (borderWidth > 0 && computed.borderTopStyle !== 'none') { var borderColor = parseColor(computed.borderTopColor); if (borderColor && borderColor !== '透明') parts.push('边框 ' + borderColor); } return parts.join(';'); } function describeText(el) { var text = ''; try { var tag = el.tagName; if (tag === 'INPUT' || tag === 'TEXTAREA') { text = el.value || ''; } else if (tag === 'SELECT') { var option = el.selectedOptions && el.selectedOptions[0]; text = option ? option.textContent : (el.value || ''); } else if (tag === 'IMG') { text = el.getAttribute('alt') || ''; } else { text = el.innerText != null ? el.innerText : (el.textContent || ''); } } catch (e) { text = el.textContent || ''; } text = String(text).replace(/\s+/g, ' ').trim(); if (!text) return '无文本'; var total = text.length; if (total > MAX_TEXT) { text = text.slice(0, MAX_TEXT) + '…(已截断,共 ' + total + ' 字符)'; } return text; } function parseColor(value) { if (!value) return ''; var match = String(value).match(/rgba?\(([^)]+)\)/i); if (!match) return String(value); var nums = match[1].split(/[,\s\/]+/).filter(Boolean); var r = parseFloat(nums[0]); var g = parseFloat(nums[1]); var b = parseFloat(nums[2]); var a = nums.length > 3 ? parseFloat(nums[3]) : 1; if (!isFinite(r) || !isFinite(g) || !isFinite(b)) return String(value); if (a === 0) return '透明'; var hex = '#' + [r, g, b].map(function (v) { var s = Math.max(0, Math.min(255, Math.round(v))).toString(16); return s.length < 2 ? '0' + s : s; }).join(''); if (a < 1) return hex + '(不透明度 ' + Math.round(a * 100) + '%)'; return hex; } function num(value) { var n = parseFloat(value); return isFinite(n) ? n : 0; } function roundPx(value) { return Math.round(value); } /* ---------------- 复制 ---------------- */ function buildCopyText(info) { var lines = [ '内容信息检查 v' + VERSION, '页面:' + (document.title || '无标题'), '地址:' + location.href, '时间:' + formatTime(new Date()), '', '元素:' + info.tag, '目录:' + info.path, '选择器:' + info.selector, '位置:' + info.position, '大小:' + info.size, '颜色:' + info.color, '文本:' + info.text ]; return lines.join('\n'); } function copyText(text) { return new Promise(function (resolve) { var nav = null; try { nav = navigator; } catch (e) { nav = null; } if (nav && nav.clipboard && typeof nav.clipboard.writeText === 'function') { nav.clipboard.writeText(text).then(function () { resolve(true); }, function () { resolve(fallbackCopy(text)); }); return; } resolve(fallbackCopy(text)); }); } function fallbackCopy(text) { try { var ta = document.createElement('textarea'); ta.value = text; ta.setAttribute('readonly', ''); ta.style.cssText = 'position:fixed;top:0;left:0;width:1px;height:1px;padding:0;border:0;opacity:0;'; document.body.appendChild(ta); ta.focus(); ta.select(); ta.setSelectionRange(0, ta.value.length); var ok = document.execCommand('copy'); document.body.removeChild(ta); return !!ok; } catch (e) { return false; } } function pad2(value) { return value < 10 ? '0' + value : '' + value; } function formatTime(date) { return date.getFullYear() + '-' + pad2(date.getMonth() + 1) + '-' + pad2(date.getDate()) + ' ' + pad2(date.getHours()) + ':' + pad2(date.getMinutes()) + ':' + pad2(date.getSeconds()); } /* ---------------- 入口 ---------------- */ function registerMenus() { if (typeof GM_registerMenuCommand !== 'function') return; try { GM_registerMenuCommand('设置', function () { openSettings('main'); }); GM_registerMenuCommand('版本履历', function () { openSettings('history'); }); } catch (e) {} } registerMenus(); setEnabled(readEnabled(), true); watchEnabled(function (value) { if (value === enabled) return; setEnabled(value, true); }); })();