面向开发者
通过链接或JavaScript,从任何网站向Download Manager Plus发送下载。
通用链接
用文件地址链接到/add。如果用户安装了扩展程序,会请求其确认下载;如果没有,可以安装扩展程序或直接下载文件。
<a href="https://download.extension.lat/add?name=manual.pdf&url=https%3A%2F%2Fexample.com%2Ffiles%2Fmanual.pdf" rel="nofollow">
使用Download Manager Plus下载
</a>| 名称 | 说明 |
|---|---|
| url | 文件的http(s)地址。必填。 |
| name | 建议的文件名。可选;保存前会被清理。 |
“添加到Download Manager Plus”按钮
一个样式为按钮的链接。没有JavaScript也能工作;有辅助脚本时,会在桥接可用时使用桥接。
<a
class="dmplus-button"
href="https://download.extension.lat/add?name=manual.pdf&url=https%3A%2F%2Fexample.com%2Ffiles%2Fmanual.pdf"
data-dmplus-url="https://example.com/files/manual.pdf"
data-dmplus-name="manual.pdf"
rel="nofollow"
>
添加到Download Manager Plus
</a>
<style>
.dmplus-button {
display: inline-flex;
align-items: center;
gap: 0.5rem;
padding: 0.625rem 1.25rem;
border-radius: 999px;
background: #a3e635;
color: #1a2e05;
font: 600 0.875rem/1 system-ui, sans-serif;
text-decoration: none;
}
.dmplus-button:focus-visible {
outline: 2px solid #1a2e05;
outline-offset: 2px;
}
</style>辅助脚本
一个不依赖其他库的小脚本,它提供window.DownloadManagerPlus。TypeScript类型位于 dmplus.d.ts.
<script src="https://download.extension.lat/sdk/dmplus.js" defer></script>
<!-- 可选:带有data-dmplus-url的链接使用桥接。 -->
<script src="https://download.extension.lat/sdk/dmplus.js" data-dmplus-auto defer></script>在script标签上添加data-dmplus-auto,让带有data-dmplus-url的链接使用桥接。不要在展示用户所写链接的页面上这样做:在受信任的网站上,桥接会不再询问就下载。
// 如果桥接在你的网站上可用就使用桥接;否则打开通用链接。
const result = await window.DownloadManagerPlus.addDownload({
url: "https://example.com/files/manual.pdf",
name: "manual.pdf",
});
if ("requestId" in result) {
const state = await window.DownloadManagerPlus.bridge.status(result.requestId);
console.log(state.status); // "pending", "downloading", "complete"…
}JavaScript桥接
如果用户允许网站向其发送下载(欢迎指南会为每个网站提供此选项),或从扩展程序图标允许你的网站,你的页面就可以用window.postMessage请求下载并查看状态。除非用户把你的网站(https)标记为信任,否则用户仍会确认每次下载。
| 名称 | 说明 |
|---|---|
| dmplus:add | 请求一次下载:url,以及可选的fileName和folder。 |
| dmplus:status | 查看你自己的页面所请求的下载的状态。 |
| dmplus:result | 扩展程序的回复,带有你发送的同一个id。 |
| dmplus:ready | 扩展程序宣布桥接已在页面上激活。 |
| dmplus:ping | 询问桥接是否已激活;扩展程序以dmplus:ready回复。 |
// 没有辅助脚本:用postMessage实现同一协议。
window.addEventListener("message", (event) => {
if (event.source !== window) return;
if (event.data?.source === "dmplus-extension" && event.data.type === "dmplus:result") {
console.log(event.data.id, event.data.result);
}
});
window.postMessage(
{
id: crypto.randomUUID(),
request: { url: "https://example.com/files/manual.pdf", fileName: "manual.pdf", v: 1 },
source: "dmplus",
type: "dmplus:add",
},
location.origin
);参考
状态
| 名称 | 说明 |
|---|---|
| pending | 正在等待用户确认下载。 |
| rejected | 用户拒绝了,或关闭了窗口。 |
| downloading | 正在下载。 |
| complete | 下载已完成。 |
| failed | 下载无法完成。 |
错误代码
| 名称 | 说明 |
|---|---|
| invalid-request | 消息格式错误、地址不是http(s),或是本地网络地址。 |
| unsupported-version | 你的页面使用的协议版本比扩展程序更新。 |
| rate-limited | 你的网站每分钟请求超过10次。 |
| too-many-pending | 已有请求正在等待确认。 |
| not-found | 该ID不存在,或不是你的页面请求的。 |
| internal | 内部错误,请重试。 |
限制和隐私
只接受http(s)地址,不能带自定义请求头或Cookie。每个网站每分钟最多10次请求,并且只能收到它自己请求的下载的状态。公开网站不能请求用户本地网络中的文件。