1- import { getCurrentTabId } from "./helpers/utils.js" ;
2-
31export default {
42 icon : "" ,
53 name : {
@@ -11,40 +9,89 @@ export default {
119 vi : "" ,
1210 } ,
1311
14- // whiteList: [" https://www.google.com/*"],
12+ // https://github.dev/GoogleChrome/chrome-extensions-samples/api-samples/tabCapture
1513
1614 onClickContentScript : async ( ) => {
15+ try {
16+ const tab = await UfsGlobal . Extension . runInBackground (
17+ "utils.getCurrentTab"
18+ ) ;
19+
20+ const streamId = await UfsGlobal . Extension . runInBackground (
21+ "chrome.desktopCapture.chooseDesktopMedia" ,
22+ [ [ "tab" , "audio" ] , tab , "callback" ]
23+ ) ;
24+
25+ navigator . webkitGetUserMedia (
26+ {
27+ audio : {
28+ mandatory : {
29+ chromeMediaSource : "tab" ,
30+ chromeMediaSourceId : streamId ,
31+ } ,
32+ } ,
33+ } ,
34+ function ( stream ) {
35+ const context = new AudioContext ( ) ;
36+ const source = context . createMediaStreamSource ( stream ) ;
37+ const analyser = context . createAnalyser ( ) ;
38+ source . connect ( analyser ) ;
39+ source . connect ( context . destination ) ;
40+ analyser . connect ( context . destination ) ;
41+ } ,
42+ function ( error ) {
43+ alert ( "no" ) ;
44+ console . log ( error ) ;
45+ }
46+ ) ;
47+ } catch ( e ) {
48+ console . log ( e ) ;
49+ }
50+ } ,
51+
52+ _onClickContentScript : async ( ) => {
1753 // https://developer.chrome.com/docs/extensions/reference/api/tabCapture#preserving-system-audio
1854 // https://github.com/Douile/Chrome-Audio-Visualizer/tree/master
55+ // https://stackoverflow.com/questions/66217882/properly-using-chrome-tabcapture-in-a-manifest-v3-extension
56+ // https://groups.google.com/a/chromium.org/g/chromium-extensions/c/ffI0iNd79oo
1957
2058 try {
21- const currentTab = await UfsGlobal . Extension . runInBackground (
22- "utils.getCurrentTabId "
59+ const tab = await UfsGlobal . Extension . runInBackground (
60+ "utils.getCurrentTab "
2361 ) ;
2462
2563 const streamId = await UfsGlobal . Extension . runInBackground (
2664 "chrome.tabCapture.getMediaStreamId" ,
2765 [
2866 {
29- consumerTabId : currentTab ,
30- targetTabId : currentTab ,
67+ targetTabId : tab . id ,
68+ consumerTabId : tab . id ,
3169 } ,
3270 ]
3371 ) ;
3472
35- const stream = await navigator . mediaDevices . getUserMedia ( {
36- audio : {
37- mandatory : {
38- chromeMediaSource : "tab" ,
39- chromeMediaSourceId : streamId ,
73+ navigator . webkitGetUserMedia (
74+ {
75+ audio : {
76+ mandatory : {
77+ chromeMediaSource : "tab" ,
78+ chromeMediaSourceId : streamId ,
79+ } ,
4080 } ,
4181 } ,
42- } ) ;
43-
44- const output = new AudioContext ( ) ;
45- const source = output . createMediaStreamSource ( stream ) ;
46- source . connect ( output . destination ) ;
47- console . log ( output ) ;
82+ function ( stream ) {
83+ const context = new AudioContext ( ) ;
84+ const source = context . createMediaStreamSource ( stream ) ;
85+ const analyser = context . createAnalyser ( ) ;
86+ source . connect ( analyser ) ;
87+ source . connect ( context . destination ) ;
88+ analyser . connect ( context . destination ) ;
89+ } ,
90+ function ( error ) {
91+ alert ( "no" ) ;
92+ console . log ( error ) ;
93+ }
94+ ) ;
4895 } catch ( e ) {
4996 alert ( e ) ;
5097 }
0 commit comments