Edge Extension Store 上架需要 V3,chrome.tabs.executeScript 停用,使用 chrome.scripting.executeScript 代替,需要 tabId !要这干啥?
JavaScript实现谷歌浏览器插件开发的方法详解_javascript技巧_脚本之家 (jb51.net)
获取当前 tabId 就没个 API 么?
- chrome.commands.onCommand.addListener(function (command) {
- console.log(command);
- if (command === 'capture_video') {
- let tab = await getCurrentTab();
- console.log(tab[0].id);
- chrome.scripting.executeScript({
- target: { tabId: tab[0].id },
- files: ['video.js']
- });
- })();
- }
- });
-
- async function getCurrentTab() {
- let queryOptions = { active: true, lastFocusedWindow: true };
- let tab = chrome.tabs.query(queryOptions);
- return tab;
- }
改好后报错:
Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules
用async匿名函数时候会出现的问题 - 杨强的个人小站 (idarc.cn)
- (async() => {
- ......
- })();
改成这样可以了
- chrome.commands.onCommand.addListener(function (command) {
- console.log(command);
- if (command === 'capture_video') {
- (async() => {
- let tab = await getCurrentTab();
- console.log(tab[0].id);
- chrome.scripting.executeScript({
- target: { tabId: tab[0].id },
- files: ['video.js']
- });
- })();
- }
- });
-
- async function getCurrentTab() {
- let queryOptions = { active: true, lastFocusedWindow: true };
- let tab = chrome.tabs.query(queryOptions);
- return tab;
- }
但是 popup.js 仍然无法获取 tabId。