Extremely enhance this prompt idea: Write a stable and working Tampermonkey script that fixes all problems like this:
Przygody Timmy'ego: JEDNO ŻYCZENIE ZA DALEKO/TYCI TIMMY - Ep. 4
Assistant
Enhanced Prompt:
Objective:
Develop a stable and efficient Tampermonkey script that automatically detects and corrects misencoded text on web pages caused by incorrect character encoding interpretations. This issue often manifests when UTF-8 encoded text is incorrectly displayed using ISO-8859-1 (Latin-1) or Windows-1252 encoding, resulting in garbled characters. The script should process all text nodes on the page, correct any misencoded characters, and update the displayed text seamlessly.
Example of the Issue:
Garbled Text:Przygody Timmy'ego: JEDNO ŻYCZENIE ZA DALEKO/TYCI TIMMY - Ep. 4
Corrected Text:Przygody Timmy'ego: JEDNO ŻYCZENIE ZA DALEKO/TYCI TIMMY - Ep. 4
Requirements:
Automatic Detection and Correction:
The script should scan all text nodes within the webpage's DOM.
Identify text that contains misencoded characters resulting from incorrect character encoding interpretations.
Correct the text so that it displays the proper characters intended in the original UTF-8 encoding.
Performance Optimization:
The script must be efficient, minimizing performance overhead to ensure it does not slow down page loading or interfere with user interactions.
Utilize best practices to avoid excessive DOM manipulation or resource-intensive operations.
Compatibility:
Ensure compatibility with modern browsers (e.g., Chrome, Firefox, Edge) where Tampermonkey is supported.
The script should function correctly across different websites and with various web technologies (including SPAs - Single Page Applications).
User Configuration (Optional but Preferred):
Allow users to specify certain domains or URLs where the script should or shouldn't run.
Provide an option to enable or disable the script without uninstalling it.
Documentation:
Include detailed comments within the code to explain how each part of the script works.
Provide instructions on how to install the script in Tampermonkey.
Offer guidance on troubleshooting common issues that might arise during installation or execution.
Deliverables:
A fully functional Tampermonkey script that meets the above requirements.
Inline code comments explaining the logic and functionality.
Installation and usage instructions.
Additional Context:
The problem arises due to a mismatch in character encoding. For example, UTF-8 encoded text is sometimes wrongly interpreted and displayed using ISO-8859-1 or Windows-1252 encoding, leading to characters like Å» instead of the correct Ż in Polish text. This script aims to enhance user experience by correcting such errors on-the-fly, ensuring that text is displayed as intended.
Example of the Tampermonkey Script:
Below is an example of how the script might look. This script utilizes the concept of re-decoding misencoded text.
javascript
// ==UserScript==
// @name Fix Misencoded Characters
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Automatically fixes misencoded text on web pages.
// @author Your Name
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to fix misencoded text
function fixTextNode(node) {
let originalText = node.nodeValue;
let fixedText = decodeText(originalText);
if (originalText !== fixedText) {
node.nodeValue = fixedText;
}
}
// Function that attempts to decode misencoded text
function decodeText(text) {
// Create a byte array from the text assuming it's misencoded
let bytes = [];
for (let i = 0; i < text.length; ++i) {
bytes.push(text.charCodeAt(i) & 0xFF);
}
try {
// Decode the bytes using the correct encoding (UTF-8)
let decoder = new TextDecoder('utf-8');
let decodedText = decoder.decode(new Uint8Array(bytes));
return decodedText;
} catch (e) {
// If decoding fails, return the original text
return text;
}
}
// Function to walk through the DOM and fix text nodes
function walk(node) {
let child, next;
switch (node.nodeType) {
case 1: // Element
case 9: // Document
case 11: // Document fragment
child = node.firstChild;
while (child) {
next = child.nextSibling;
walk(child);
child = next;
}
break;
case 3: // Text node
fixTextNode(node);
break;
}
}
// Run the script on page load
walk(document.body);
// MutationObserver to handle dynamically added content
const observer = new MutationObserver(function(mutations) {
for (let mutation of mutations) {
for (let node of mutation.addedNodes) {
walk(node);
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
})();
Instructions for Installation and Use:
Install Tampermonkey Extension:
If you haven't already, install Tampermonkey for your browser:
Click on the Tampermonkey extension icon in your browser.
Select Create a new script from the dropdown menu.
Paste the Script:
Remove any content in the editor that appears by default.
Copy and paste the script provided above into the Tampermonkey editor.
Save the Script:
Click on File > Save, or use the keyboard shortcut (usually Ctrl+S or Cmd+S).
Refresh the Webpage:
Navigate to a webpage where you have encountered misencoded text issues.
Refresh the page to allow the script to run and fix the text.
Verify the Fix:
Check the text on the page to ensure that the misencoded characters have been corrected.
Troubleshooting:
Script Not Running:
Ensure that the script's @match parameter includes the website you're visiting.
Confirm that Tampermonkey is enabled and the script is active (the script name should appear in the Tampermonkey dashboard with a green icon).
Performance Issues:
The script is designed to be efficient, but if you notice performance degradation on complex web pages, consider modifying the script to exclude certain sites by adjusting the @match or adding @exclude directives.
Dynamic Content Not Fixed:
The script uses a MutationObserver to monitor changes to the DOM and fix newly added text nodes. If some dynamic content is still not being corrected, ensure that the observer is properly configured.
Notes:
This script assumes that the misencoding is consistent across the text nodes. It may not work perfectly in all cases due to the complexities of character encoding issues.
Always ensure you comply with website terms of service when running user scripts.
Customize the script as needed for specific websites or use cases.
By following this enhanced prompt and utilizing the provided script as a starting point, you should be able to create a robust solution to fix misencoded text issues on web pages, improving readability and user experience.