-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
122 lines (106 loc) · 4.29 KB
/
script.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
const testing = 'http://localhost:5000'
const production = 'https://www.ticketdecisionassistant.com'
globalThis.environment = production
window.addEventListener('DOMContentLoaded', async (e) => {
const Toast = Swal.mixin({
toast: true,
position: 'top-right',
iconColor: 'white',
customClass: {
popup: 'colored-toast'
},
showConfirmButton: false,
timer: 2300
})
const ErrorToast = Swal.mixin({
toast: true,
position: 'top-right',
iconColor: 'white',
customClass: {
popup: 'colored-toast'
},
showConfirmButton: false,
showCloseButton: true
})
const LargeErrorToast = Swal.mixin({
toast: true,
position: 'top-right',
iconColor: 'white',
customClass: {
popup: 'colored-toast',
title: 'swal-title-33'
},
showConfirmButton: false,
timer: 5000
})
document.querySelector('#confirm-name').addEventListener('click', async () => {
let desired_user_name = document.querySelector('#user_name').value
if (desired_user_name.length >= 1) {
chrome.storage.local.set({
'user_name': desired_user_name
})
await Toast.fire({ icon: 'success', title: 'Your name has been set!' })
return
}
await Toast.fire({ icon: 'error', title: 'Your name can\'t be blank!' })
})
document.querySelector('#confirm-discord').addEventListener('click', async () => {
let discord_channel_url = document.querySelector('#discord_channel_url').value
let isValidDiscordWebhook = await isDiscordLinkValid(discord_channel_url)
if (isValidDiscordWebhook == 'Success') {
chrome.storage.local.set({
'discord_channel_url': discord_channel_url
})
await Toast.fire({ icon: 'success', title: 'Your discord is set up and ready to go!' })
return
}
chrome.storage.local.set({
'discord_channel_url': ''
})
if (isValidDiscordWebhook.includes('https://discord.com/channels/1010934604561112345/1010934604561154321')) {
await LargeErrorToast.fire({ icon: 'error', title: isValidDiscordWebhook})
return
} else {
await ErrorToast.fire({ icon: 'error', title: isValidDiscordWebhook})
}
})
let discord_channel_url_raw = await chrome.storage.local.get('discord_channel_url')
let discord_channel_url = discord_channel_url_raw['discord_channel_url']
discord_channel_url ? document.querySelector('#discord_channel_url').value = discord_channel_url : document.querySelector('#discord_channel_url').value = ''
let user_name_raw = await chrome.storage.local.get('user_name')
let user_name = user_name_raw['user_name']
user_name ? document.querySelector('#user_name').value = user_name : document.querySelector('#user_name').value = ''
})
async function isDiscordLinkValid(channel_url) {
const channel_id = channel_url.split('/')[channel_url.split('/').length - 1]
if (channel_id.length != 19 || isNaN(channel_id)) {
return 'Invalid Channel Link. Your Discord URL should look like this: https://discord.com/channels/1010934604561112345/1010934604561154321'
}
else if (channel_url == 'https://discord.com/channels/1010934604561112345/1010934604561154321') {
return 'You can\'t use the example channel URL. The channel URL you provide is important. It\'s where your messages will be sent.'
}
try {
const get_channel = await fetch(`${globalThis.environment}/api/get_channel`, {
"method": "POST",
"body": JSON.stringify({
'channel_id': channel_id.toString()
}),
cache: "no-cache",
headers: new Headers({
"content-type": "application/json"
})
}).then((response) => response.json())
.then((data) => {
return data.response
})
.catch(err => {
console.error(err)
return 'Unkown error occured. Please contact an admin.'
})
return get_channel
}
catch (err) {
console.error(err)
return 'Unkown error occured. Please contact an admin.'
}
}