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.

相关推荐
Spider_Man40 分钟前
打造属于你的前端沙盒 🎉
前端·typescript·github
用户479492835691542 分钟前
🤫 你不知道的 JavaScript:`"👦🏻".length` 竟然不是 1?
前端·javascript·面试
掘金一周43 分钟前
凌晨零点,一个TODO,差点把我们整个部门抬走 | 掘金一周 9.11
前端·人工智能·后端
用户8174413427481 小时前
kubernetes核心概念 Service
前端
xingkongv1 小时前
从“调接口仔”到“业务合伙人”:前端的 DDD 初体验
javascript·前端框架
东北南西1 小时前
Web Worker 从原理到实战 —— 把耗时工作搬到后台线程,避免页面卡顿
前端·javascript
Zz_waiting.1 小时前
案例开发 - 日程管理 - 第六期
前端·javascript·vue.js·路由·router
A 风1 小时前
封装日期选择器组件,带有上周,下周按钮
开发语言·javascript·vue.js
袁煦丞1 小时前
企业微信开发者的‘跨网穿梭门’:cpolar内网穿透实验室第499个成功挑战
前端·程序员·远程工作
Simon_He1 小时前
vue-markdown-renderer:比 vercel streamdown 更低 CPU、更多节点支持、真正的流式渲染体验
前端·vue.js·markdown