返回资讯中心
外部精选
软件工程
#开发者实时流

Pebble 插件系统更新:设置页可打包进 pbw 并实时通信

来源标题:Pebble Plugins

Pebble 现在允许将设置页面直接打包进 pbw,并通过 configPage 字段配置,取代旧的 showConfiguration 流程。设置页在打开期间可实时与 plugin/PKJS 代码互发消息。设置页与应用网络权限相同,加载的资源必须来自已在 Internet 中声明且用户允许的域名。

Hacker News 最新droidjj2026/11/6 · 5 天前12 分钟阅读

以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。

尽量返回所有属性和形状;有了数据之后,text 形状不花什么代价,由 app 来挑选。只有在请求里带 iconPixelSize 时才生成 icon/image。 Pebble app 会在 app 看到每次更新之前先做检查:manifest 里没有声明的形状,或者格式不符合(上述)要求的形状,都会被丢弃。原因会出现在 pebble logs 里。 到目前为止,PKJS 设置页必须托管在某个地方(或者用 Clay 生成),而且只能通过 URL 以及在关闭时传递数据。现在设置页可以打包进 pbw,并且可以在打开期间实时向你的 plugin/PKJS 代码发消息。 把 package.json 里的 configPage 设成项目中的一个 HTML 文件(会打包进 pbw)或者一个 URL。如果设置了,就会用它代替旧的 showConfiguration 流程。 设置页拥有和你的 app 相同的网络访问权限:它加载或连接的一切(脚本、样式、字体、图片、fetch、WebSockets)都必须位于你在 Internet 中声明且用户允许的域名上。把页面需要的东西打包进去,而不是从 CDN 加载。 // in the page Pebble.addEventListener("ready", (e) => { /* e.target ('plugin' or 'pkjs') is up */ }); const reply = await Pebble.sendMessage("plugin", { type: "listTickers" }); // in plugin.js Pebble.registerConfigHandler((message, respond) => respond({ tickers: tickers() })); // in pebble-js-app.js Pebble.addEventListener("configmessage", (e) => e.respond({ theme: theme() })); 发送前要等待 ready:页面打开时 plugin 才会启动。 因为页面只能访问你声明的域名,指向其他任何地方(你的源代码、帮助页)的链接都无法在页面中打开。改用 Pebble.openUrl:用户会看到地址,并可以在浏览器中打开。它接受一个 http(s) url,并 resolve 为用户是否打开了它。 const opened = await Pebble.openUrl("https://github.com/you/your-plugin"); 文件访问 plugin 可以读写用户允许的文件/目录。在 usesPermissions 中声明 ReadFiles(和/或 WriteFiles 以修改文件;允许它也就允许读取)。 plugin 必须拥有读/写权限,才能以相应模式使用文件选择器(从设置页调用),选择器随后会以该模式授予对该特定文件或目录的额外访问权限。 得到的文件或文件夹可以保存下来,并在设置页之外复用,比如在 plugin 使用期间根据用户请求读写文件,或用于向 app 提供数据。 // in the settings page const folder = await Pebble.showDirectoryPicker({ mode: "readwrite" }); // or const [file] = await Pebble.showOpenFilePicker({ types: [{ accept: { "text/markdown": [".md"] } }] }); await Pebble.sendMessage("plugin", { type: "setFolder", id: folder.id }); const held = await Pebble.fileSystem.grants(); // [{ id, kind, name, mode }] await Pebble.fileSystem.revoke(held[0].id); // in plugin.js Pebble.registerConfigHandler((message, respond) => { if (message.type === "setFolder") localStorage.setItem("folder", message.id); respond({ ok: true }); }); Pebble.registerActionHandler(async (request, respond) => { const dir = await Pebble.fileSystem.getHandle(localStorage.getItem("folder")); const file = await dir.getFileHandle("inbox.md", { create: true }); const existing = await (await file.getFile()).text(); const writable = await file.createWritable(); await writable.write(existing + "\n- " + request.args.text); await writable.close(); for await (const [name, entry] of dir.entries()) console.log(name, entry.kind); respond.ok({ text: "Added." }); }); 取消选择器会以 AbortError reject;用户未允许 ReadFiles(对于读写选择则是 WriteFiles)的 plugin 会得到 NotAllowedError,通过只读选择尝试写入,或者使用用户已经撤回的条目,也会得到同样的错误。 plugin 可以通过 Pebble.fileSystem.revoke(id) 撤销不再需要的访问权限(比如用户正在重新配置 plugin 时)。 plugin-test dashboard 展示了由此可以实现什么:它的设置页向 app 的 PKJS 请求所有已安装内容的目录(由 enumeratePlugins 构建)来填充下拉框。每次更改都直接发送给 PKJS,因此用户编辑时手表会实时更新,没有保存按钮。PKJS 会把实时读数推回去,因此每个选项都带有它当前的读数标签(例如 "temperature: 21°C"),每个 tile 显示其当前值。 用 pebble build 构建,使用 SDK 4.44 或更高版本以及 pebble-tool 5.1.0 或更高版本。构建会把 plugin 脚本和本地 configPage 打包进 pbw,并在项目配置时检查 plugin 块。 仅 plugin 项目(没有 src/c)在 package.json 中设置 "projectType": "plugin",不需要 SDK;对于 watchapp 项目,脚本会自己运行 pebble build。脚本会写出 <project-dir>/build/<name>.pbw,并标记为使用权限(manifestVersion 2)。 该仓库中的 plugin 都是可运行的示例。

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →