chrome.tabs.executeScrip To chrome.scripting.executeScript

问:

chrome.tabs.executeScript(null, { code: `token = '${token}';` }, () => chrome.tabs.executeScript(null, {file: 'js/hook.js'})); //在需要时注入到页面中 转 chrome.scripting.executeScript

gpt:

In Manifest V3, you can replace `chrome.tabs.executeScript` with `chrome.scripting.executeScript`. Here's how you can modify your code:

```javascript

chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {

const activeTab = tabs[0];

if (activeTab) {

chrome.scripting.executeScript({

target: { tabId: activeTab.id },

function: (token) => {

// This code runs in the context of the page

window.token = token;

},

args: [token],

}, () => {

// This code runs after the script has been executed

chrome.scripting.executeScript({

target: { tabId: activeTab.id },

files: ['js/hook.js'],

});

});

}

});

```

In this example:

  • `chrome.scripting.executeScript` is used to execute a script in the context of the active tab.

  • The first `executeScript` call sets the `token` variable in the context of the page.

  • The second `executeScript` call injects the `js/hook.js` file into the page.

Remember to handle the logic in `js/hook.js` accordingly, and ensure that the necessary permissions are set in your manifest file.

相关推荐
小村儿25 分钟前
一起吃透 Claude Code,告别 AI 编程迷茫
前端·后端·ai编程
牛十二25 分钟前
openclaw安装mcporter搜索小红书
开发语言·javascript·ecmascript
小金鱼Y35 分钟前
🔥 前端人必看:浏览器安全核心知识点全解析(XSS/CSRF/DDoS)
前端·javascript·安全
时寒的笔记39 分钟前
js逆向05_ob混淆花指令,平坦流,某麦网(突破ob混淆寻找拦截器)
开发语言·前端·javascript
ZengLiangYi43 分钟前
从文章到脚本:把 Git Tag + Semver + CI/CD 收敛成一个 `release.mjs`
前端·github
im_AMBER1 小时前
Lexical依赖版本冲突与标题渲染
前端·react.js·前端框架
起风了___1 小时前
解决大数据渲染卡顿:Vue3 虚拟列表组件的完整实现方案
前端·程序员
前端fun1 小时前
React如何远程加载组件
前端·react.js
淑子啦1 小时前
React录制视频和人脸识别
javascript·react.js·音视频
代码煮茶1 小时前
Vue3 路由实战 | Vue Router 从 0 到 1 搭建权限管理系统
前端·javascript·vue.js