开发一个 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. Yves Yves

      对于密码提取,建议兼容更多分隔符,比如“提取码:”、“密码是”等等,现在的匹配有点严格。

    2. Xavier Xavier

      I've translated the plugin interface into French. If you need the language files, let me know.

    3. Wendy Wendy

      The regex pattern might also match some URLs that are not Lanzou links. A more specific domain list would be safer.

    4. Vic Vic

      Great plugin! Could you add an option to disable the plugin on mobile devices to save bandwidth?

    5. Una Una

      I modified the JavaScript to use a toast notification instead of an alert popup. The UX is much better now.