开发一个 Typecho 插件,专门用于解析蓝奏云分享链接并自动填充密码,甚至可以生成直链嵌入文章

🧩 插件开发方案

1. 插件基本结构

根据 Typecho 官方文档,插件需要遵循以下基本结构:
2026-02-17T13:52:44.png

<?php
/**
 * 蓝奏云解析器
 *
 * @package LanzouParser
 * @author [你的名字]
 * @version 1.0.0
 * @link [你的网站]
 */

class LanzouParser_Plugin implements Typecho_Plugin_Interface
{
    /* 激活插件方法 */
    public static function activate(){
        // 挂载文章内容解析接口
        Typecho_Plugin::factory('Widget_Abstract_Contents')->content = array('LanzouParser_Plugin', 'parseContent');
        Typecho_Plugin::factory('Widget_Abstract_Contents')->excerpt = array('LanzouParser_Plugin', 'parseContent');
        return _t('插件已激活,请配置相关参数');
    }
    
    /* 禁用插件方法 */
    public static function deactivate(){
        return _t('插件已禁用');
    }
    
    /* 插件配置面板 */
    public static function config(Typecho_Widget_Helper_Form $form){
        // 添加配置选项
        $cache_time = new Typecho_Widget_Helper_Form_Element_Text(
            'cache_time', 
            NULL, 
            '3600', 
            _t('缓存时间(秒)'),
            _t('解析结果缓存时间,默认3600秒')
        );
        $form->addInput($cache_time);
        
        $auto_fill = new Typecho_Widget_Helper_Form_Element_Radio(
            'auto_fill',
            array('1' => '开启', '0' => '关闭'),
            '1',
            _t('自动填充密码'),
            _t('开启后自动为蓝奏云链接填充密码')
        );
        $form->addInput($auto_fill);
    }
    
    /* 个人用户的配置方法 */
    public static function personalConfig(Typecho_Widget_Helper_Form $form){}
    
    /* 核心解析方法 */
    public static function parseContent($content, $widget, $lastResult)
    {
        // 处理文章内容中的蓝奏云链接
        $options = Helper::options()->plugin('LanzouParser');
        return self::parseLanzouLinks($content, $options);
    }
    
    /**
     * 解析蓝奏云链接
     */
    private static function parseLanzouLinks($content, $options)
    {
        // 匹配蓝奏云链接和密码的正则
        $pattern = '/(https?:\/\/[a-zA-Z0-9]+\.lanzou[b|c|t|v|s]?\.com\/[a-zA-Z0-9]+)(?:\s+密码[::]\s*([a-zA-Z0-9]+))?/i';
        
        return preg_replace_callback($pattern, function($matches) use ($options) {
            $url = $matches[1];
            $password = isset($matches[2]) ? $matches[2] : '';
            
            // 如果开启自动填充且有密码,生成增强链接
            if ($options->auto_fill && !empty($password)) {
                return self::generateEnhancedLink($url, $password);
            }
            
            // 否则返回原始链接
            return sprintf('<a href="%s" target="_blank" rel="nofollow">%s</a>', $url, $url);
        }, $content);
    }
    
    /**
     * 生成增强链接(自动填充密码)
     */
    private static function generateEnhancedLink($url, $password)
    {
        // 方案1: 生成包含密码的HTML按钮
        $html = '<div class="lanzou-block">';
        $html .= '<a href="' . $url . '" class="lanzou-link" target="_blank" data-pwd="' . $password . '">';
        $html .= '📁 蓝奏云文件 (密码:' . $password . ')</a>';
        $html .= '<button class="copy-pwd-btn" data-pwd="' . $password . '">复制密码</button>';
        $html .= '<script>/* 自动填充脚本 */</script>';
        $html .= '</div>';
        
        return $html;
        
        // 方案2: 如果需要更高级的功能,可以调用第三方解析API
        // return self::fetchDirectLink($url, $password);
    }
}

2. 功能实现要点

核心功能模块

  • 链接识别:使用正则表达式匹配蓝奏云各种域名格式(lanzoub.comlanzoux.com等)
  • 密码提取:支持常见格式如"密码:2ewq"、"提取码:2ewq"等
  • 内容过滤:避免在评论等非文章区域生效

增强功能选项

根据搜索结果,已有类似插件实现了七牛云、又拍云等云存储集成,你可以参考实现:

// 配置面板增强
$enable_cache = new Typecho_Widget_Helper_Form_Element_Radio(
    'enable_cache',
    array('1' => '开启', '0' => '关闭'),
    '1',
    _t('开启解析缓存'),
    _t'开启后缓存解析结果,减少请求')
);
$form->addInput($enable_cache);

