-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
109 lines (90 loc) · 3 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
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, twitchOAuth) {
let newConfigData = configData;
if (userName != null)
newConfigData["twitchUserName"] = userName;
if (twitchOAuth != null)
newConfigData["makeTwitchAuthWorkToken"] = 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() {
window.open("https://make.twitchauth.work/login?template=3c71c079-2a9d-41aa-b5a4-726670b59efa", '_blank').focus();
}
function GetDataToSet() {
var twitchUserName = "";
var oauthToken = "";
oauthToken = window.prompt("Enter the User Auth Token", "");
if (oauthToken == null) {
alert("Provided User Auth Token is not valid, please generate a new one");
return;
}
if (!HasConfigDataKey("twitchUserName")) {
twitchUserName = window.prompt("Enter your twitch user name", "");
} else {
twitchUserName = configData["twitchUserName"];
}
CreateConfigDownload(twitchUserName, oauthToken);
}
function CreateSetupButtons() {
let linkContainer = document.getElementById("setupContainer");
if (!IsInOBS()) {
// Generate New OAuth
let GenNewOAuth = document.createElement("a");
GenNewOAuth.innerText = "Get User Login 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();