Last active
August 15, 2020 18:53
-
-
Save abhisheksambyal/c44000f810bcea809cd9a7f9a5575e28 to your computer and use it in GitHub Desktop.
If you want to use scramble.js in your website, you can use this code snippet to generate the scrambled email and indices. You can see the demo of scramble.js on https://jeffdonahue.com/.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Reference "scramble.js": https://jeffdonahue.com/scramble.js [added] | |
# https://jeffdonahue.com/ | |
# Author: Abhishek Singh Sambyal | |
# Usage: Modify the email variable with your email. | |
# Run: python generate_scrambled_email_indices.py | |
# Output: scrambled email string with indices | |
from random import random | |
email = '[email protected]' | |
email_list = list(email) | |
email_dict = {k:v for k, v in enumerate(email_list)} | |
shuffled_email_dict = dict(sorted(email_dict.items(), key=lambda x: random())) | |
scrambled_email = ''.join(shuffled_email_dict.values()) | |
scrambled_email_indices = list(shuffled_email_dict.keys()) | |
print(scrambled_email) | |
print(scrambled_email_indices) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// scramble.js | |
// | |
// 2011, Jeff Donahue (http://jeffdonahue.com/). | |
// license: you can use this if you want to i guess | |
function scrambledString(tag, objName, initScrambledString, initScrambledStringIndices) { | |
this.tag = tag; | |
this.objName = objName; | |
this.string = initScrambledString; | |
this.indices = initScrambledStringIndices; | |
this.rescramble = rescramble; | |
this.initAnimatedBubbleSort = initAnimatedBubbleSort; | |
this.bubbleSortStep = bubbleSortStep; | |
this.bubbleSortBookmark = 0; | |
this.rescramble(); | |
this.tag.innerHTML = this.string + ' <a href="#" onClick="' + this.objName + '.initAnimatedBubbleSort();return false;">unscramble</a>'; | |
} | |
function rescramble() { | |
for (i = 0; i < this.indices.length; i++) { | |
indexToMove = Math.floor(Math.random() * (this.indices.length - i)); | |
charIndexRemoved = this.indices.splice(indexToMove, 1); | |
this.indices = this.indices.concat(charIndexRemoved); | |
scrambledStringTemp = this.string.substring(0, indexToMove) + | |
this.string.substring(indexToMove + 1) + | |
this.string.substring(indexToMove, indexToMove + 1); | |
this.string = scrambledStringTemp; | |
} | |
} | |
function initAnimatedBubbleSort() { | |
this.interval = setInterval(this.objName + '.bubbleSortStep()', 12); | |
} | |
function bubbleSortStep() { | |
if (this.bubbleSortBookmark >= this.indices.length - 1) { | |
this.bubbleSortBookmark = 0; | |
} | |
for (i = this.bubbleSortBookmark; i < this.indices.length - 1; i++) { | |
if (i == 0) { | |
this.changed = 0; | |
} | |
if (this.indices[i] > this.indices[i + 1]) { | |
this.changed = 1; | |
tempIndex = this.indices[i]; | |
this.indices[i] = this.indices[i + 1]; | |
this.indices[i + 1] = tempIndex; | |
tempArrange = this.string.substring(0, i) + | |
this.string.substring(i + 1, i + 2) + | |
this.string.substring(i, i + 1) + | |
this.string.substring(i + 2); | |
this.string = tempArrange; | |
this.tag.innerHTML = this.string; | |
this.bubbleSortBookmark = i; | |
break; | |
} | |
} | |
this.bubbleSortBookmark = i; | |
if (!this.changed) { | |
clearInterval(this.interval); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added generate_scrambled_email_indices.py with scramble.js.