-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
67 lines (54 loc) · 2 KB
/
app.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
var textArea = document.getElementById('og-tweet');
var tweetCount = 0;
var totalTweets = 0;
var tweetCountTemplate = '(1 of 1)';
var charToRemove = tweetCountTemplate.length;
function liveUpdate() {
var inputVal = textArea.value;
var rawTweetCount = inputVal.length / 280;
if (rawTweetCount >= 10) {
charToRemove = charToRemove + 1;
}
var characterCount = inputVal.length + charToRemove;
var tweetFraction = characterCount / 280;
var tweetCalc = Math.ceil(tweetFraction);
if (inputVal.length === 0) {
document.getElementById('tweet-count').classList = '';
totalTweets = 0;
}
else {
document.getElementById('tweet-count').textContent = `You're currently at ${tweetCalc} tweet${tweetCalc > 1 ? 's' : ''}`;
document.getElementById('tweet-count').classList = 'visible';
totalTweets = tweetCalc;
}
}
textArea.addEventListener('keyup', liveUpdate);
document.getElementById('submit').onclick = function() {
document.getElementById('output').innerHTML = '';
if (totalTweets === 0) {
var audio = new Audio('23.mp3');
audio.play();
}
for (var i = 0; i < totalTweets; i++) {
var outputItems = document.getElementsByTagName('p');
var currCharToRemove = charToRemove;
if (i + 1 < 10) {
currCharToRemove = charToRemove;
}
else {
currCharToRemove = charToRemove + 1;
}
var charNeeded = 280 - currCharToRemove;
var rawOutput = textArea.value.substr(i*charNeeded,charNeeded);
var finalOutput = `<li class="tweet"><span id="copy-confirm">copied!</span><p>${rawOutput} (${i + 1} of ${totalTweets})</p><button onclick="copyTweet(${i})">copy tweet</button></li>`;
document.getElementById('output').innerHTML += finalOutput;
}
};
copyTweet = function(index) {
var copyText = document.getElementsByTagName('p')[index].textContent;
navigator.clipboard.writeText(copyText);
document.getElementById('copy-confirm').classList = 'visible';
setTimeout(function(){
document.getElementById('copy-confirm').classList = '';
}, 2000);
}