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 分钟前
TypeScript 接口入门:定义代码的契约与形态
前端·javascript·typescript
掘金安东尼23 分钟前
使用自定义高亮API增强用户‘/’体验
前端·javascript·github
参宿71 小时前
electron之win/mac通知免打扰
java·前端·electron
石小石Orz1 小时前
性能提升60%:前端性能优化终极指南
前端·性能优化
夏日不想说话1 小时前
API请求乱序?深入解析 JS 竞态问题
前端·javascript·面试
zhaoolee1 小时前
通过rss订阅小红书,程序员将小红书同步到自己的github主页
前端
掘金安东尼1 小时前
我们让 JSON.stringify 的速度提升了两倍以上
前端·javascript·面试
Cheney95012 小时前
TypeScript 中,! 是 非空断言操作符
前端·vue.js·typescript
sp422 小时前
老旧前端项目如何升级工程化的项目
前端
青山Coding2 小时前
Cesium应用(二):基于heatmap.js 的全球气象可视化实现方案
前端·gis·cesium