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 = tabs0;

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.

相关推荐
IT_陈寒2 小时前
JavaScript的闭包把我坑惨了,说好的内存会自动回收呢?
前端·人工智能·后端
Jackson__3 小时前
分享一个横向滚动案例,带悬停暂停,通用性很强
前端
MariaH3 小时前
git rebase的使用
前端
_柳青杨3 小时前
深入理解 JavaScript 事件循环
前端·javascript
阡陌Jony3 小时前
关于前端性能优化的一些问题:
前端
用户600071819104 小时前
【翻译】简化 TSRX
前端
IT乐手5 小时前
佛德角逼平西班牙,国足还有啥借口?
前端
JustHappy5 小时前
我汇总了身边朋友的经历才发现,其实第一份实习是最难找的......
前端·后端·面试
星栈6 小时前
Dioxus 的响应式系统:`Signal`、`Memo`、`Effect` 和异步状态到底该怎么分工
前端·前端框架
yingyima6 小时前
Java 正则表达式:比你想象的更强大
前端