-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.js
266 lines (212 loc) · 6.97 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"use strict"
/*
* main.js
*
* New Age Bullshit Generator
* © 2014–2022 Seb Pearce (sebpearce.com)
* Licensed under the MIT License.
*
* TODO:
*
* Fix things like "This is the vision behind our 100% zero-point energy,
* zero-point energy karma bracelets."
*
* bs.generateSentence() should do 1 thing only (generate a sentence),
* not pull patterns out of use.
*/
function capitalizeFirstLetter(str) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
function randomInt(max) {
return Math.floor(Math.random() * (max + 1))
}
function replaceAWithAn(sentence) {
// replace 'a [vowel]' with 'an [vowel]'
// I added a \W before the [Aa] because one time I got
// 'Dogman is the antithesis of knowledge' :)
return sentence.replace(/(^|\W)([Aa]) ([aeiou])/g, "$1$2n $3")
}
function removeSpacesBeforePunctuation(sentence) {
// remove spaces before commas/periods/semicolons
return sentence.replace(/ ([,\.;\?])/g, "$1")
}
function deleteSpaceAfterHyphen(sentence) {
// take care of prefixes (delete the space after the hyphen)
return sentence.replace(/- /g, "-")
}
function addSpaceAfterQuestionMarks(sentence) {
// add space after question marks if they're mid-sentence
return sentence.replace(/\?(\w)/g, "? $1")
}
function insertSpaceBeforePunctuation(sentence) {
return sentence.replace(/([\.,;\?])/g, " $1")
}
function insertSpaceBetweenSentences(text) {
// insert a space between sentences (after periods and question marks)
return text.replace(/([\.\?])(\w)/g, "$1 $2")
}
// The generator in all its quantum glory
const bs = {
sentencePool: [],
initializeSentencePool: function () {
// [...foo] only does shallow copies
// but sentencePatterns is an array of arrays
this.sentencePool = [...this.sentencePatterns.map((group) => group.slice())]
},
removeSentenceFromPool: function (topic, el) {
if (el > -1) {
this.sentencePool[topic].splice(el, 1)
}
},
retrieveRandomWordOfType: function (type) {
const rand = randomInt(this.bullshitWords[type].length - 1)
return this.bullshitWords[type][rand]
},
cleanSentence: function (sentence) {
let result = replaceAWithAn(sentence)
result = result.trim()
result = capitalizeFirstLetter(result)
result = removeSpacesBeforePunctuation(result)
result = deleteSpaceAfterHyphen(result)
result = addSpaceAfterQuestionMarks(result)
return result
},
generateSentence: function (topic) {
const patternNumber = randomInt(this.sentencePool[topic].length - 1)
let pattern = this.sentencePool[topic][patternNumber]
if (typeof pattern === "undefined") {
throw new Error("ran out of pattern " + patternNumber)
}
// insert a space before . , ; ? so we can split the string into an array
pattern = insertSpaceBeforePunctuation(pattern)
pattern = pattern.split(" ")
// remove the pattern from the sentence pool so it can't be re-used
this.removeSentenceFromPool(topic, patternNumber)
// remove the topic from the sentence pool if there are no sentences left
// for that particular topic
if (this.sentencePool[topic].length === 0) {
this.sentencePool.splice(topic, 1)
}
let result = ""
for (let x in pattern) {
// if word matches one of the placeholder words (e.g. nPerson),
// replace it with a random instance of its type (e.g. warrior)
if (this.bullshitWords.hasOwnProperty(pattern[x])) {
result += this.retrieveRandomWordOfType(pattern[x])
} else {
result += pattern[x]
}
result += " "
}
result = this.cleanSentence(result)
return result
},
generateText: function ({ numberOfSentences, topicIndex }) {
let fullText = ""
for (let i = 0; i < numberOfSentences; i++) {
fullText += this.generateSentence(topicIndex)
// if the topic has been deleted, pick another topic
if (typeof this.sentencePool[topicIndex] === "undefined") {
topicIndex = randomInt(this.sentencePool.length - 1)
}
}
fullText = insertSpaceBetweenSentences(fullText)
return fullText
},
}
const timeouts = []
const reionizeBtn = document.querySelector(".reionize")
const pageFlash = document.querySelector(".page-flash")
const mainHeading = document.querySelector("#main-heading")
const subHeading = document.querySelector("#sub-heading")
const thirdHeading = document.querySelector("#third-heading")
const quote = document.querySelector(".quote")
const newAgeImage = document.querySelector(".new-age-image")
const paragraphs = document.querySelectorAll(".bs-paragraph")
const allText = document.querySelectorAll(".fade-text")
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1))
let t = array[i]
array[i] = array[j]
array[j] = t
}
}
const images = [
"fantasy.jpg",
"forest.jpg",
"planet.jpg",
"pyramids.jpg",
"rocks.jpg",
"sacred-geometry.jpg",
"sea.jpg",
"spiritualism.jpg",
"stones.jpg",
"sunset-tree.jpg",
"thunderstorm.jpg",
"water.jpg",
"woman.jpg",
]
let currentImages = [...images]
shuffle(currentImages)
function loadImage() {
const imageUrl = "/bullshit/images/" + currentImages[0]
newAgeImage.setAttribute("src", imageUrl)
currentImages.shift()
if (currentImages.length === 0) {
currentImages = [...images]
shuffle(currentImages)
}
}
function reionizeElectrons() {
pageFlash.classList.replace("hide", "show")
// clear existing timeouts if button is pushed while last round is animating
timeouts.forEach((n) => window.clearTimeout(n))
window.setTimeout(() => {
pageFlash.classList.replace("show", "hide")
}, 20)
allText.forEach((t) => t.classList.replace("show", "hide"))
bs.initializeSentencePool()
// generate random topic
let topicIndex = 0
mainHeading.textContent = bs.generateText({
numberOfSentences: 1,
topicIndex,
})
subHeading.textContent = bs.generateText({
numberOfSentences: 2,
topicIndex,
})
topicIndex = randomInt(bs.sentencePool.length - 2)
thirdHeading.textContent = bs.generateText({
numberOfSentences: 1,
topicIndex,
})
paragraphs.forEach((p) => {
topicIndex = randomInt(bs.sentencePool.length - 1)
p.textContent = bs.generateText({ numberOfSentences: 3, topicIndex })
})
topicIndex = randomInt(bs.sentencePool.length - 1)
quote.textContent = bs.generateText({ numberOfSentences: 1, topicIndex })
newAgeImage.classList.replace("show", "hide")
allText.forEach((t, i) => {
timeouts.push(
window.setTimeout(() => {
t.classList.replace("hide", "show")
}, i * 250 + 250)
)
})
loadImage()
}
reionizeBtn.addEventListener("click", reionizeElectrons)
// cancel the button jump if user hovers on it mid-jump
reionizeBtn.addEventListener("mouseenter", () =>
reionizeBtn.classList.remove("glowjump")
)
newAgeImage.addEventListener("load", () => {
// Chromium seems to need this delay for its show transition to work
window.setTimeout(() => {
newAgeImage.classList.replace("hide", "show")
}, 5)
})
loadImage()