/
popup.js
115 lines (90 loc) · 3.72 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
const TAUNT_DELAY_LABEL = "tauntDelayLabel";
const MAX_TAUNTS_LABEL = "maxTauntsLabel";
var WARNING_MESSAGE_DISPLAYED = false;
// Once the DOM is ready...
window.addEventListener('DOMContentLoaded', () => {
// non aoe stream option
var nonAoeOption = document.getElementById(NON_AOE_OPTION);
updateUiElement(NON_AOE_OPTION,nonAoeOption,updateCheckbox);
// onClick's logic below:
nonAoeOption.addEventListener('click', function() {
sendContent(NON_AOE_OPTION, nonAoeOption.checked);
});
// taunt delay option
var tauntDelay = document.getElementById(TAUNT_DELAY);
updateUiElement(TAUNT_DELAY,tauntDelay,updateSlider);
tauntDelay.addEventListener('change', function() {
sendContent(TAUNT_DELAY, tauntDelay.value);
});
// taunt delay text box
// the update UI element function might not of been the best idea now that I think about it...
var tauntDelayLabel = document.getElementById(TAUNT_DELAY_LABEL);
updateUiElement(TAUNT_DELAY, tauntDelayLabel, updateLabel);
// this updates the textbox
tauntDelay.addEventListener('input', function() {
updateLabel(tauntDelayLabel,tauntDelay.value);
});
// max taunts option
var maxTaunts = document.getElementById(MAX_TAUNTS);
updateUiElement(MAX_TAUNTS,maxTaunts,updateSlider);
maxTaunts.addEventListener('change', function() {
sendContent(MAX_TAUNTS, maxTaunts.value);
});
var maxTauntsLabel = document.getElementById(MAX_TAUNTS_LABEL);
updateUiElement(MAX_TAUNTS, maxTauntsLabel, updateLabel);
// this updates the textbox
maxTaunts.addEventListener('input', function() {
updateLabel(maxTauntsLabel,maxTaunts.value);
});
var resetButton = document.getElementById(RESET_BUTTON);
resetButton.addEventListener('click', function(){
sendContent(RESET_BUTTON, "toggle");
// then just refresh
window.location.reload();
});
});
/*
This function passed a message to our message handler in content.js so we can actually
do things with the options being changed from the default_popup window
*/
function sendContent(messageContent, messageValue){
// loads in when we are loaded
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, { message: messageContent, value: messageValue});
});
}
/*
This function takes in the message to send to the content script in order to return some response
whcih then get's sent to a function specified as the second paramater in the function.
*/
async function updateUiElement(messageContent, uiElement, func){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, { message: messageContent}, function(response){
if (window.chrome.runtime.lastError) {
// do you work, that's it. No more unchecked error
if(!WARNING_MESSAGE_DISPLAYED)
alert("Please refresh twitch.tv to access the options menu! Your options will not be saved! " +
"If this error persists after refreshing the window then please post a github issue ticket. " + "Failed for " + messageContent);
// we should also hide the popup menu
WARNING_MESSAGE_DISPLAYED = true;
return;
}
func(uiElement,response);
});
});
}
/*
This function updates a checkbox checked property
*/
function updateCheckbox(checkbox, value){
// javascript :)
checkbox.checked = value;
}
// updates a slider
function updateSlider(slider, value){
// javascript :)
slider.value = value;
}
function updateLabel(label, value){
label.value = value;
}