Skip to content

Instantly share code, notes, and snippets.

@BubuInc
Last active November 10, 2019 05:30
Show Gist options
  • Save BubuInc/8f05a597112bb9599c82 to your computer and use it in GitHub Desktop.
Save BubuInc/8f05a597112bb9599c82 to your computer and use it in GitHub Desktop.
Encode text method Thunder
// ==UserScript==
// @name EncodeText
// @namespace mangareader
// @version 0.23
// @description Codificar texto, activar con Ctrl + Shift + H, desactivar con Esc
// @author http://www.taringa.net/MangaReader/
// @match http://*/*
// @match https://*/*
// @grant none
// ==/UserScript==
var v = ['a','e','i','o','u'];
var c = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'];
function isUpperCase(str) {
return (str == str.toUpperCase());
}
function enc (str) {
var newStr='', l, index, newIndex;
for (var i = 0; i < str.length; i++) {
var isUpp = isUpperCase(str[i]);
l = str[i].toLowerCase();
if(v.indexOf(l) != -1){//Es vocal
index = v.indexOf(l);
newIndex = (index != v.length - 1) ? index+1 : 0;
l = v[newIndex];
}else if(c.indexOf(l) != -1){//Es consonante
index = c.indexOf(l);
newIndex = (index != c.length - 1) ? index+1 : 0;
l = c[newIndex];
}
if(isUpp) l = l.toUpperCase();
newStr += l;
}
return newStr;
}
function dec (str) {
var newStr='', l, index, newIndex;
for (var i = 0; i < str.length; i++) {
var isUpp = isUpperCase(str[i]);
l = str[i].toLowerCase();
if(v.indexOf(l) != -1){//Es vocal
index = v.indexOf(l);
newIndex = (index != 0) ? index-1 : v.length - 1;
l = v[newIndex];
}else if(c.indexOf(l) != -1){//Es consonante
index = c.indexOf(l);
newIndex = (index != 0) ? index-1 : c.length - 1;
l = c[newIndex];
}
if(isUpp) l = l.toUpperCase();
newStr += l;
}
return newStr;
}
function encDec (event) {
var el = event.target;
if(el.nodeName == 'TEXTAREA' || el.nodeName == 'INPUT'){
el.value = enc(el.value);
}else if (el.textContent != '') {
var nodes = el.childNodes;
for (var i = 0; i < nodes.length; i++) {
if(nodes[i].nodeName == "#text"){
nodes[i].nodeValue = dec(nodes[i].textContent);
}
}
}
}
function highlight(event){
var el = event.target;
if ( el.nodeName == 'TEXTAREA' || el.nodeName == 'INPUT' || el.textContent != '') {
event.target.style.cursor = 'pointer';
event.target.style.textDecoration = 'underline';
}
}
function restHigh(event){
event.target.style.textDecoration = '';
event.target.style.cursor = '';
}
function remListeners () {
window.document.body.removeEventListener('mouseover',highlight);
window.document.body.removeEventListener('mouseout',restHigh);
window.document.body.removeEventListener('click',encDec);
}
function addListeners () {
window.document.body.addEventListener('mouseover',highlight);
window.document.body.addEventListener('mouseout',restHigh);
window.document.body.addEventListener('click',encDec);
}
// Se activa con Ctrl+Shift+H
function onkeydown (event) {
var k = event.keyCode;
if (k == 27) {remListeners();} //Esc
else if(event.shiftKey === true && event.ctrlKey === true && k == 72){
addListeners();
}
}
window.document.body.addEventListener('keydown', onkeydown);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment