-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
110 lines (100 loc) · 4.06 KB
/
script.js
File metadata and controls
110 lines (100 loc) · 4.06 KB
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
// jquery listeners
$('#input').on('blur', function() {fillOutput()});
$('#weight').on('input', function() {fillOutput()});
$('#filename').on('input', function() {openFile()});
$('#save').on('click', function() {saveFile()});
$('#delete').on('click', function() {deleteFile()});
$('#toggleui').on('click', function() {toggleStealth()});
// delegated event since output is dynamic
$(document).on('click', '.redacted', function() {$(this).toggleClass('show')});
loadStorage();
openFile();
// refresh the output text
function fillOutput() {
$('#output').empty();
var weight = $('#weight').val();
var wordlist = parseText();
var line = '<li>';
for (var i = 0; i < wordlist.length; i++) {
var word = wordlist[i];
if (word === "\n") {
$('#output').append(line+'</li>');
line='<li>';
}
else {line+=redactWrap(word,weight);}
}
$('#output').append(line+'</li>');
}
// split input text into an array, separating the words, spaces, newlines, and punctuation
function parseText() {
words = $('#input').val().match(/\'?\w+([-']\w+)*\'?|\n|\w+|\s+|[^\s\w]+|\n|/g);
return words;
}
// wrap valid words in span tags according to random weight selected
function redactWrap(word,weight) {
if (!word.match(/[a-zA-Z]/)) {return word;}
else if (Math.floor(Math.random() * 100 + 1) > weight) {return word;}
else {return "<span class='redacted'>" + word + "</span>";}
}
// initialize localstorage variables on first run
function initStorage() {
localStorage.setItem("files", JSON.stringify(['u_beatit']));
localStorage.setItem("last_file", "u_beatit");
localStorage.setItem("u_beatit", "They told him don't you ever come around here\nDon't wanna see your face, you better disappear\nThe fire's in their eyes and their words are really clear\nSo beat it, just beat it\n\nYou better run, you better do what you can\nDon't wanna see no blood, don't be a macho man\nYou wanna be tough, better do what you can\nSo beat it, but you wanna be bad\n\nJust beat it, beat it, beat it, beat it\nNo one wants to be defeated\nShowin' how funky, strong it's your fight\nIt doesn't matter who's wrong or right\n\nJust beat it, beat it, just beat it, beat it\nJust beat it, beat it, just beat it, beat it");
}
// refresh the file selection drop-down
function loadStorage() {
if (localStorage.getItem("files") === null) {initStorage();}
var files = JSON.parse(localStorage.getItem("files"));
var selected = localStorage.getItem("last_file");
$('#filename').empty()
for (i=0; i<files.length; i++) {
$('#filename').append("<option value='" + files[i] + "'>" + files[i].slice(2) + "</option>")
}
$('#filename').val(selected);
return files;
}
// write input text to localstorage
function saveFile() {
var fileName = $('#filename').val();
var saveName = $('#newfile').val();
var saveContent = $('#input').val();
var files = loadStorage();
if (saveName==="" || saveName === null) {saveName=fileName.slice(2)}
if (files.indexOf("u_"+saveName)<0) {
files.push("u_" + saveName);
localStorage.setItem("files", JSON.stringify(files));
$('#filename').append("<option value='u_" + saveName + "'>" + saveName + "</option>");
}
$('#filename').val('u_'+saveName);
$('#newfile').val("");
localStorage.setItem("u_" + saveName, saveContent);
localStorage.setItem("last_file", 'u_'+saveName);
}
// remove selected file from local storage
function deleteFile() {
var fileName = $('#filename').val();
var files = loadStorage();
var index = files.indexOf(fileName);
if (index!== -1) {files.splice(index, 1);}
localStorage.setItem("files", JSON.stringify(files));
localStorage.removeItem(fileName);
loadStorage();
}
// load selected file
function openFile() {
var fileName = $('#filename').val();
var fileContent = localStorage.getItem(fileName);
localStorage.setItem("last_file", fileName);
if (fileContent != "") {
$('#input').val(fileContent);
fillOutput();
}
}
// toggle ui elements
function toggleStealth() {
$('#instructions').toggleClass('hide');
$('#input').toggleClass('hide');
$('#fileform').toggleClass('hide');
$('#weight').toggleClass('hide');
}