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.

相关推荐
@大迁世界20 分钟前
07.React 中的 createRoot 方法是什么?它具体如何运作?
前端·javascript·react.js·前端框架·ecmascript
January120728 分钟前
VBen Admin Select 选择框选中后仍然显示校验错误提示的解决方案
前端·vben
. . . . .36 分钟前
前端测试框架:Vitest
前端
xiaotao1311 小时前
什么是 Tailwind CSS
前端·css·css3
颜酱1 小时前
DFS 岛屿系列题全解析
javascript·后端·算法
战南诚2 小时前
VUE中,keep-alive组件与钩子函数的生命周期
前端·vue.js
发现一只大呆瓜2 小时前
React-彻底搞懂 Redux:从单向数据流到 useReducer 的终极抉择
前端·react.js·面试
霍理迪2 小时前
Vue的响应式和生命周期
前端·javascript·vue.js
李剑一2 小时前
别再瞎写了!Cesium 模型 360° 环绕,4 套源码全公开,项目直接用
前端