Write a userscript that fixes all problems like this: Przygody Timmy'ego: JEDNO…
- User
Write a userscript that fixes all problems like this:
Przygody Timmy'ego: JEDNO ŻYCZENIE ZA DALEKO/TYCI TIMMY - Ep. 4
- Assistant
Write a userscript that fixes all problems like this:
Przygody Timmy'ego: JEDNO ŻYCZENIE ZA DALEKO/TYCI TIMMY - Ep. 4
To fix misencoded characters like "Ż" in words such as "ŻYCZENIE", you can write a userscript that re-encodes the text correctly. The issue arises when UTF-8 encoded text is misinterpreted as ISO-8859-1 (Latin-1), leading to garbled characters.
Below is a userscript that traverses all text nodes in a webpage and fixes the encoding by re-decoding the text from the incorrect encoding to the correct one.
// ==UserScript==
// @name Fix Misencoded Characters
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Fix misencoded characters on webpages caused by encoding issues
// @author Your Name
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to fix the encoding of a misencoded string
function fixEncoding(str) {
// Convert the misencoded string back to bytes
const bytes = [];
for (let i = 0; i < str.length; i++) {
bytes.push(str.charCodeAt(i));
}
// Decode the bytes using the correct encoding (UTF-8)
const decoder = new TextDecoder('utf-8');
return decoder.decode(new Uint8Array(bytes));
}
// Walk through the document's text nodes
function walk(node) {
let child = node.firstChild;
while (child) {
if (child.nodeType === 3) { // Text node
const originalText = child.nodeValue;
const fixedText = fixEncoding(originalText);
if (originalText !== fixedText) {
child.nodeValue = fixedText;
}
} else if (child.nodeType === 1) { // Element node
walk(child);
}
child = child.nextSibling;
}
}
// Start the process from the body of the document
walk(document.body);
})();
Instructions:
Install a Userscript Manager: Make sure you have a userscript manager installed in your browser, such as Tampermonkey or Greasemonkey.
Create a New Userscript: Open your userscript manager dashboard and create a new userscript. Replace any existing template code with the script provided above.
Save the Script: Save the userscript. It should now be active and running on all websites due to the @match *://*/* metadata tag.
How It Works:
fixEncoding Function: The fixEncoding function takes a string that has been misencoded. It converts each character in the string back to its original byte value and then decodes it using TextDecoder with UTF-8 encoding.
walk Function: The walk function recursively traverses all nodes in the document's body. For each text node, it attempts to fix the encoding.
Text Nodes: Only text nodes (nodeType === 3) are processed to avoid altering the structure of the webpage.
Example:
With the userscript active, the misencoded text:
Przygody Timmy'ego: JEDNO ŻYCZENIE ZA DALEKO/TYCI TIMMY - Ep. 4
Will be corrected to:
Przygody Timmy'ego: JEDNO ŻYCZENIE ZA DALEKO/TYCI TIMMY - Ep. 4
Note: This script assumes that the misencoding is consistent across the page and that misencoded characters are due to UTF-8 text being misread as ISO-8859-1. If other types of encoding issues are present, additional handling may be required.
Not yet included in a published release.