-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopup.js
157 lines (120 loc) · 4.62 KB
/
popup.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// inspiration: https://github.com/MartinMouritzen/segment-chromeextension/blob/master/popup.js
let settings = {};
$(document).ready( function () {
let busyBtn = $("#busyBtn");
let notBusyBtn = $("#notBusyBtn");
let hid = false;
let busy = false;
let port = chrome.extension.connect({
name: "WebRTC Presence popup"
});
port.onMessage.addListener((msg) => {
console.log(msg);
if (msg.type === "update") {
if(msg.data.status) {
if (msg.data.status === "on") {
busy = true;
console.log("turn busy button on");
busyBtn.removeClass("hidden");
notBusyBtn.addClass("hidden");
} else if (msg.data.status === "off") {
//busyBtn.addClass("hidden");
busy = false;
busyBtn.addClass("hidden");
notBusyBtn.removeClass("hidden");
}
}
if(msg.data.presences){
let table = "<table><th>url</th><th>Streams</th>";
msg.data.presences.forEach((item) => {
table += "<tr>" +
"<td>" + item.url + "</td>" +
"<td>" + item.streamCount + "</td>" +
"</tr>"
});
table += "</table>";
document.getElementById("data").innerHTML = table;
}
}
});
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
port.postMessage({type: "request", id: tabs[0].id});
});
$("button.busy").on("click", (e)=>{
busyBtn.toggleClass("hidden");
notBusyBtn.toggleClass("hidden");
if(e.target.innerText==="Busy"){
console.log("on button clicked; turning off");
port.postMessage({type: "command", command:"off"});
busy = false;
}
else if(e.target.innerText==="Not busy"){
console.log("off button clicked; turning on");
port.postMessage({type: "command", command:"on"});
busy = true;
}
else{
console.log("on/off button handler error", e);
}
sendSettings();
});
// Function for sending the form data back to background.js
function sendSettings(){
$.each($(".settingsForm").serializeArray() , function(i, field){
settings[field.name] = field.value;
});
settings.onMethod = $("#onGetSwitch")[0].checked ? "GET" : "POST";
settings.offMethod = $("#offGetSwitch")[0].checked ? "GET" : "POST";
settings.hid = hid;
settings.busy = busy;
console.log(settings);
port.postMessage({type: "settings", data: settings});
}
let onForm = $("#onSettingsForm")[0];
let offForm = $("#offSettingsForm")[0];
// Get settings data from a localStorage and populate the forms
let settingsData = JSON.parse(localStorage.getItem("settings"));
console.log("saved settings:", settingsData);
// Update the UI if we have saved settings
if(settingsData){
hid = settingsData.hid;
busy = settingsData.busy;
$("#embrava").prop("checked", settingsData.hid);
if(settingsData.busy){
busyBtn.removeClass("hidden");
notBusyBtn.addClass("hidden");
}
else{
busyBtn.addClass("hidden");
notBusyBtn.removeClass("hidden");
}
onForm["onUrl"].value = settingsData["onUrl"] || "";
onForm["onHeaders"].value = settingsData["onHeaders"] || "";
onForm["onPostBody"].value = settingsData["onPostBody"] || "";
onForm["onGetSwitch"].checked = settingsData["onMethod"] === "GET";
if(onForm["onGetSwitch"].checked)
$(".onPostBody").hide();
offForm["offUrl"].value = settingsData["offUrl"] || "";
offForm["offHeaders"].value = settingsData["offHeaders"] || "";
offForm["offGetSwitch"].checked = settingsData["offMethod"] === "GET";
offForm["offPostBody"].value = settingsData["offPostBody"] || "";
if(offForm["offGetSwitch"].checked)
$(".offPostBody").hide();
}
$("#settingsToggle").click(() => {
$("#settings").toggle()
});
$("#onGetSwitch").change(()=>{
$(".onPostBody").toggle();
});
$("#offGetSwitch").change(()=>{
$(".offPostBody").toggle();
});
$("#embrava").change(()=>{
hid = !hid;
});
$("#saveButton").click(()=>{
sendSettings();
localStorage.setItem("settings", JSON.stringify(settings));
});
});