-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitteripsum.js
204 lines (183 loc) · 6.93 KB
/
twitteripsum.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
/*
* Twitter Ipsum generates lorem ipsum placeholder text
* based on any Twitter user's latest tweets.
*
* It requires jQuery. It also updates and mangles some
* pieces from Fredrik Bridell's Lorem Ipsum Generator
* (http://bridell.com/loremipsum/)
*
* It expects that somewhere in your markup, you'll provide
* a <div id="ipsum"> or <textarea id="ipsum"> to dump text into
*
* If you provide a container with id="tweet-it", it will
* also dump in a Twitter button with a random quote as text
*/
// SET US UP THE CONFIG
// the Twitter username for your ipsum
var twitterUser = "andymboyle"
// how far back in the timeline to go
var tweetCount = 75
// seedWords are the terms you want to be SURE are available in the ipsum
var seedWords = "mom|Southie|Boston|whining|movies|Gen Y|Springsteen|Get Up Kids|cat|not practicing python"
// a seedWeight greater than 1 gives seedWords a higher chance of appearing
var seedWeight = 8;
// groupers will stay attached to the term that follows them
var groupers = "a|an|the|of|and|or|of|in"
// choose the number of paragraphs, and the words in each, for your ipsum
var grafs = "65|30|50"
// if you want, append a credit to the Twitter button's text
var twitterButtonVia = "/via @boyleipsum"
$(document).ready(function(){
var loremLang = null;
var loremIpsumText = '';
var seedList = new Array(seedWeight).join(seedWords+'|');
var url='https://api.twitter.com/1/statuses/user_timeline.json?screen_name='+twitterUser+'&count='+tweetCount.toString()+'&callback=?';
$.getJSON(url, function(data) {
var words = '';
$.each(data, function(i, tweets) {
words += tweets.text.toLowerCase() + ' ';
})
words = words.replace(/[^a-zA-Z 0-9 '@]+/g,'');
words = words.replace(/(\bhttp)\w+/g,'');
words = words.replace(/ /g,' ');
bits = groupers.split('|');
for (var i=0;i<bits.length;i++) {
var newBit = ' '+bits[i]+'~';
words = words.replace(new RegExp('\\s'+bits[i]+'\\s','gi'),newBit);
}
words = words.replace(/ /g,'|');
words = words.replace(/\|i\|/g,'\|I\|');
words = words.replace(/~/g,' ');
words = uniquesOnly(words);
words += seedList
loremLang = words.split('|');
grafSet = grafs.split('|');
var isTextArea = $("#ipsum").is("textarea")
for (var i=0;i<grafSet.length;i++) {
if (isTextArea) {
loremIpsumText += "<p>"+loremIpsumParagraph(grafSet[i])+"</p>"
} else {
loremIpsumText += "<p><span style='font-size: 0;'><p></span>"+loremIpsumParagraph(grafSet[i])+"<span style='font-size: 0;'></p></span></p>"
}
}
if (isTextArea) {
$("#ipsum").val(loremIpsumText);
} else {
$("#ipsum").append(loremIpsumText);
}
var newTweet = '';
while (newTweet.length == 0 || newTweet.length > 100) {
var newTweet = loremIpsumParagraph(14);
}
var twitterVia = ''
if (twitterButtonVia != null && twitterButtonVia != undefined) {
twitterVia = ' '+twitterButtonVia;
}
var button = '<a href="http://twitter.com/share" class="twitter-share-button" data-url="'+window.location.href+'" data-text="'+trim(newTweet)+twitterVia+'" data-count="none"></a>'
var buttonscript = document.createElement('script');
buttonscript.type = 'text/javascript';
buttonscript.src = ('http://platform.twitter.com/widgets.js');
var target=document.getElementById("deferredjs");
target.parentNode.insertBefore(buttonscript,target);
$("span#tweet-it").html(button);
$('#ipsum').click(function() {
SelectText('ipsum');
CopyToClipboard();
});
});
// loreming and ipsuming - original versions at http://bridell.com/loremipsum/
var endings = "................................??!";
function chance(percentage){
return (Math.floor(Math.random() * 100) < percentage);
}
function capitalize(aString){
return aString.substring(0,1).toUpperCase() + aString.substring(1, aString.length);
}
function getLoremWord(){
return loremLang[Math.floor(Math.random()*loremLang.length)];
}
function getLoremEnding(){
var i = Math.floor(Math.random()*endings.length);
return endings.substring(i, i+1);
}
function loremIpsum(numWords) {
var words = "";
for(var i=0; i<numWords-1; i++){
words += getLoremWord() + " ";
}
return words;
}
function loremIpsumSentenceNoPunc(numWords) {
var words = "";
words += capitalize(getLoremWord()) + " ";
words += loremIpsum(numWords-1);
words += getLoremEnding() + " ";
return words;
}
function loremIpsumSentence(numWords) {
var words = "";
words += capitalize(getLoremWord()) + " ";
var part1 = 0;
if(chance(33)){
// insert a comma within the sentence
part1 = Math.floor(Math.random() * numWords-2);
words += loremIpsum(part1);
words = trim(words) + ", ";
} else {
words += " ";
}
words += loremIpsum(numWords - part1 - 1);
words = trim(words) + getLoremEnding() + " ";
return words;
}
function loremIpsumParagraph(numWords){
var thisNum = numWords;
var words = "";
while(thisNum > 0){
if(thisNum > 10){
w = Math.floor(Math.random() * 8) + 2;
words += loremIpsumSentence(w);
thisNum = thisNum - w;
} else {
words += loremIpsumSentence(thisNum);
thisNum = 0;
}
}
return trim(words.replace(/ /g,' '));
}
});
// some extra helpers
function uniquesOnly(str) {
arr = str.split('|');
temp = new Array();
for (i=0;i<arr.length;i++) {
if (temp.indexOf(arr[i]) == -1) {
temp.push(arr[i]);
}
}
return temp.join('|');
}
function trim(strText) {
while (strText.substring(0,1) == ' ')
strText = strText.substring(1, strText.length);
while (strText.substring(strText.length-1,strText.length) == ' ')
strText = strText.substring(0, strText.length-1);
return strText;
}
function SelectText(element) {
var text = document.getElementById(element);
if ($.browser.msie) {
var range = document.body.createTextRange();
range.moveToElementText(text);
range.select();
} else if ($.browser.mozilla || $.browser.opera) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(text);
selection.removeAllRanges();
selection.addRange(range);
} else if ($.browser.safari) {
var selection = window.getSelection();
selection.selectAllChildren(text);
}
}