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.

相关推荐
李少兄1 小时前
HTML 表单控件
前端·microsoft·html
学习笔记1012 小时前
第十五章认识Ajax(六)
前端·javascript·ajax
消失的旧时光-19432 小时前
Flutter 异步编程:Future 与 Stream 深度解析
android·前端·flutter
曹牧3 小时前
C# 中的 DateTime.Now.ToString() 方法支持多种预定义的格式字符
前端·c#
勿在浮沙筑高台3 小时前
海龟交易系统R
前端·人工智能·r语言
歪歪1003 小时前
C#如何在数据可视化工具中进行数据筛选?
开发语言·前端·信息可视化·前端框架·c#·visual studio
Captaincc4 小时前
AI 能帮你写代码,但把代码变成软件,还是得靠人
前端·后端·程序员
吃饺子不吃馅5 小时前
如何设计一个 Canvas 事件系统?
前端·canvas·图形学
theOtherSky5 小时前
element+vue3 table上下左右键切换input和select
javascript·vue.js·elementui·1024程序员节
Baklib梅梅5 小时前
无头内容管理系统:打造灵活高效的多渠道内容架构
前端·ruby on rails·前端框架·ruby