主页 > 手机  > 

asyncawait

asyncawait

Edge Extension Store 上架需要 V3,chrome.tabs.executeScript 停用,使用 chrome.scripting.executeScript 代替,需要  tabId !要这干啥?

JavaScript实现谷歌浏览器插件开发的方法详解_javascript技巧_脚本之家 (jb51.net)

获取当前 tabId 就没个 API 么? 

chrome mands.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 )

(async() => { ...... })();

改成这样可以了 

chrome mands.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。

标签:

asyncawait由讯客互联手机栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“asyncawait