# 快速获取页面中mdx/mdd/css文件的下载链接的JavaScript脚本

**URL:** <https://forum.freemdict.com/t/topic/33153>\
**Category:** 软件经验交流展望\
**Tags:** javascript\
**Created:** [2024 年10 月 21 日 23:22 UTC](https://forum.freemdict.com/t/topic/33153 "2024-10-21T23:22:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

作者： ![hkreporter](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/h/41988e/32.png) [@hkreporter](https://forum.freemdict.com/u/hkreporter)\
发布日期： [2024 年10 月 21 日 23:22 UTC](https://forum.freemdict.com/t/topic/33153/1 "2024-10-21T23:22:18Z")

</div>

最近有几篇帖子是直接页面中逐个贴出mdx/mdd/css文件，而不是放在网盘。逐个点击容易遗漏，不如用脚本更为省力。

F12–控制台–粘贴脚本–运行，就可以获得链接

```JavaScript
function getDownloadLinks() {
    // 获取页面中的所有 <a> 标签
    const links = document.querySelectorAll('a');
    
    // 存储符合条件的下载链接
    const downloadLinks = [];
    
    // 遍历所有 <a> 标签
    links.forEach(link => {
        const href = link.href;
        // 检查 href 是否以 .mdx, .mdd 或 .css 结尾
        if (href.endsWith('.mdx') || href.endsWith('.mdd') || href.endsWith('.css')) {
            downloadLinks.push(href);
        }
    });
    
    return downloadLinks;
}

// 调用函数并逐行打印结果
const links = getDownloadLinks();

// 遍历数组，逐行打印每个链接
links.forEach(link => {
    console.log(link);
});

```

用Surfingkeys实现的话更为简单：

```JavaScript
api.mapkey("ymf", "#7Copy all specified file URLs to the clipboard", function() {

    var links = document.querySelectorAll('a');
    var fileLinks = [];
    
    // 定义支持的文件扩展名，在这里添加或修改文件后缀
    var fileExtensions = ['.pdf', '.css', '.js', '.mdx', '.mdd'];

    // 遍历所有链接
    links.forEach(link => {
        var href = link.href;
        // 检查 href 是否以任意支持的扩展名结尾
        if (href && fileExtensions.some(ext => href.toLowerCase().endsWith(ext))) {
            fileLinks.push(href);
        }
    });

    if (fileLinks.length > 0) {
        Clipboard.write(fileLinks.join("\n"));
        Front.showBanner(`${fileLinks.length} links copied to clipboard!`);
    } else {
        Front.showBanner("No matching links found.");
    }
});

```
