-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
135 lines (104 loc) · 3.43 KB
/
main.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
/**
* Triggered when the emote url input is modified.
* @param {Event} event The associated event.
*/
function emoteUrlHandler(event) {
var url = event.target.value.length > 0 ? event.target.value : 'https://static-cdn.jtvnw.net/emoticons/v1/12/1.0';
var customEmotes = document.getElementsByClassName('customEmote');
for(var i = 0; i < customEmotes.length; i++) {
customEmotes[i].src = url;
}
}
/**
* Triggered when the icon url input is modified.
* @param {event} event The associated event.
*/
function iconUrlHandler(event) {
var url = event.target.value.length > 0 ? event.target.value : 'https://static-cdn.jtvnw.net/jtv_user_pictures/subscriber-star.png';
var customIcons = document.getElementsByClassName('subscriber');
for(var i = 0; i < customIcons.length; i++) {
customIcons[i].style.backgroundImage = 'url(' + url + ')';
}
}
/**
* Shares a ready-to-use url contaning the icon & emote URLs.
*
* The generated url is copied to the clipboard if possible.
*/
function shareHandler() {
var url = window.location.origin + window.location.pathname;
var iconUrl = document.getElementById('iconUrlInput').value;
var emoteUrl = document.getElementById('emoteUrlInput').value;
if (iconUrl.length === 0 && emoteUrl.length === 0) {
log('Nothing to share. Change the icon or emote URLs.');
return;
}
var queryString = '';
if (iconUrl.length > 0) {
queryString += 'icon=' + encodeURIComponent(iconUrl);
}
if (emoteUrl.length > 0) {
if (queryString.length > 0) {
queryString += '&';
}
queryString += 'emote=' + encodeURIComponent(emoteUrl);
}
copyTextToClipboard(url + '?' + queryString);
}
/**
* Copies text to the clipboard.
* @param {string} text The text to copy to the clipboard.
*/
function copyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
textArea.style.width = '2em';
textArea.style.height = '2em';
textArea.style.padding = 0;
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
textArea.style.background = 'transparent';
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
var errorMessage = 'Sorry, unable to copy to your clipboard.';
try {
var successful = document.execCommand('copy');
log(successful ? 'Successfully copied the URL to you clipboard.' : errorMessage);
} catch (err) {
log(errorMessage);
}
document.body.removeChild(textArea);
}
/**
* Logs a message.
* @param {string} message The message to log.
*/
function log(message) {
var logElement = document.getElementById('log');
logElement.innerHTML = message;
setTimeout(function () {
logElement.innerHTML = '';
}, 3000);
}
/**
* Here we go!
*/
document.addEventListener("DOMContentLoaded", function() {
var iconUrlInput = document.getElementById('iconUrlInput');
iconUrlInput.oninput = iconUrlHandler;
var emoteUrlInput = document.getElementById('emoteUrlInput');
emoteUrlInput.oninput = emoteUrlHandler;
var parameters = new URLSearchParams(window.location.search);
if (parameters.has('icon')) {
iconUrlInput.value = parameters.get('icon');
iconUrlInput.dispatchEvent(new Event('input'));
}
if (parameters.has('emote')) {
emoteUrlInput.value = parameters.get('emote');
emoteUrlInput.dispatchEvent(new Event('input'));
}
});