userScripts.update()

更新扩展程序注册的用户脚本。

语法

js
let updatingUserScript = browser.userScripts.update(
  scripts       // array of objects
);

参数

scripts

要更新的用户脚本的详细信息,类型为 array,元素为 userScripts.RegisteredUserScript

值为 null 或被省略的属性将不会被更改。将空数组传递给 matchesexcludeMatchesglobsexcludeGlobs 会清除这些属性。

返回值

如果所有请求的用户脚本都已更新,则此 Promise 将在不带参数的情况下兑现。如果任何用户脚本更新失败或请求因其他原因失败,则所有脚本都不会被更新,并且该 Promise 将以错误消息被拒绝。

示例

此代码片段展示了对用户脚本进行两次更新的示例。第一次更新失败,因为它试图创建一个无效的脚本注册。第二个示例展示了一次成功的更新。

js
// Valid registration:
await browser.userScripts.register([
  {
    worldId: "myScriptId",
    js: [{ code: "console.log('Hello world!');" }],
    matches: ["*://example.com/*"],
  },
]);

// Invalid! Would result in script without matches or includeGlobs!
await browser.userScripts.update([{ matches: [] }]);

// Valid: replaces matches with includeGlobs.
await browser.userScripts.update([
  {
    matches: [],
    includeGlobs: ["*example*"],
  },
]);

扩展程序示例

浏览器兼容性