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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值