Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to execute the following JavaScript on the backend by recreating the logic with C# code. I'm trying to obtain the "sucuri_cloudproxy_js" cookie in order to access webcontent, but in order to obtain this cookie, you must execute this JavaScript. What is the most efficient way to execute javascript in C#? Thank you!
var s = {},
u, c, U, r, i, l = 0,
a, e = eval,
w = String.fromCharCode,
sucuri_cloudproxy_js = '',
S = 'dj0nd0U3Jy5jaGFyQXQoMikrU3RyaW5nLmZyb21DaGFyQ29kZSg5OSkgKyAiIiArIjZzdSIuc2xpY2UoMCwxKSArICI5c3UiLnNsaWNlKDAsMSkgKyAnNScgKyAgIjUiICsgImZzZWMiLnN1YnN0cigwLDEpICsgIjNzdWN1ciIuY2hhckF0KDApKyAnJyArIAoiMyIgKyAiIiArImQiLnNsaWNlKDAsMSkgKyAgJycgKyIwc3UiLnNsaWNlKDAsMSkgKyAgJycgKydlJyArICAiYyIgKyAiIiArImJzdWN1ciIuY2hhckF0KDApK1N0cmluZy5mcm9tQ2hhckNvZGUoMHgzMikgKyAgJycgKycnKydlJyArICAiOCIuc2xpY2UoMCwxKSArICAnJyArJ2ZLNycuY2hhckF0KDIpKydANCcuc2xpY2UoMSwyKSsiIiArImQiICsgICcnICsgCiJiIiArICI5IiArICAnJyArJycrU3RyaW5nLmZyb21DaGFyQ29kZSg1NCkgKyAiYiIgKyAgJycgKyAKIjYiICsgJzAnICsgICIiICsiNyIgKyAnb01kJy5jaGFyQXQoMikrImFzZWMiLnN1YnN0cigwLDEpICsgU3RyaW5nLmZyb21DaGFyQ29kZSg0OSkgKyAgJycgKycnKyc4JyArICAiZHNlYyIuc3Vic3RyKDAsMSkgKyAnJztkb2N1bWVudC5jb29raWU9J3NzdWMnLmNoYXJBdCgwKSsgJ3VzdWMnLmNoYXJBdCgwKSsgJ2MnKyd1JysncicrJ2knKydfJysnYycuY2hhckF0KDApKydsc3UnLmNoYXJBdCgwKSArJ3N1Y3VybycuY2hhckF0KDUpICsgJ3UnKydzdWN1cmQnLmNoYXJBdCg1KSArICdwc3VjdXInLmNoYXJBdCgwKSsgJ3InKydvJysneHN1Y3VyJy5jaGFyQXQoMCkrICd5Jy5jaGFyQXQoMCkrJ18nKycnKyd1c3VjdXInLmNoYXJBdCgwKSsgJ3N1Jy5jaGFyQXQoMSkrJ2knKycnKydkJysnX3MnLmNoYXJBdCgwKSsnM3N1Y3UnLmNoYXJBdCgwKSAgKycwJysnZCcrJzEnLmNoYXJBdCgwKSsnOXN1Y3UnLmNoYXJBdCgwKSAgKydzdTknLmNoYXJBdCgyKSsnc3VjdXIzJy5jaGFyQXQoNSkgKyAnYXN1Y3VyaScuY2hhckF0KDApICsgJ2YnKyI9IiArIHY7IGxvY2F0aW9uLnJlbG9hZCgpOw==';
L = S.length;
U = 0;
r = '';
var A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
for (u = 0; u < 64; u++) {
s[A.charAt(u)] = u;
}
for (i = 0; i < L; i++) {
c = s[S.charAt(i)];
U = (U << 6) + c;
l += 6;
while (l >= 8) {
((a = (U >>> (l -= 8)) & 0xff) || (i < (L - 2))) && (r += w(a));
}
}
e(r);
This looks like some sort of base 64 decoding algorithm. There are built in base64 decoders in C# you could try. Alternatively, if 'S' never changes, you could just execute this in javascript and put the result in to your C# program.
executing this in javascript console:
var s = {},
u, c, U, r, i, l = 0,
a, e = eval,
w = String.fromCharCode,
sucuri_cloudproxy_js = '',
S = 'dj0nd0U3Jy5jaGFyQXQoMikrU3RyaW5nLmZyb21DaGFyQ29kZSg5OSkgKyAiIiArIjZzdSIuc2xpY2UoMCwxKSArICI5c3UiLnNsaWNlKDAsMSkgKyAnNScgKyAgIjUiICsgImZzZWMiLnN1YnN0cigwLDEpICsgIjNzdWN1ciIuY2hhckF0KDApKyAnJyArIAoiMyIgKyAiIiArImQiLnNsaWNlKDAsMSkgKyAgJycgKyIwc3UiLnNsaWNlKDAsMSkgKyAgJycgKydlJyArICAiYyIgKyAiIiArImJzdWN1ciIuY2hhckF0KDApK1N0cmluZy5mcm9tQ2hhckNvZGUoMHgzMikgKyAgJycgKycnKydlJyArICAiOCIuc2xpY2UoMCwxKSArICAnJyArJ2ZLNycuY2hhckF0KDIpKydANCcuc2xpY2UoMSwyKSsiIiArImQiICsgICcnICsgCiJiIiArICI5IiArICAnJyArJycrU3RyaW5nLmZyb21DaGFyQ29kZSg1NCkgKyAiYiIgKyAgJycgKyAKIjYiICsgJzAnICsgICIiICsiNyIgKyAnb01kJy5jaGFyQXQoMikrImFzZWMiLnN1YnN0cigwLDEpICsgU3RyaW5nLmZyb21DaGFyQ29kZSg0OSkgKyAgJycgKycnKyc4JyArICAiZHNlYyIuc3Vic3RyKDAsMSkgKyAnJztkb2N1bWVudC5jb29raWU9J3NzdWMnLmNoYXJBdCgwKSsgJ3VzdWMnLmNoYXJBdCgwKSsgJ2MnKyd1JysncicrJ2knKydfJysnYycuY2hhckF0KDApKydsc3UnLmNoYXJBdCgwKSArJ3N1Y3VybycuY2hhckF0KDUpICsgJ3UnKydzdWN1cmQnLmNoYXJBdCg1KSArICdwc3VjdXInLmNoYXJBdCgwKSsgJ3InKydvJysneHN1Y3VyJy5jaGFyQXQoMCkrICd5Jy5jaGFyQXQoMCkrJ18nKycnKyd1c3VjdXInLmNoYXJBdCgwKSsgJ3N1Jy5jaGFyQXQoMSkrJ2knKycnKydkJysnX3MnLmNoYXJBdCgwKSsnM3N1Y3UnLmNoYXJBdCgwKSAgKycwJysnZCcrJzEnLmNoYXJBdCgwKSsnOXN1Y3UnLmNoYXJBdCgwKSAgKydzdTknLmNoYXJBdCgyKSsnc3VjdXIzJy5jaGFyQXQoNSkgKyAnYXN1Y3VyaScuY2hhckF0KDApICsgJ2YnKyI9IiArIHY7IGxvY2F0aW9uLnJlbG9hZCgpOw==';
L = S.length;
U = 0;
r = '';
var A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
for (u = 0; u < 64; u++) {
s[A.charAt(u)] = u;
}
for (i = 0; i < L; i++) {
c = s[S.charAt(i)];
U = (U << 6) + c;
l += 6;
while (l >= 8) {
((a = (U >>> (l -= 8)) & 0xff) || (i < (L - 2))) && (r += w(a));
}
}
r;
gives this:
v='wE7'.charAt(2)+String.fromCharCode(99) + "" +"6su".slice(0,1) + "9su".slice(0,1) + '5' + "5" + "fsec".substr(0,1) + "3sucur".charAt(0)+ '' +
"3" + "" +"d".slice(0,1) + '' +"0su".slice(0,1) + '' +'e' + "c" + "" +"bsucur".charAt(0)+String.fromCharCode(0x32) + '' +''+'e' + "8".slice(0,1) + '' +'fK7'.charAt(2)+'#4'.slice(1,2)+"" +"d" + '' +
"b" + "9" + '' +''+String.fromCharCode(54) + "b" + '' +
"6" + '0' + "" +"7" + 'oMd'.charAt(2)+"asec".substr(0,1) + String.fromCharCode(49) + '' +''+'8' + "dsec".substr(0,1) + '';document.cookie='ssuc'.charAt(0)+ 'usuc'.charAt(0)+ 'c'+'u'+'r'+'i'+'_'+'c'.charAt(0)+'lsu'.charAt(0) +'sucuro'.charAt(5) + 'u'+'sucurd'.charAt(5) + 'psucur'.charAt(0)+ 'r'+'o'+'xsucur'.charAt(0)+ 'y'.charAt(0)+'_'+''+'usucur'.charAt(0)+ 'su'.charAt(1)+'i'+''+'d'+'_s'.charAt(0)+'3sucu'.charAt(0) +'0'+'d'+'1'.charAt(0)+'9sucu'.charAt(0) +'su9'.charAt(2)+'sucur3'.charAt(5) + 'asucuri'.charAt(0) + 'f'+"=" + v; location.reload();
take off the location.reload and execute that and it results in a string:
"sucuri_cloudproxy_uuid_30d1993af=7c6955f33d0ecb2e874db96b607da18d"
all of that was originally passed through the eval function, so in the end, I'm guessing you want the 7c...18d, or maybe something to do with the uuid variable.
if S changes, then you'll need to reverse engineer this whole thing, or find a way to leverage a server side tool to execute javascript. You could use something like phantomjs perhaps.
Adding to the answer from Mark Evaul, the cookie content should be available in your controller code by referencing:
Request.Cookies["sucuri_cloudproxy_js"].Value
You can run the replacement algorithm on the value to decode the value of the cookie.
Related
Is there any way to deobfuscate some javascript code that produced with webpack 4 and is also splitChunked?
It's a little more than 1MB js code and I only need to understand a small portion of the code, which is this function :
function l(e) {
t.d(8, function(e) {
for (var n = e.length, r = t.b(n), f = a(), c = 0; c < n; c++) {
var i = e.charCodeAt(c);
if (i > 127)
break;
f[r + c] = i
}
if (c !== n) {
0 !== c && (e = e.slice(c)),
r = t.c(r, n, n = c + 3 * e.length);
var d = a().subarray(r + c, r + n);
c += o(e, d).written
}
return u = c,
r
}(e), u);
var n, r, f = (null !== i && i.buffer === t.e.buffer || (i = new Int32Array(t.e.buffer)),
i), c = (n = f[2],
r = f[3],
d.decode(a().subarray(n, n + r))).slice();
return t.a(f[2], 1 * f[3]),
c
}
I used chrome debugger and set some breakpoints and I was able to grasp what it's doing but I need to do the exact same thing in my project So I need a more readable code to do that.
As far as I know, there is no easy way to do so, but I want to share some tips with you:
Replace all ',' with ', '\n'
Review and correct the possible code breaks
Use a for loop to iterate between static value arrays and replace all the usages.
Change the encoding in case of language dependent breaks
Replace all ; with ; \n and correct possible code breaks
Know it is easier to rename functions and write comments for them.
https://jsfiddle.net/2L4t9saq/180/ is my fiddle
most of the code is just useless, ill just post the stuff that matters
var baseConverter = function(r, e, n) {
var o = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (e <= 0 || e > o.length || n <= 0 || n > o.length) return console.log("Base unallowed"), null;
var l, t = 0;
if (10 != e) {
var a = r.length;
for (l = 0; l < a; l++) {
var u, f = -1;
for (u = 0; u < o.length; u++)
if (r[l] == o[u]) {
f = 1;
break
}
if (u >= e) return console.log("Symbol unallowed in baseform"), null;
if (-1 == f) return console.log("Symbol not found"), null;
var s = a - l - 1;
t += 0 == s ? u : u * Math.pow(e, s)
}
} else t = parseInt(r);
if (10 != n) {
for (var g = []; t > 0;) {
var i = t % n;
if (i < 0 || i >= o.length) return console.log("Out of bounds error"), null;
g.push(o[i]), t = parseInt(t / n)
}
return g.reverse().toString().replace(/,/g, "")
}
return t.toString()
}
var b36torgba = function(input) {
for (var i = 1; i < (input.length / 8) + 1; i++) {
var arr = input
var r = arr.charAt[0 + (i - 1) * 8] + "" + arr.charAt[1 + (i - 1) * 8]
var g = arr.charAt[2 + (i - 1) * 8] + "" + arr.charAt[3 + (i - 1) * 8]
console.log(g.charAt[2])
var b = arr.charAt[4 + (i - 1) * 8] + "" + arr.charAt[5 + (i - 1) * 8]
console.log(b)
var a = arr.charAt[6 + (i - 1) * 8] + "" + arr.charAt[7 + (i - 1) * 8]
console.log(a)
var rrgba = baseConverter(r, 36, 10)
var grgba = baseConverter(r, 36, 10)
var brgba = baseConverter(r, 36, 10)
var argba = baseConverter(r, 36, 10)
var bigMessOfAVariable = "rgba(" + rrgba + "," + grgba + "," + brgba + "," + argba + "),"
return bigMessOfAVariable;
}
}
you can ignore the top function, all it is is a base converter script, that takes in three inputs, an input, the base its in, and the base it should be converted to: eg baseConverter(73,36,10) will output 255.
now, the problem is with my b36torgba function.
it will take in a string, which is guaranteed to have a length that is either 0, 8, or a multiple of 8, this is just standardization to make sure everything runs smoothly, without having 700 indexOf[] functions.
it takes in the input, and divides it by 8, this tells the function how many bytes it has to go through, and how many it will spit out, so a string "[7300002S7300002S]" should (divided by 8) output 2, therefore the script runs 2 iterations.
currently, it should be taking in the string, and assigning each group of 2 characters (again standard) to a specific variable, this will allow it to all be put in the end and outputted as the same string but in base 10 rgba (hence 73 being use, 73 in base 36 is 255), but before it can do any of that, it breaks when it tries to find the characters in a string, saying this syntax error:
Uncaught TypeError: Cannot read property '0' of undefined
at b36torgba ((index):40)
at window.onload ((index):55)
why does it break as soon as it tries to feed the string into my charAt()'s?
ps: i do understand that the code in its current state, if it worked, it'd only output the rgba value of the last 8 characters
Easy mistake. You're using charAt (which is a function) by doing charAt[index] (using square brackets), rather than charAt(index) (using round brackets). Fixing that up should solve your issue.
Also - you're calling the function by doing b36torgba(["7300002S7300002S"]) in your JSFiddle, and trying to do string manipulation on it. Since ["7300002S7300002S"] is an array, not a string, .charAt() won't work on it. Try calling the function by doing b36torgba("7300002S7300002S") instead.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am very new to the front end programming and I sometimes do not get the meaning of the shortcuts of the javascript and jquery. I have some code template to work on and I do not understand clearly that how it is working which are as below.
Will you please help me to get through article to understand these definations which are in the js file.
Thank you in advance.
I have something like this. I just want to know that how it works! I am not getting the clear idea.
1
for (var e = document.getElementsByTagName("div"), t = 0; t < e.length; t++)
"fish" == e[t].getAttribute("class") && fishArray.push(e[t])
2.
"vertical" == layersMovement ? (balloonDiv.style.left = o + "px", robbyContainerDiv.style.left = n + "px") : "not moving 1" == layersMovement ||
"not moving 2" == layersMovement ? (robbyContainerDiv.style.left = n + pageVerticalPosition - (pageDiv.offsetHeight - containerDiv.offsetHeight - distanceBetweenRobbyAndBalloon) + "px",
balloonDiv.style.left = o + "px") : (balloonDiv.style.left = layerHorizontalArray[layerHorizontalArray.length - 1].offsetLeft + layerHorizontalArray[layerHorizontalArray.length - 1].offsetWidth - .5 * (containerDiv.offsetWidth + balloonDiv.offsetWidth) + "px",
robbyContainerDiv.style.left = "50%")
Transforming it to cleaner code will help you understand:
for (var e = document.getElementsByTagName("div"), t = 0; t < e.length; t++) "fish" == e[t].getAttribute("class") && fishArray.push(e[t])
cleanning the code:
var e = document.getElementsByTagName("div");
==> Defining e to hold all div elements
for (var t = 0; t < e.length; t++)
==> Loop for each div element in e. The first section of for statement can be used to initialize variables. For example: for (var a = 0, b = 2, c = 3; a < b; a++) This is why the original code defined e inside the for statement.
"fish" == e[t].getAttribute("class") && fishArray.push(e[t])
==> a && b evaluates a and in case the evaluation result is true, b is performed. It is a shortcut for if (a) { b; }. In this statement, if the class attribute of the current div equals to 'fish', push the reference of the div to fishArray array.
In other words, the code can be written cleaner like this:
var e = document.getElementsByTagName("div");
for (var t = 0; t < e.length; t++) {
var currentDiv = e[t];
if ("fish" == currentDiv.getAttribute("class")) {
fishArray.push(e[t]);
}
}
The second code:
"vertical" == layersMovement ? (balloonDiv.style.left = o + "px", robbyContainerDiv.style.left = n + "px") : "not moving 1" == layersMovement || "not moving 2" == layersMovement ? (robbyContainerDiv.style.left = n + pageVerticalPosition - (pageDiv.offsetHeight - containerDiv.offsetHeight - distanceBetweenRobbyAndBalloon) + "px", balloonDiv.style.left = o + "px") : (balloonDiv.style.left = layerHorizontalArray[layerHorizontalArray.length - 1].offsetLeft + layerHorizontalArray[layerHorizontalArray.length - 1].offsetWidth - .5 * (containerDiv.offsetWidth + balloonDiv.offsetWidth) + "px",
robbyContainerDiv.style.left = "50%")
Explanation:
The second code contains nested conditions.
The following: a ? b : c is equals to: if (a) { b; } else { c; }.
I can write a, b, c; in order to execute a, then b and then c ( a; b; c; ).
With those two rules, take that block of code and rewrite it so you could understand it.
BTW, I believe you are looking on minified code.
Hope that helps!
What I'm doing: I'm developing a mobile dictionary app for a number of languages
How I'm doing it: Using ionic framework with combination of some angular and some pure js (imported from a working online dictionary site of the same languages)
The problem: Our search function is an approximate search that uses a Levenstein distance calculator to rank all entries in the dictionary with respect to the query form. When the dictionary has up to 1,500 words, this isn't a problem at all on phones, but when the dictionary has around 10,000 words, there is about a 5-8 second delay before results are shown, despite it being instantaneous on a web browser using "ionic serve". When I run firebug, the javascript that takes the longest time to process are the distance calculations, so my working assumption is that this is where I should start, but I'm open to any suggestions at all.
Here's the distance calculator:
/**
* editDistance.js
*
* A simple Levenshtein distance calculator, except weighted such
* that insertions at the beginning and deletions at the end cost less.
*
* AUTHOR: Pat Littell
* LAST UPDATED: 2015-05-16
*/
var distanceCalculator = {
insertionCost : 1.0,
deletionCost : 1.0,
insertionAtBeginningCost : 0.11,
deletionAtEndCost : 0.1,
substitutionCost : 1.0,
getEditDistance : function(a, b) {
if(a.length === 0) return b.length;
if(b.length === 0) return a.length;
var matrix = [];
// var currentInsertionCost, currentDeletionCost, currentSubstitutionCost = 0;
// increment along the first column of each row
var i;
for(i = 0; i <= b.length; i++){
matrix[i] = [i * this.insertionAtBeginningCost];
}
// increment each column in the first row
var j;
for(j = 0; j <= a.length; j++){
matrix[0][j] = j;
}
// Fill in the rest of the matrix
for(i = 1; i <= b.length; i++){
for(j = 1; j <= a.length; j++){
currentInsertionCost = matrix[i][j-1] + this.insertionCost;
currentSubstitutionCost = matrix[i-1][j-1] + (b.charAt(i-1) != a.charAt(j-1) ? this.substitutionCost : 0);
currentDeletionCost = matrix[i-1][j] + (j==a.length ? this.deletionAtEndCost : this.deletionCost);
matrix[i][j] = Math.min(currentSubstitutionCost, Math.min(currentInsertionCost, currentDeletionCost));
}
}
return matrix[b.length][a.length];
},
// Given a query <a> and a series of targets <bs>, return the least distance to any target
getLeastEditDistance : function(a, bs) {
var that = this;
return Math.min.apply(null, bs.map(function(b) {
return that.getEditDistance(a,b);
}));
}
}
First of all, if you have a known dictionary you will get the fastest solution with something like a Levenshtein Automata, which will solve this in linear time to get all candidates. You can't beat this with a general purpose implementation.
With that said, this implementation of levenshtein distance is a few times faster than yours.
function distance(s, t) {
if (s === t) {
return 0;
}
var n = s.length, m = t.length;
if (n === 0 || m === 0) {
return n + m;
}
var x = 0, y, py, a, b, c, d, e, f, k;
var p = new Array(n);
for (y = 0; y < n;) {
p[y] = ++y;
}
for (; (x + 3) < m; x += 4) {
var tx0 = t.charCodeAt(x);
var tx1 = t.charCodeAt(x + 1);
var tx2 = t.charCodeAt(x + 2);
var tx3 = t.charCodeAt(x + 3);
a = x;
b = x + 1;
c = x + 2;
d = x + 3;
e = x + 4;
for (y = 0; y < n; y++) {
k = s.charCodeAt(y);
py = p[y];
if (py < a || b < a) {
a = (py > b ? b + 1 : py + 1);
}
else {
if (tx0 !== k) {
a++;
}
}
if (a < b || c < b) {
b = (a > c ? c + 1 : a + 1);
}
else {
if (tx1 !== k) {
b++;
}
}
if (b < c || d < c) {
c = (b > d ? d + 1 : b + 1);
}
else {
if (tx2 !== k) {
c++;
}
}
if (c < d || e < d) {
d = (c > e ? e + 1 : c + 1);
}
else {
if (tx3 !== k) {
d++;
}
}
p[y] = e = d;
d = c;
c = b;
b = a;
a = py;
}
}
for (; x < m;) {
tx0 = t.charCodeAt(x);
a = x;
b = ++x;
for (y = 0; y < n; y++) {
py = p[y];
if (py < a || b < a) {
b = (py > b ? b + 1 : py + 1);
}
else {
if (tx0 !== s.charCodeAt(y)) {
b = a + 1;
}
else {
b = a;
}
}
p[y] = b;
a = py;
}
f = b;
}
return f;
}
I would also not use map in getLeastEditDistance, it is very slow. Just use a normal loop. Also Math.min with many arguments is not very performant.
I am working with Levenstein distances by my self and I have not found a good way to improve performance and will not recommend using it in a non-batch application.
I suggest you use another approach by using a search tree. A binary or ternary search tree can also find near match.
A good place to start is those articles:
http://www.codeproject.com/Articles/5819/Ternary-Search-Tree-Dictionary-in-C-Faster-String
or
http://www.codeproject.com/Articles/68500/Balanced-Binary-Search-Tree-BST-Search-Delete-InOr
The code is relatively simple sp you should not use much time to port it to JavaScript.
I wanted to write a method to escape special chars like 'ä' to their responding Unicode (e.g. \u00e4).
For some reason JS finds it amusing to not even save the 'ä' internally but use 'üÜ' or some other garble, so when I convert it spits out '\u00c3\u00b6\u00c3\u002013' because it converts these chars instead of 'ä'.
I have tried setting the HTML file's encoding to utf-8 and tried loading the scripts with charset="UTF-8" to no avail. The code doesn't really do anything special but here it is:
String.prototype.replaceWithUtf8 = function() {
var str_newString = '';
var str_procString = this;
for (var i = 0; i < str_procString.length; i++) {
if (str_procString.charCodeAt(i) > 126) {
var hex_uniCode = '\\u00' + str_procString.charCodeAt(i).toString(16);
console.log(hex_uniCode + " (" + str_procString.charAt(i) + ")");
str_newString += hex_uniCode;
} else {
str_newString += str_procString.charAt(i);
}
}
return str_newString;
}
var str_item = "Lärm, Lichter, Lücken, Löcher."
console.log(str_item); // Lärm, Lichter, Lücken, Löcher.
console.log(str_item.replaceWithUtf8()); //L\u00c3\u00a4rm, Lichter, L\u00c3\u00bccken, L\u00c3\u00b6cher.
I have no idea how or why but I just restarted the server again and now it's displaying correctly. To follow up; here's the code for everyone who's interested:
String.prototype.replaceWithUtf8 = function() {
var str_newString = '';
var str_procString = this;
var arr_replace = new Array('/', '"');
var arr_replaceWith = new Array('\\/', '\\"');
for (var i = 0; i < str_procString.length; i++) {
var int_charCode = str_procString.charCodeAt(i);
var cha_charAt = str_procString.charAt(i);
var int_chrIndex = arr_replace.indexOf(cha_charAt);
if (int_chrIndex > -1) {
console.log(arr_replaceWith[int_chrIndex]);
str_newString += arr_replaceWith[int_chrIndex];
} else {
if (int_charCode > 126 && int_charCode < 65536) {
var hex_uniCode = '\\u' + ("000" + int_charCode.toString(16)).substr(-4);
console.log(hex_uniCode + " (" + cha_charAt + ")");
str_newString += hex_uniCode;
} else {
str_newString += cha_charAt;
}
}
}
return str_newString;
}
Use '\\u' + ('000' + str_procString.charCodeAt(i).toString(16) ).stubstr(-4); instead to get the right escape sequences - yours do always start with 00. Also, instead of a for-loop processing your string, .replace() might be faster.
On your question:
console.log("Lärm, Lichter, Lücken, Löcher."); // Lärm, Lichter, Lücken, Löcher.
does not sound as you really sent the file with the right encoding. Might be a server problem, too, if it is correctly saved already.
String.prototype.replaceWithUtf8 = function() {
function r(r) {
for (var t, n, e = "", i = 0; !isNaN(t = r.charCodeAt(i++)); ) n = t.toString(16),
e += 256 > t ? "\\x" + (t > 15 ? "" :"0") + n :"\\u" + ("0000" + n).slice(-4);
return e;
}
var a, c, o, u, s, e = "", i = this, t = [ "/", '"' ], n = [ "\\/", '\\"' ];
for (a = 0; a < i.length; a++) c = i.charCodeAt(a), o = i.charAt(a), u = t.indexOf(o),
u > -1 ? e += n[u] :c > 126 && 65536 > c ? (s = r(o), e += s) :e += o;
return e;
};
prompt("Your escaped string:","Lärm, Lichter, Lücken, Löcher.".replaceWithUtf8());
alert("L\xe4rm, Lichter, L\xfccken, L\xf6cher.");
Unicode encoding only makes every character 6 digits. But for characters above 127 to 256, we can actually make these hexdecimal with less bytes (4 digits per character).