Skip to content

脚本编写

Chrome 文档Firefox 文档

请参考上述浏览器文档,了解如何使用 API 的基础知识。

执行脚本返回值

在调用 browser.scripting.executeScript 时,可以执行内容脚本或未列出的脚本。要使脚本返回值,请在脚本的 main 函数中返回一个值。

typescript
// entrypoints/background.ts
const res = await browser.scripting.executeScript({
  target: { tabId },
  files: ['content-scripts/example.js'],
});
console.log(res); // "Hello John!"
typescript
// entrypoints/example.content.ts
export default defineContentScript({
  registration: 'runtime',
  main(ctx) {
    console.log('脚本被执行了!');
    return 'Hello John!';
  },
});