$parse_mode = new Typecho_Widget_Helper_Form_Element_Select(
    'parse_mode',
    array('local' => '本地处理', 'api' => 'API解析'),
    'local',
    _t('解析模式'),
    _t('本地处理仅填充密码,API解析可获取直链')
);
$form->addInput($parse_mode);

3. 前端交互增强

为了让用户体验更好,可以添加JavaScript自动填充功能:

// 插件自动生成的JavaScript
(function() {
    // 自动复制密码
    document.querySelectorAll('.copy-pwd-btn').forEach(btn => {
        btn.addEventListener('click', function() {
            const pwd = this.dataset.pwd;
            navigator.clipboard.writeText(pwd);
            alert('密码已复制:' + pwd);
        });
    });
    
    // 可选:在页面打开时自动填写(需要跨域支持)
    if (window.location.href.includes('lanzou')) {
        // 检测当前页面是否为蓝奏云下载页
        // 自动填充密码并提交
    }
})();

📦 插件安装与使用

  1. 创建插件目录:在 Typecho 的 /usr/plugins/ 下创建 LanzouParser 文件夹
  2. 放置插件文件:将上述代码保存为 Plugin.php
  3. 激活插件:进入 Typecho 后台 → 插件管理 → 激活"蓝奏云解析器"
  4. 配置插件:根据需要设置缓存时间、解析模式等
  5. 使用效果:在文章中直接写入:

    https://wwbve.lanzoub.com/ik9fy3ib1seb 密码:2ewq

    插件会自动识别并生成带密码填充功能的按钮

💡 进阶实现思路

如果你想要更强大的功能,可以参考搜索结果中的几种实现:

  1. 直链解析:调用现有的蓝奏云解析API(GitHub上有多个开源项目),获取真实下载链接
  2. 文件预览:对于图片、PDF等文件,直接嵌入预览
  3. 下载统计:记录链接点击次数
  4. 短链接生成:将长链接转为短链接,方便分享

这样的插件既满足你的需求,又能为 Typecho 社区做贡献。

已有 88 条评论

    1. 清风 清风

      用过WordPress的类似插件,功能很全。Typecho这边一直空缺,楼主要是做出来记得发到官方插件库。配置界面最好加上“是否在新窗口打开链接”的选项,有些人喜欢当前页跳转,有些人喜欢新标签页。

    2. Emma Watson Emma Watson

      This could be a game-changer for Chinese content creators abroad. Lanzous is so popular for sharing files within China, but the experience outside China can be hit or miss. A direct link generator would bypass a lot of those access issues.

    3. 小胖 小胖

      蓝奏云现在有直链解析的API吗?我记得很多都是靠模拟请求获取真实地址,容易被封。如果做直链功能,最好加上代理选项,或者让用户自己选择是否启用,免得网站IP被拉黑。

    4. Michael Michael

      I appreciate the attention to security. The note about "avoiding comment sections" is important - you don't want random users injecting scripts through comments. Make sure the parsing only applies to the main content area and maybe custom fields.

    5. 老王 老王

      Typecho玩的人少,愿意写插件的更少。楼主这个方案写得挺详细,连正则都给了,照着抄都能用。建议再加上一个功能:识别文章中所有的蓝奏云链接,统一在文章底部生成一个下载列表,方便批量下载。

    6. Sarah Johnson Sarah Johnson

      The HTML button approach is clean, but what about mobile users? The copy-password function might not work as smoothly on all browsers. Consider adding a fallback that just selects the password text for older devices. Progressive enhancement is key.

    7. 阿杰 阿杰

      我去年就想写这么个插件,一直懒得动手。没想到今天就看到方案了。建议加上直链解析功能,GitHub上有很多现成的解析API可以调用,直接获取真实下载地址,读者点一下就能下载,不用跳转页面,体验更好。

    8. TechGuy_42 TechGuy_42

      Great concept! The caching mechanism is essential here. If you're calling external APIs for direct links, you definitely don't want to hit them on every page load. 3600 seconds default seems reasonable, but maybe make it configurable per-link for frequently updated content?

    9. 小林 小林

      正则表达式那块要注意一下,蓝奏云的域名后缀太多了,lanzous、lanzoub、lanzoux、lanzoui、lanzouw……建议搞个可配置的域名列表,或者用模糊匹配,不然哪天他们换了域名插件就废了。

    10. Alex Chen Alex Chen

      I've been looking for something like this! I run a small software blog and share a lot of Lanzous links. The auto-fill password feature would save my readers so much time. Any plans to support the newer lanzouv.com domains? Their domain changes so frequently.

    11. 老张 老张

      这个插件太实用了!每次在文章里贴蓝奏云链接,都要单独写一句“密码是xxx”,读者还得手动复制粘贴,太麻烦了。如果能自动生成带密码填充的按钮,体验直接提升一个档次。建议加上短链接功能,蓝奏的原始链接真的太长了。