定制 linkrobins/clipboard 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

linkrobins/clipboard

Composer 安装命令:

composer require linkrobins/clipboard

包简介

Adds a copy-to-clipboard button to code blocks, plus an optional language label. Ported to Flarum 2.0 from ffans/clipboardjs.

README 文档

README

Adds a copy-to-clipboard button to every code block in posts, plus an optional language label in the top-right. Seven visual themes to choose from.

Ported to Flarum 2.0 from ffans/clipboardjs (MIT). Credit to Golden for the original.

What it does

Two features:

  1. Copy button on code blocks — when a visitor hovers over a <pre> in a post, a small "copy" button appears. Click it; the code is on their clipboard. The button briefly flips to a checkmark to confirm. Seven button styles to match the look of GitHub, CSDN, Jianshu, etc.
  2. Language label on code blocks — when a syntax highlighter (Prism, highlight.js, etc.) decorates <code> with a language-X class, the extension hoists that class onto the parent <pre> and the CSS shows the language name in the top-right of the block.

Both features have independent toggles in the admin settings; you can use one without the other.

Requirements

  • Flarum 2.0 or later
  • PHP 8.2 or later
  • Optional: a syntax highlighter extension (for the language label feature to have something to display)

Installation

composer require linkrobins/clipboard
php flarum cache:clear

In Flarum admin → Extensions, find Link Robins Clipboard and enable it. Open its settings to pick a theme and toggle features.

Settings

  • Enable copy button on code blocks — turn the copy button on or off
  • Copy button style — default, GitHub, LingCoder, CSDN, cnBlogs, Jian Shu, SegmentFault
  • Show language label on code blocks — turn the language label on or off

How it works under the hood

The extension extends CommentPost.oncreate and, after each post renders, walks the post DOM for <pre> elements. For each one not already decorated, it inserts a themed button. Click handlers use the modern native navigator.clipboard.writeText() API to copy code text, with a hidden-textarea + document.execCommand('copy') fallback for older browsers or non-secure contexts.

The language detection sets up a MutationObserver once per page that watches <code> elements for a language-X class. When one appears (after a syntax highlighter runs), the class is mirrored onto the parent <pre> so the CSS selectors in _languages.less can display the language name.

What changed from the 1.x original

  • Targets flarum/core ^2.0
  • Renamed namespace: linkrobins-clipboard.*
  • Dropped the clipboard npm dependency — modern browsers have a native clipboard API, and the original library is now obsolete
  • Dropped the SaveSettings listener — it used Flarum\Api\Event\Serializing which was removed in Flarum 2.0, and its work is redundant with the serializeToForum extender calls already in extend.php
  • No webpack/JSX build — vanilla JS, hand-written, no node_modules
  • Uses app.registry.for('linkrobins-clipboard') (the 2.0 name) instead of app.extensionData.for(...)
  • Setting type renamed switchboolean (both work; boolean is the modern name)
  • CSS class renamed to LinkRobinsClipboard (the .clipboard button class stays the same so themes from upstream still apply)

License

MIT. Credit to Golden / ffans for the original implementation.

linkrobins/clipboard 适用场景与选型建议

linkrobins/clipboard 是一款 基于 TypeScript 开发的 Composer 扩展包,目前已累计 76 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 linkrobins/clipboard 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 linkrobins/clipboard 我们能提供哪些服务?
定制开发 / 二次开发

基于 linkrobins/clipboard 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 76
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 50
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: TypeScript

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-11