To remove HTML URL encoding (percent-encoding) for param2 in your JavaScript code, you need to decode the URL-encoded string. This ensures that param2 contains the actual characters rather than their encoded representations.
JavaScript provides the decodeURIComponent function, which decodes a portion of a URI by replacing each escape sequence in the encoded URI component with the character that it represents. However, it's important to note that decodeURIComponent assumes the encoding is UTF-8. If your URL parameters are encoded using Windows-1252, additional handling is required, as JavaScript doesn't natively support decoding Windows-1252 encoded strings.
Below, I'll guide you through:
- Decoding
param2 assuming UTF-8 encoding.
- Handling Windows-1252 encoded URLs if necessary.
1. Decoding param2 with decodeURIComponent (UTF-8)
If your URL parameters are encoded using UTF-8 (which is standard for web URLs), you can simply use decodeURIComponent to decode param2. Here's how you can modify your code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Striker In The Crypt Of Trojan</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
let ie = 0;
let pb = [];
let param2 = pbf = pbt = frc = game = t = u = 0;
function cb(b, c) {
if (c === 1)
emul = b;
else
game = b;
if (c === 3)
document.write(game);
else if (!t--)
this.eval(emul.substr(311365));
else if (c === 2) {
let k = b.indexOf('\0');
let ci = b.length;
frc = bp = k + 1;
while (bp < ci)
pbf += (ap = b.charCodeAt(--ci)), ap !== 255 && bp++;
while (ci < b.length)
ap = b.charCodeAt(ci++), pb[pbt++] = ap === 255 ? ap : ap | b.charCodeAt(frc++) << 8;
frc = pb[0] & 255;
let param = b.substr(0, k);
let paramParts = param.split('/');
let param1 = paramParts[0];
param2 = paramParts[1] ? decodeURIComponent(paramParts[1]) : ''; // Decoding param2
ajax(
(param1.slice(-1) === 'a' ? 'snaps/' : 'games/') + param1,
0
);
}
}
function ajax(f, g, h) {
let xhr = new XMLHttpRequest();
if (g < 0) {
xhr.onreadystatechange = function() {
if (xhr && xhr.readyState === 4)
rt(ie ? bin2arr(xhr.responseBody) : bin2str(xhr.responseText));
};
} else {
xhr.onreadystatechange = function() {
if (xhr && xhr.readyState === 4)
cb(ie ? bin2arr(xhr.responseBody) : bin2str(xhr.responseText), h);
};
}
xhr.open(g ? 'POST' : 'GET', f, true);
if (!ie)
xhr.overrideMimeType('text/plain;charset=x-user-defined');
xhr.send(g);
}
function bin2str(a) {
return a.replace(/[\s\S]/g, function(t) {
return String.fromCharCode(t.charCodeAt(0) & 255);
});
}
function bin2arr(a) {
return arr(a).replace(/[\s\S]/g, function(t) {
let v = t.charCodeAt(0);
return String.fromCharCode(v & 0xff, v >> 8);
}) + arrl(a);
}
let k = location.href.indexOf('?') + 1;
let ifra = location.href.slice(-1) === '#';
let param1 = 'sp2.dsk';
let fullUrl = window.location.href;
let lastEqualIndex = fullUrl.lastIndexOf('=');
param2 = lastEqualIndex !== -1 ? decodeURIComponent(fullUrl.slice(lastEqualIndex + 1)) : ''; // Decoding param2 from URL
t++;
ajax('' + param1, 0);
ajax('_6128s.rom', 0, 1);
</script>
</body>
</html>
Explanation of Changes:
-
Decoding param2 from the URL:
param2 = lastEqualIndex !== -1 ? decodeURIComponent(fullUrl.slice(lastEqualIndex + 1)) : '';
This line extracts the substring after the last = in the URL and decodes it using decodeURIComponent.
-
Decoding param2 from AJAX Response:
param2 = paramParts[1] ? decodeURIComponent(paramParts[1]) : '';
When processing the AJAX response, if param2 is part of the response, it gets decoded similarly.
2. Handling Windows-1252 Encoded URLs
If your URL parameters are encoded using Windows-1252 instead of UTF-8, decodeURIComponent won't correctly decode characters outside the ASCII range. To handle Windows-1252 encoding, you can use the following approach:
-
Include a Windows-1252 Decoder Library:
JavaScript doesn't have built-in support for Windows-1252 decoding, but you can use a library like iconv-lite in a Node.js environment. However, in the browser, you can implement a simple decoder or use existing code snippets.
-
Implement a Basic Windows-1252 Decoder:
Here's a basic decoder that maps Windows-1252 byte values to Unicode characters:
function decodeWindows1252(str) {
const windows1252Map = [
// 0x80 to 0x9F mapping
'\u20AC','\uFFFD','\u201A','\u0192','\u201E','\u2026','\u2020','\u2021',
'\u02C6','\u2030','\u0160','\u2039','\u0152','\uFFFD','\u017D','\uFFFD',
'\uFFFD','\u2018','\u2019','\u201C','\u201D','\u2022','\u2013','\u2014',
'\u02DC','\u2122','\u0161','\u203A','\u0153','\uFFFD','\u017E','\u0178'
];
return str.replace(/[\x80-\x9F]/g, function(match) {
const code = match.charCodeAt(0) - 0x80;
return windows1252Map[code] || match;
});
}
// Usage example:
let encodedParam2 = lastEqualIndex !== -1 ? fullUrl.slice(lastEqualIndex + 1) : '';
// Decode percent-encoding first
let decodedURIComponent = decodeURIComponent(encodedParam2);
// Then decode Windows-1252 specific characters
param2 = decodeWindows1252(decodedURIComponent);
Explanation:
- The
decodeWindows1252 function replaces Windows-1252 specific byte values (from 0x80 to 0x9F) with their corresponding Unicode characters.
- First, use
decodeURIComponent to handle standard percent-encoding.
- Then, pass the result through
decodeWindows1252 to correctly map Windows-1252 characters.
-
Integrate the Decoder into Your Code:
Modify the assignment of param2 to use the custom decoder:
// After extracting the encoded param2 from the URL
let encodedParam2 = lastEqualIndex !== -1 ? fullUrl.slice(lastEqualIndex + 1) : '';
let decodedURIComponent = decodeURIComponent(encodedParam2);
param2 = decodeWindows1252(decodedURIComponent);
And similarly, if param2 is obtained from an AJAX response or elsewhere, ensure you decode it using the decodeWindows1252 function after applying decodeURIComponent.
Final Modified Code with Windows-1252 Handling
Here's the complete code integrating both decoding steps:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Striker In The Crypt Of Trojan</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
let ie = 0;
let pb = [];
let param2 = pbf = pbt = frc = game = t = u = 0;
function decodeWindows1252(str) {
const windows1252Map = [
'\u20AC','\uFFFD','\u201A','\u0192','\u201E','\u2026','\u2020','\u2021',
'\u02C6','\u2030','\u0160','\u2039','\u0152','\uFFFD','\u017D','\uFFFD',
'\uFFFD','\u2018','\u2019','\u201C','\u201D','\u2022','\u2013','\u2014',
'\u02DC','\u2122','\u0161','\u203A','\u0153','\uFFFD','\u017E','\u0178'
];
return str.replace(/[\x80-\x9F]/g, function(match) {
const code = match.charCodeAt(0) - 0x80;
return windows1252Map[code] || match;
});
}
function cb(b, c) {
if (c === 1)
emul = b;
else
game = b;
if (c === 3)
document.write(game);
else if (!t--)
this.eval(emul.substr(311365));
else if (c === 2) {
let k = b.indexOf('\0');
let ci = b.length;
frc = bp = k + 1;
while (bp < ci)
pbf += (ap = b.charCodeAt(--ci)), ap !== 255 && bp++;
while (ci < b.length)
ap = b.charCodeAt(ci++), pb[pbt++] = ap === 255 ? ap : ap | b.charCodeAt(frc++) << 8;
frc = pb[0] & 255;
let param = b.substr(0, k);
let paramParts = param.split('/');
let param1 = paramParts[0];
if (paramParts[1]) {
// Decode param2 with Windows-1252 handling
let decodedURIComponent = decodeURIComponent(paramParts[1]);
param2 = decodeWindows1252(decodedURIComponent);
} else {
param2 = '';
}
ajax(
(param1.slice(-1) === 'a' ? 'snaps/' : 'games/') + param1,
0
);
}
}
function ajax(f, g, h) {
let xhr = new XMLHttpRequest();
if (g < 0) {
xhr.onreadystatechange = function() {
if (xhr && xhr.readyState === 4)
rt(ie ? bin2arr(xhr.responseBody) : bin2str(xhr.responseText));
};
} else {
xhr.onreadystatechange = function() {
if (xhr && xhr.readyState === 4)
cb(ie ? bin2arr(xhr.responseBody) : bin2str(xhr.responseText), h);
};
}
xhr.open(g ? 'POST' : 'GET', f, true);
if (!ie)
xhr.overrideMimeType('text/plain;charset=x-user-defined');
xhr.send(g);
}
function bin2str(a) {
return a.replace(/[\s\S]/g, function(t) {
return String.fromCharCode(t.charCodeAt(0) & 255);
});
}
function bin2arr(a) {
return arr(a).replace(/[\s\S]/g, function(t) {
let v = t.charCodeAt(0);
return String.fromCharCode(v & 0xff, v >> 8);
}) + arrl(a);
}
let k = location.href.indexOf('?') + 1;
let ifra = location.href.slice(-1) === '#';
let param1 = 'sp2.dsk';
let fullUrl = window.location.href;
let lastEqualIndex = fullUrl.lastIndexOf('=');
if (lastEqualIndex !== -1) {
let encodedParam2 = fullUrl.slice(lastEqualIndex + 1);
let decodedURIComponent = decodeURIComponent(encodedParam2);
param2 = decodeWindows1252(decodedURIComponent);
} else {
param2 = '';
}
t++;
ajax('' + param1, 0);
ajax('_6128s.rom', 0, 1);
</script>
</body>
</html>
Key Points:
- Decoding Order: Always decode percent-encoding (
decodeURIComponent) before handling character encoding like Windows-1252.
- Custom Decoding: Since JavaScript doesn't support Windows-1252 natively, a custom decoder function is necessary to accurately map specific byte values to their Unicode equivalents.
- Error Handling: The
windows1252Map replaces unknown or undefined mappings with the Unicode Replacement Character (\uFFFD), which is a standard way to handle decoding errors.
Additional Recommendations
-
Ensure Consistent Encoding:
- If possible, standardize your application to use UTF-8 encoding for URLs and data transmission. This simplifies decoding and ensures broader compatibility.
-
Use Libraries for Encoding Handling:
- For more comprehensive encoding and decoding needs, consider using well-established libraries such as iconv.js or TextDecoder with the appropriate encoding label, though support for Windows-1252 may vary.
-
Validate Input:
- Always validate and sanitize URL parameters to prevent potential security vulnerabilities such as injection attacks.
-
Browser Compatibility:
- Test your decoding functions across different browsers to ensure consistent behavior, especially if relying on non-standard encoding schemes.
By implementing the above changes, param2 will contain the decoded, human-readable string without the unwanted URL encoding, accommodating both UTF-8 and Windows-1252 encoded URLs.