This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
113 lines (112 loc) · 3.75 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
function time() {
return new Date().getTime()
}
var background = {
init: function() {
chrome.runtime.onMessage.addListener(background.onMessage);
audioCapture.captures.onAdd.addListener(background.onCaptureAdd)
chrome.browserAction.onClicked.addListener(background.onBrowserAction);
chrome.tabs.onActiveChanged.addListener(background.onPageUpdate);
chrome.tabs.onUpdated.addListener(background.onPageUpdate);
},
onCaptureAdd: function(object) {
background.audioObj = object;
chrome.tabs.sendMessage(background.current,{message:"you",id:object.object.capture.id})
background.activeId = background.current;
chrome.browserAction.setIcon({path:"/img/iconon128.png"});
},
onBrowserAction: function(a) {
console.log("browserAction",a);
var id;
chrome.tabs.sendMessage(a.id,{message:"you",id:undefined});
for (i=0;i<audioCapture.captures.content.length;i++) {
id = audioCapture.captures.content[i].capture.id;
try {
tracks = audioCapture.captures.content[i].close();
} catch(e) {
console.warn("Unable to close track",e);
}
console.log("Closed",id);
}
if (background.activeId == a.id) {
background.activeId = null;
chrome.browserAction.setIcon({path:"/img/iconoff128.png"})
} else {
if (e = audioCapture.startCapture() != undefined) {
console.error(e);
}
}
},
onPageUpdate: function(a) {
background.current = a
chrome.browserAction.setIcon({path:"/img/iconoff128.png"});
chrome.tabs.sendMessage(a,{message:"who"},function(id) {
console.log("who",id);
b = audioCapture.captures.getById(id);
if (b != undefined) {
background.audioObj.object = b;
chrome.browserAction.setIcon({path:"/img/iconon128.png"});
background.activeId = background.current;
} else {
background.audioObj = {};
background.activeId = null;
}
});
},
onMessage: function(message, sender, response) {
if (message.message == "streamupdate") {
if (background.audioObj == undefined) {
chrome.tabs.update(message.data.id,{muted:false},function(tab) {
audioCapture.startCapture();
})
response({sucess:false,reason:"Setting up"})
return 0;
}
background.audioObj.object.update();
response({sucess:true,obj:background.audioObj});
} else if (message.message == "update") {
if (background.audioObj.object != undefined) {
background.audioObj.object.update();
response({success:true,freqData:background.audioObj.object.data.frequencyData,timeData:background.audioObj.object.data.timeData,bufferSize:background.audioObj.object.data.bufferSize})
}
response({sucess:false})
}
response({success:false})
},
capture: function() {
audioCapture.startCapture();
},
activeId: null
}
background.init();
// context menus
const CONTEXTS = {
purchases: {
properties: {
type: "normal",
id: "purchases",
title: "In-App purchases",
contexts: ["browser_action","page_action"]
},
callback: function() {
chrome.tabs.create({url:chrome.runtime.getURL("purchases.html")});
}
},
faq: {
properties: {
type: "normal",
id: "faq",
title: "FAQ",
contexts: ["browser_action","page_action"]
},
callback: function() {
chrome.tabs.create({url:chrome.runtime.getURL("faq.html")});
}
}
}
function contextMenu(info, tab) {
CONTEXTS[info.menuItemId].callback();
}
chrome.contextMenus.onClicked.addListener(contextMenu);
chrome.contextMenus.create(CONTEXTS.purchases.properties);
chrome.contextMenus.create(CONTEXTS.faq.properties);