-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathpr.js
222 lines (183 loc) · 6.17 KB
/
pr.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
(function (w, d) {
// Export to global namespace
w.pr = {
methods: {
loadURL: loadURL,
addClass: addClass,
removeClass: removeClass,
docReady: docReady,
goToTop: goToTop,
parseTextForEmojis: parseTextForEmojis,
getAjax: getAjax,
createElementByTag: createElementByTag,
createElementById: createElementById,
createElementByClassName: createElementByClassName,
selectElementByTag: selectElementByTag,
selectElementById: selectElementById,
selectElementByClassName: selectElementByClassName
}
}
// Functions and Operations
function loadURL(url) {
window.location.href = url
}
// Get a URL ; credit of youmightnotneedjquery.com
function getAjax(url, successFunc, errorFunc) {
if (!url) return Error('no url specified to getAjax')
successFunc = successFunc || function() { console.info('Success') }
errorFunc = errorFunc || function() { console.error('Error') }
var req = new XMLHttpRequest()
req.open('GET', url, true)
req.timeout = 15000 // 15sec timeout
req.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status >= 200 && this.status < 400) {
var response = this.responseText
successFunc(response)
}
else {
errorFunc()
}
}
}
req.send();
}
// addClass && removeClass ; credit of youmightnotneedjquery.com
function addClass(className, self) {
if (self.classList) self.classList.add(className)
else self.className = ' ' className;
}
function removeClass(className, self) {
if (self.classList) self.classList.remove(className);
else
self.className = self.className
.replace(
new RegExp('(^|\\b)'
className
.split(' ')
.join('|')
'(\\b|$)', 'gi'),
' ');
}
function createElementByTag(tag) {
return d.createElement(tag)
}
function createElementById(tag, id) {
var el = createElementByTag(tag)
el.id = id
return el
}
function createElementByClassName(tag, className) {
var el = createElementByTag(tag)
addClass(className, el)
return el
}
function selectElementByTag(tag) {
return d.querySelector(tag)
}
function selectElementById(id) {
return d.getElementById(id)
}
function selectElementByClassName(className) {
return d.querySelector(className)
}
// Same as jQuery(document).ready ; credit of youmightnotneedjquery.com
function docReady(fn) {
if (d.readyState !== 'loading') {
fn()
}
else if (d.addEventListener) {
d.addEventListener('DOMContentLoaded', fn, false)
}
else {
d.attachEvent('onreadystatechange', function () {
if (d.readyState !== 'loading') {
fn()
}
})
}
}
// Same as jQuery(el).html() ; gets html of domNode
function getHTML(domNode) {
return domNode.innerHTML
}
// Same as jQuery(el).html(html) ; sets html of domNode
function setHTML(domNode, html) {
domNode.innerHTML = html
}
// Scroll window to (x,y)
function scroll(x, y) {
window.scrollTo(x, y);
}
function goToTop() {
scroll(0, 0);
}
/* Emojis */
var emojiCodes = {
':بوس': '😘',
':عشق': '😍',
':سوال': '🤔',
':آغوش': '🤗',
':خنده': '😂',
':لبخند': '😊',
':پوکرفیس': '😐',
':چشمک': '😉',
':دی': '😁',
':پی': '😋',
}
function yahooCodesToEmoji(str){
let chart = [
{ replace: '🤗', pattern: /\>:(-?)d\< /ig, dublicateBy: /\>/ig}, // >:D<
{ replace: '😂', pattern: /:(-?)\){2,}/ig, double: true, dublicateBy: /\)/ig}, // :))))
{ replace: '😀', pattern: /:(-?)\)/ig}, // :)
{ replace: '😁', pattern: /:(-?)d /ig, dublicateBy: /d/ig}, // :DDD
{ replace: '🤣', pattern: /=(-?)\){2,}/ig, double: true, dublicateBy: /\)/ig}, // =))))
{ replace: '😊', pattern: /\^_\^/ig}, // ^_^
{ replace: '😋', pattern: /:(-?)p /ig, dublicateBy: /p/ig}, // :p
{ replace: '😘', pattern: /:(-?)\*{2,}/ig, double: true, dublicateBy: /\*/ig}, // :**
{ replace: '😙', pattern: /:(-?)\*/ig}, // :*
{ replace: '🤔', pattern: /:(-?)\? /ig, dublicateBy: /\?/ig}, // :?
{ replace: '😑', pattern: /:(-?)\|{2,}/ig, double: true, dublicateBy: /\|/ig}, // :||
{ replace: '😐', pattern: /:(-?)\|/ig}, // :|
{ replace: '😉', pattern: /;(-?)\) /ig, dublicateBy: /\)/ig}, // ;)
{ replace: '😭', pattern: /:(-?)\({2,}/ig, double: true, dublicateBy: /\(/ig}, // :((
{ replace: '☹', pattern: /:(-?)\(/ig}, // :(
{ replace: '😍', pattern: /:(-?)x /ig, dublicateBy: /x/ig}, // :x
{ replace: '😌', pattern: /:(-?)\"\> /ig, dublicateBy: /\>/ig}, // :">
{ replace: '😲', pattern: /:(-?)o /ig, dublicateBy: /\o/ig}, // :o
{ replace: '😢', pattern: /:'\( /ig, dublicateBy: /\(/ig}, // :'(
{ replace: '🤢', pattern: /:(-?)\& /ig, dublicateBy: /\&/ig}, // :&
];
let ret = str;
chart.forEach( (val)=>{
if( val.dublicateBy ){
let founds = str.match(val.pattern);
if( founds ){
founds.forEach( (found)=>{
let repeatLength = found.match(val.dublicateBy);
repeatLength = repeatLength? repeatLength.length: 1;
if( val.double ){
repeatLength = repeatLength-1;
}
ret = ret.replace( found, val.replace.repeat( repeatLength ) )
});
}
}
else{
ret = ret.replace( val.pattern, val.replace )
}
});
return ret;
}
function replaceAllCodesWithEmojis(txt) {
var codes = Object.keys(emojiCodes)
codes.forEach(function (code) {
txt = txt.replace(new RegExp(code, 'gi'), emojiCodes[code])
})
txt = yahooCodesToEmoji(txt);
return txt
}
function parseTextForEmojis(domNode) {
setHTML(domNode, replaceAllCodesWithEmojis(getHTML(domNode)))
}
})(window, document)