forked from Jeet-1577/ZOOM-ADS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
descrambler.js
138 lines (122 loc) ยท 11.5 KB
/
descrambler.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
(function( $ ) {
// scramble:
// duration: the duration over which the function will execute. Min is 200. Max is 20000. Parameter is type safe.
// interval: interval between new random letters being added/removed. Default is 20. Min is 5. Max is 10000. Parameter is type safe.
// charset: the character set to use. Optional. Default is 'all'.
// uppercase: determines whether scrambled characters will be uppercase or lowercase. Optional. Boolean.Default is true.
$.fn.scramble = function( duration, interval, charset, uppercase ) {
// duration: The time, in ms, over which the function is to execute.
// If duration is not a valid number or outside min/max (200ms/2000ms), default duration (3000) will be used.
// NOTE: isNaN does not operate consistently across browsers and browser support for Number.isInteger is poor. Until then, this mess is the best I can do to make the duration argument type-safe.
if ((typeof(duration) !== "number") || (duration === NaN) || (duration < 1000) || (duration > 20000)) {
duration = 3000;
}
// interval: If interval is not a valid number or outside min/max (5/10000ms), default interval 20 will be used.
// NOTE: isNaN does not operate consistently across browsers and browser support for Number.isInteger is poor. Until then, this mess is the best I can do to make the interval argument type-safe.
if ((typeof(interval) !== "number") || (interval === NaN) || (interval < 5) || (interval > 1000)) {
interval = 20;
}
// charsets: pre-defined character sets to use for descrambling characters.
// Default: charsets.all.
var charsets = {
numbers: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"],
alphabet: ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"],
punctuation: ["@", "#", "$", "%", "^", "*", "(", ")", "&", "+", "=", "}", "{", "|", ":", ";", ">", "<", "?", "~", " "],
emoji: ["๐ธ๐ผ","๐ฉ๐ฐ","โ","๐๐ฝ","๐","๐","๐๐ฝ","๐ฑ๐น","๐","๐","๐ต๐พโโ","๐ป","๐๐ปโโ","๐ ","๐ฉ","๐ฅ","๐๐ฟโโ","๐ณ","โ","๐","๐๐ผโโ","๐ฌ๐ผ","๐","๐","๐ฌ๐น","๐","๐๐ผ","โ๐ฟ","๐","โน๐ฟโโ","๐","๐ฑ๐ผ","๐๐ฟโโ","๐ฏ","๐ฏ","๐ฐ","๐ก","๐ฃ","๐น","๐","๐ต๐ฟ","๐๐ป","๐๐ผโโ","๐ป๐ณ","๐โโ","๐ต๐ผ","๐ฆ","๐","๐๐ผ","๐๐ฟ","๐ง๐ฟ","๐ซ","๐๐พ","๐ต๐ผโโ","๐พ๐ช","๐น","๐ค","๐","๐บ","๐ฌ๐ฑ","๐ท๐ธ","๐ฟ๐ฆ","๐ค๐ป","๐ผ๐ผ","๐๐ฝ","๐","๐ก","๐ฆ","๐","๐ผ","๐จ","๐ณ","๐","๐ฃ","๐","๐","๐","๐๐ผ","๐ค","๐ช๐ฟ","๐ฎ๐พ","โน๐ผโโ","๐ฑ๐บ","๐๐ฟ","๐","๐๐ฝ","๐ฉ๐ผ","๐ฒ๐ผ","๐ฝ","๐ฅ","๐ฅ","๐ฝ","๐ฌ๐ญ","๐น","๐ด๐ฝ","๐ฉ","๐","๐ผ","๐จ๐ฆ","๐ต","๐ฌ","โ๐ป","๐ฐ","๐พ","๐ง๐น","๐ธ๐ธ","๐ฉ","๐ฒ","โ","๐","๐ค","๐ฑโโ","๐ฑ๐ฎ","๐ต๐ป","๐ช","๐ฌ๐ถ","๐ณ๐ด","๐ ","๐๐ป","๐ช๐น","โช","๐","๐ผ","๐ด๐พโโ","๐","๐๐ฟโโ","๐ฑ๐พโโ","๐๐ผ","๐ป๐จ","โ๐ฝ","๐
๐ปโโ","๐ต๐ญ","๐ฅ","๐","๐ฐ๐ผ","๐๐ปโโ","โญ","๐ช๐ผ","โ","๐ณ๐ฝ","๐
๐ฟ","๐๐พ","๐","๐ด","๐๐ฝโโ","๐","๐ผ","๐บ","๐๐ผโโ","๐๐ผ","๐ง๐ณ","๐ฑ๐ฟโโ","๐๐ผ","๐๐ฝโโ","๐๐ผโโ","โน๐พโโ","๐ณ","๐ง๐ฝ","๐","โ","๐๐ป","๐ฆ","๐ฏโโ","๐จ๐ต","๐","๐","๐๐ผ","๐๐ฝโโ","๐ก","๐๐ฝ","๐ณ๐พโโ","๐จโ๐ง","๐จ๐ฉ","๐","๐บ๐ฌ","๐ง๐ฟ","๐ฑ๐ฝ","๐ช๐บ","๐","๐","โ
","โต","๐ค","๐ฒ๐ท","๐","๐","๐๐พโโ","๐ฆ","๐","๐","๐ญ","๐๐พโโ","๐ฎ๐ผ","๐","๐","๐โ๐จ","๐ซ๐ฏ","๐ฏ","๐๐ป","๐๐ป","๐น","๐
","๐ง","๐โโ","๐","๐ป","๐พ","๐ท๐ฝโโ","๐โโ","๐","๐๐ป","๐","๐จ๐ฟ","๐ณโโ","โ๐ฟ","๐","๐","๐ถ๐ผ","๐ผ","๐ฎ","๐ฆ","๐๐ฟ","๐บ","๐๐ป","๐ป๐ฆ","๐๐ผ","๐","๐","๐","๐๐ป","๐ฐ","๐ฌ๐ซ","๐","๐ฅ","๐ ","๐","๐ท","๐ฉ๐ฟ","๐
","๐๐พ","๐ต๐ฟโโ","๐๐ผโโ","๐ถ","๐ง๐ฑ","๐ธ๐ป","๐ต๐ฟ","๐๐ป","๐จโ๐จโ๐ง","๐๐ผ","โณ","๐","๐ธ๐ด","๐ค๐พ","๐
๐ผ","๐ป","๐ก","๐ฎ๐ณ","๐","๐","๐","5โฃ","๐ฌ๐ฆ","๐จโ๐ฉโ๐งโ๐ฆ","๐","๐","๐๐พ","โ๐ผ","๐ช๐ฆ","๐","๐จ","๐ผ","๐","๐ฐ","๐","๐","๐น๐ฆ","๐๐ฟ","๐ฎ๐ปโโ","๐ต๐ท","โ","โฌ","๐๐ผ","๐ธ๐ญ","๐ง๐ญ","โ","๐
","๐ฒ๐ณ","โ","โฆ๏ธ","๐","๐๐ฝ","๐ฒ๐ป","๐","๐๐ป","๐ผ๐ป","๐ฒ๐ฟ","๐น","๐ฌ๐ฌ","๐๐ป","โฎ","๐ช๐ญ","๐ผ๐ฝ","๐ง๐ซ","๐","๐","๐","๐๐ป","๐ณ๐ป","๐ถ๐ผ","๐ฆ๐ฒ","๐ท","๐","๐ฎ","๐๐ผโโ","๐ธ๐ป","๐ต๐พ","๐ฑ๐ท","๐ง๐ธ","๐๐ฟโโ","๐ฉโ๐ฉโ๐ฆ","๐ธ","๐ค","๐","๐ฆ","๐ฟ","๐๐ฝโโ","๐ธ๐ฆ","๐","๐ฏ๐ต","๐โโ","๐","๐
โโ","๐๐ปโโ","๐พ","๐","๐ธ","โฝ","๐๐พโโ","๐๐ฟ","๐","๐ฎ๐ฟ","๐ท๐พ","๐๐พโโ","๐ฉ","๐ ","๐","๐ฃ","๐ฒ๐ญ","๐๐ฝ","๐","๐","๐ญ๐ณ","๐","๐๐พ","๐","๐ฒ๐ถ","๐","๐ต๐ฟ","๐ท๐ป","๐","๐
๐ฝ","๐","๐","๐ง๐พ","๐ญ","๐ธ๐ฒ","๐จ","๐ฆ๐ฑ","๐ง๐ป","๐ด๐พ","๐ถ","๐๐ผ","๐ณ","๐ฌ๐ฉ","๐พ","๐","๐ต๐ผโโ","๐ตโโ","๐ฉ","๐ป","๐๐ฝ","๐๐พ","๐ต๐ปโโ","๐ซ","๐น๐ซ","๐ซ","๐ท","๐ธ๐ฝ","๐ป๐ฎ","๐ฆ๐ท","๐ฆ๐ช","๐","๐ฉ๐ฏ","๐","๐ค๐ฝ","๐ค","๐ธ๐ฐ","๐ฉโ๐งโ๐ฆ","๐๐พ","๐ฎ๐ฟโโ","๐ธ๐พ","๐ถ๐ฟโโ","๐ช๐ป","๐ต๐ธ","๐๐พ","๐","๐","๐ฌ๐ฎ","๐","๐ต๐ฒ","๐ท๐ผโโ","โ","๐","๐ณ๐ฝโโ","๐๐ฝ","๐","๐ฃ๐ป","๐ฌ๐ท","๐ฃ๐ฟ","๐ท","๐","๐ฒ๐บ","๐","๐๐ป","๐","๐ฌ๐ธ","๐","๐ฆ๐ธ","๐","๐๐พโโ","๐ธ๐ณ","๐","๐ญ","๐","๐ต","๐
","๐ฌ","๐","๐","๐ธ","๐บ","๐ฌ","๐ ","๐
๐ฟ","๐๐ฝ","๐๐พ","๐ง๐พ","๐ง๐ด","๐๐ผ","๐
๐พ","๐ณ๐ปโโ","๐","๐ณ๐บ","๐จ๐ฝ","๐๐ผ","๐ฉ๐ฟ","๐ฑ๐ป","๐ ","๐๐พ","๐","๐","๐๐ฝโโ","โพ","๐","๐
","๐ฑ","๐ณ","๐๐ป","๐๐ฟ","๐๐ฟโโ","๐๐ฝโโ","๐ณ๐พ","โ","๐","๐","๐ฒ๐ด","โน๐ผ","๐ ","๐ฃ๐ฟ","๐ณ","๐ผ","๐","โ๐ป","๐
โโ","๐","๐๐ผ","๐ง","๐
๐ป","๐","๐ฆ๐บ","๐๐ป","๐ฏ","๐","๐","๐ด","๐๐ป","๐","๐","๐ช","๐ซ"],
get alphanumeric() {
return this.numbers.concat(this.alphabet);
},
get all() {
return this.alphanumeric.concat(this.punctuation);
},
get allPlusEmoji() {
return this.all.concat(this.emoji)
}
};
// If no charset parameter is passed in, or if an invalid charset parameter is passed in, the default charset (all) will be used.
if ((charset === undefined) || !(charset in charsets) ) {
charset = "all";
}
charset = charsets[charset];
// uppercase: Whether uppercase character set should be used.
// If uppercase boolean not defined, or if an invalid parameter is passed, the default false is used.
if ((uppercase === undefined) || (uppercase === false)) {
uppercase = false;
} else {
uppercase = true;
}
// originalText: the text contents of the the element on which the scramble method is called. Once stored, the text of the element is cleared.
var originalText = this.text();
this.text("");
// random: returns a random letter from the characterList array (this function is always called passing in the charset variable).
var random = function(characterList) {
var length = characterList.length;
var randomNumber = Math.floor(Math.random() * length);
var letter = characterList[randomNumber];
if (uppercase) {
return letter.toUpperCase();
} else {
return letter;
}
};
// replacer: takes three arguments - scambledArray is an array of random characters the same length as the original word; originalArr is the original word, split into an array; charsToReplace is the number of characters to replace (counting from index 0).
var replacer = function(scrambledArray, originalArr, charsToReplace) {
// take the first charsToReplace number of characters from the original array...
var replacement = originalArr.slice(0, charsToReplace);
// ...join that array on the empty string...
replacement = replacement.join("");
// ... and replace the first n characters in the scrambledArray with the replacement string.
return scrambledArray.splice(0, replacement.length, replacement);
};
var scrambler = function(lengthOfArray) {
var scrambledArray = [];
for (var i = 0; i < lengthOfArray; i++) {
scrambledArray.push(random(charset));
}
return scrambledArray;
};
// constructor function: returns a wordScrambler object, including details of the word, the iteration number, etc. Internal 'scramble' function performs the substantive work of the whole method.
var wordScrambler = function($element, word) {
this.iteration = 0;
this.spliceIteration = 0;
this.$element = $element;
this.word = word;
this.len = word.length;
this.arr = word.split("");
this.terminated = false;
$element.on("click", (e) => {
this.terminated = true;
});
var magicNumber = parseInt(duration / interval / this.len);
// this.scramble takes a setInterval timer parameter (necessary to clear the setInterval when the slice iterations have completed). Called every interval milliseconds, calls the replacer function, which returns an array of un/scrambled letters, and re-sets the .text of this.$element
this.scramble = function(timer) {
if (this.terminated) {
this.$element.text(word)
window.clearInterval(timer);
return;
}
this.iteration += 1;
// Get an array of random characters the same length as the text of the element on which the method is called.
var scrambledArray = scrambler(this.len);
if (this.iteration % magicNumber === 0) {
this.spliceIteration += 1;
}
replacer(scrambledArray, this.arr, this.spliceIteration);
// }
// Join the array of characters in scrambledArray (once modified by replacer function).
var scrambledWord = scrambledArray.join("");
// Replace the text of the element on which the method was called with that string.
this.$element.text(scrambledWord);
// Once all letters have been replaced, clear the setInterval passed in to this function.
if (this.spliceIteration === this.len) {
window.clearInterval(timer);
}
};
};
var text = new wordScrambler(this, originalText);
var intervalTimer = window.setInterval(function() {
text.scramble(intervalTimer);
}, interval);
return this;
};
}(jQuery));