-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
134 lines (112 loc) · 3.86 KB
/
utils.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
function GetBooleanValueFromSettings(setting) {
if (typeof(setting) === "string") {
var LowerString = setting.toLowerCase();
return LowerString === "yes" || LowerString == "on";
}
else
return setting;
}
function IsInOBS() {
return (typeof(window.obsstudio) !== 'undefined');
}
function IsHostedLocally() {
return location.protocol === "file:";
}
// Taken from StackOverflow: https://stackoverflow.com/a/175787
function isNumeric(str) {
var curType = typeof(str);
if (curType == "number")
return true;
else if (curType != "string")
return false; // we only process strings!
return !isNaN(str) && !isNaN(parseFloat(str));
}
function LoadExampleConfigIfNeeded() {
if (typeof(configData) === "undefined") {
console.log("Example config is being loaded now");
var script = document.createElement("script");
script.src = "config_example.js";
document.head.appendChild(script);
}
}
function HasConfigDataKey(key_name) {
return (typeof(configData) !== "undefined" &&
typeof(configData[key_name]) !== "undefined" &&
configData[key_name].length > 0);
}
function ConvertToDataURI(target_json) {
const OutputText = "var configData = " + JSON.stringify(target_json, null, 3) + ";";
return "data:text/javascript;base64,"+btoa(OutputText);
}
function CreateConfigDownload(userName, clientID, twitchOAuth) {
let newConfigData = configData;
if (userName != null)
newConfigData["twitchUserName"] = userName;
if (clientID != null)
newConfigData["twitchClientId"] = clientID;
if (twitchOAuth != null)
newConfigData["twitchOAuthToken"] = twitchOAuth;
// Generate a new download with the new data
let a = document.createElement("a");
a.setAttribute("href", ConvertToDataURI(newConfigData));
a.setAttribute("download", "config.js");
a.click();
}
function QueryForTwitchOAuthTokens() {
const GenerateTwitchOAuth = (clientID) => {
const scopes = encodeURIComponent("channel:read:ads channel:edit:commercial channel_commercial channel_read");
const url = 'https://id.twitch.tv/oauth2/authorize?response_type=token&client_id='+ clientID +'&redirect_uri=https://twitchapps.com/tokengen/&scope=' + scopes;
window.open(url, '_blank').focus();
};
if (HasConfigDataKey("twitchClientId")) {
GenerateTwitchOAuth(configData["twitchClientId"]);
} else {
const getClientID = window.prompt("Please enter your twitch client id", "");
if (getClientID == null || getClientID.length <= 0) {
console.error("Invalid client id data provided");
} else {
GenerateTwitchOAuth(getClientID);
}
}
}
function GetDataToSet() {
var clientID = "";
var twitchUserName = "";
var oauthToken = "";
oauthToken = window.prompt("Enter the OAuth token you have generated", "");
if (oauthToken == null) {
alert("Provided OAuth Token is not valid, please generate a new one");
return;
}
if (!HasConfigDataKey("twitchClientId")) {
clientID = window.prompt("Enter your twitch client id", "");
} else {
clientID = configData["twitchClientId"];
}
if (!HasConfigDataKey("twitchUserName")) {
twitchUserName = window.prompt("Enter your twitch user name", "");
} else {
twitchUserName = configData["twitchUserName"];
}
CreateConfigDownload(twitchUserName, clientID, oauthToken);
}
function CreateSetupButtons() {
let linkContainer = document.getElementById("setupContainer");
if (!IsInOBS()) {
// Generate New OAuth
let GenNewOAuth = document.createElement("a");
GenNewOAuth.innerText = "Generate new OAuth Token";
GenNewOAuth.onclick = QueryForTwitchOAuthTokens;
linkContainer.appendChild(GenNewOAuth);
// Set data
let SetNewData = document.createElement("a");
SetNewData.innerText = "Save Data";
SetNewData.onclick = GetDataToSet;
linkContainer.appendChild(SetNewData);
} else {
linkContainer.class = ".hidden";
}
}
// This will load in the example config file if the main config file cannot be found
LoadExampleConfigIfNeeded();
CreateSetupButtons();