← 全部示例

JavaScript 开发与测试 vscode.dev

VS Code Web:活动标签页显示行数徽章

在活动标签页上附加一个显示当前文件行数的小徽章(github.dev 也适用)。

可复制的代码

/* Append the line count of the active editor as a badge on the tab (works on github.dev too) */
(function () {
  const update = () => {
    const tab = document.querySelector('.monaco-workbench .tabs-container [role="tab"].active .tab-label');
    const lineCount = document.querySelectorAll('.monaco-editor .view-line').length;
    if (!tab || !lineCount) return;
    let badge = tab.querySelector('.vsc-lines-badge');
    if (!badge) {
      badge = document.createElement('span');
      badge.className = 'vsc-lines-badge';
      Object.assign(badge.style, {
        marginLeft: '6px', padding: '0 5px', borderRadius: '8px',
        background: 'rgba(255,138,61,0.25)', color: '#ff8a3d',
        fontSize: '10px', fontWeight: '700'
      });
      tab.appendChild(badge);
    }
    badge.textContent = lineCount + 'L';
  };
  update();
  const obs = new MutationObserver(update);
  obs.observe(document.body, { childList: true, subtree: true });
})();

如何使用此示例

  1. 用上方按钮复制代码。
  2. 安装 JustZix(2 分钟),在目标页面打开扩展。
  3. 添加一条匹配该页面的新规则。
  4. 将代码粘贴到规则的 JavaScript 面板并保存 — 每次访问页面时都会运行。

为此示例评分

暂无评分 — 成为第一个。

此示例有效吗?

没有可粘贴的地方,代码片段毫无用处。

JustZix 安装只需 2 分钟,并在每个匹配的页面上运行你的代码。无需账户、无需付费。

免费下载 查看应用场景