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.

相关推荐
讯方洋哥7 小时前
应用冷启动优化
前端·harmonyos
speedoooo7 小时前
未来的App不再需要菜单栏?
前端·ui·容器·小程序·web app
猿究院_xyz8 小时前
微信小程序与echarts联动安卓真机测试出现黑色阴影
前端·javascript·微信小程序·小程序·echarts
JANG10248 小时前
【Linux】进程
linux·网络·chrome
IT_陈寒8 小时前
Redis性能翻倍的5个冷门技巧,90%开发者都不知道的深度优化方案
前端·人工智能·后端
清水迎朝阳8 小时前
监听 edge大声朗读 样式变化
前端·edge
油丶酸萝卜别吃8 小时前
修改chrome配置,关闭跨域校验
前端·chrome
刺客xs8 小时前
Qt-----QSS样式表
开发语言·javascript·qt
m0_740043739 小时前
3、Vuex-Axios-Element UI
前端·javascript·vue.js
阿蒙Amon9 小时前
JavaScript学习笔记:14.类型数组
javascript·笔记·学习