Greasy Fork is available in English.

讨论 » 创建请求

A little help porting a simple chrome extension as user script

§
发表于:2016-05-27

A little help porting a simple chrome extension as user script

It is a script that replaces text with random characters from an unknown script so that you can focus on the images or ignore bordersome text, without collapsing it. The chrome extension had a button to click when you visited a page you wanted to change characters.

var frequent = ['అ','ఇ','ఉ','ఎ','ౡ','ఒ']; var med = ['ఌ','ఐ','ఆ','ఊ','ౠ','ఏ']; var rare = ['ఋ','ఓ','అం','ఈ'];

chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) { if( request.message === "clicked_browser_action" ) { replaceTxt(); } } );

function replaceTxt(){ var elements = document.getElementsByTagName('*');

for (var i = 0; i < elements.length; i++) {
    var element = elements[i];

    for (var j = 0; j < element.childNodes.length; j++) {
        var node = element.childNodes[j];

        if (node.nodeType === 3 && node.parentElement.nodeName != "script" && node.parentElement.nodeName != "SCRIPT" && node.parentElement.nodeName != "noscript" && node.parentElement.nodeName != "NOSCRIPT" && node.parentElement.nodeName != "style" && node.parentElement.nodeName != "STYLE") {
            var text = node.nodeValue;

            var array = text.split('');
            for (k = 0; k < array.length; k++) { 
                if(array[k] != String.fromCharCode(32) && array[k] != String.fromCharCode(160)  && array[k] != String.fromCharCode(8232) && array[k] != String.fromCharCode(13) && array[k] != "\r" && array[k] != "\n" && array[k] != String.fromCharCode(9)){
                    array[k] = replaceChar();
                }
            }
            var replacedText = array.join('');
            if (replacedText !== text) {
                element.replaceChild(document.createTextNode(replacedText), node);
            }
        }
    }
}

}

function replaceChar(){ var temp = Math.random()* 10; if(temp <= 1){ return rare[Math.floor(Math.random()*rare.length)]; } else if (temp <= 3){ return med[Math.floor(Math.random()*med.length)]; } else{ return frequent[Math.floor(Math.random()*frequent.length)]; } }

Pastebin for easier view: http://pastebin.com/0yWPakv8

§
发表于:2016-05-29

solved it

发表回复

登录以发表回复。