-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.js
184 lines (168 loc) · 4.61 KB
/
options.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const VRCHAT_PUBLIC_APIKEY = "JlE5Jldo5Jibnk5O5hTx6XVqsJu4WJ26";
let options = {
worldID: "",
instanceID: "",
nonce: "",
instanceOwnerID: "",
instancePermission: "",
region: "",
};
chrome.storage.local.get(options, (item) => {
// load setting.
for (let optionsKey in options) {
let elm = document.getElementsByName(optionsKey);
if (
typeof item[optionsKey] !== "undefined" &&
typeof elm[0] !== "undefined"
) {
elm[0].value = item[optionsKey];
}
}
});
/**
* @returns {string} UUID V4
*/
const generateNonce = () => {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
let r = (Math.random() * 16) | 0,
v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
};
/**
* setting save to chrome storage.
* @param e Event
* @returns {boolean} disable bubbling
*/
const save = (e) => {
resetValidColor();
if (checkValidForJS()) {
e.preventDefault();
return false;
}
for (let optionsKey in options) {
document.getElementsByName(optionsKey);
let elm = document.getElementsByName(optionsKey);
if (optionsKey === "instancePermission" && elm.tagName === "select") {
elm = elm.getElementsByTagName("option")[elm.selectedIndex];
}
if (
typeof options[optionsKey] !== "undefined" &&
typeof elm[0] !== "undefined"
) {
options[optionsKey] = elm[0].value;
}
}
chrome.storage.local.set(options, () => {
console.log("stored");
console.log(options);
});
// disable popup reload
e.preventDefault();
};
/**
* launch vrchat fixed instance.
* @param e Event
* @returns {boolean}
*/
const launch = (e) => {
for (let optionsKey in options) {
let elm = document.getElementsByName(optionsKey);
if (optionsKey === "instancePermission" && elm.tagName === "select") {
elm = elm.getElementsByTagName("option")[elm.selectedIndex];
}
options[optionsKey] = elm[0].value;
}
options["instancePermission"] = options["instancePermission"].replace(
"%%OWNER_ID%%",
options["instanceOwnerID"]
);
let params = {
worldId: `${options["worldID"]}&instanceId=${options["instanceID"]}${options["instancePermission"]}${options["region"]}`,
};
if (options["nonce"] !== "") {
params.worldId += `~nonce(${options["nonce"]})`;
}
resetValidColor();
if (checkValidForJS()) {
e.preventDefault();
return false;
}
window.open(
"https://www.vrchat.com/home/launch?" +
Object.keys(params)
.map((k) => k + "=" + params[k])
.join("&")
);
};
/**
* HTML5 require check for javascript.
* @returns {boolean}
*/
const checkValidForJS = () => {
let eles = document.querySelectorAll("input[required]:invalid");
eles.forEach((ele) => {
ele.style.backgroundColor = "red";
});
return eles.length !== 0;
};
/**
* Clear validation status.
*/
const resetValidColor = () => {
document.querySelectorAll("input").forEach((ele) => {
ele.style.backgroundColor = null;
});
};
const gen = document.getElementById("nonce_gen");
gen.addEventListener("click", (e) => {
let ele = document.getElementsByName("nonce")[0];
if (ele.value !== "" && !window.confirm("nonceを新たに生成しますか?")) {
return false;
}
ele.value = generateNonce();
// disable popup reload
e.preventDefault();
});
const inviteMe = (e) => {
e.preventDefault();
for (let optionsKey in options) {
let elm = document.getElementsByName(optionsKey);
if (optionsKey === "instancePermission" && elm.tagName === "select") {
elm = elm.getElementsByTagName("option")[elm.selectedIndex];
}
options[optionsKey] = elm[0].value;
}
options["instancePermission"] = options["instancePermission"].replace(
"%%OWNER_ID%%",
options["instanceOwnerID"]
);
let params = `https://vrchat.com/api/1/instances/${options["worldID"]}:${options["instanceID"]}~${options["instancePermission"]}`;
if (options["nonce"] !== "") {
params += `~nonce(${options["nonce"]})`;
}
resetValidColor();
if (checkValidForJS()) {
return false;
}
params += `/invite?apiKey=${VRCHAT_PUBLIC_APIKEY}`;
console.log(params);
fetch(params, {
method: "POST",
body: "",
})
.then((e) => {
console.log(e);
})
.catch((e) => {
console.error(e);
});
return false;
};
const saveButton = document.getElementById("save");
saveButton.addEventListener("click", save);
saveButton.addEventListener("click", launch);
const launchButton = document.getElementById("launch");
launchButton.addEventListener("click", launch);
const inviteMeButton = document.getElementById("invite_me");
inviteMeButton.addEventListener("click", inviteMe);