Skip to content

Scripting

Chrome 文档Firefox 文档

有关该 API 的基本用法,请参阅上方的浏览器文档。

执行脚本的返回值

使用 browser.scripting.executeScript 时,你可以执行 Content Script 或未注册的脚本。要返回值,只需在脚本的 main 函数中返回一个值即可。

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