Fix Broken Zillow Links on Reddit

Fixes broken Zillow links on Reddit caused by backslashes (e.g. \_zpid)

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Fix Broken Zillow Links on Reddit
// @namespace    https://greasyfork.org/users/livejamie
// @version      1.0
// @description  Fixes broken Zillow links on Reddit caused by backslashes (e.g. \_zpid)
// @author       livejamie
// @license      MIT
// @match        https://*.reddit.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function fixZillowLinks() {
        // Fix <a> tags with backslashes in href or text
        document.querySelectorAll('a[href*="zillow.com"]').forEach(a => {
            if (a.href.includes('\\_zpid')) {
                a.href = a.href.replace(/\\_zpid/, '_zpid');
            }
            if (a.textContent.includes('\\_zpid')) {
                a.textContent = a.textContent.replace(/\\_zpid/, '_zpid');
            }
        });

        // Convert broken plain text Zillow URLs to real links
        const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false);
        let node;
        while ((node = walker.nextNode())) {
            if (node.nodeValue.includes('zillow.com') && node.nodeValue.includes('\\_zpid')) {
                const fixedText = node.nodeValue.replace(/\\_zpid/g, '_zpid');
                const parent = node.parentNode;
                const zillowRegex = /(https:\/\/www\.zillow\.com\/[^\s)]+)/g;
                const span = document.createElement('span');
                span.innerHTML = fixedText.replace(zillowRegex, (match) => {
                    const clean = match.replace(/\\_zpid/g, '_zpid');
                    return `<a href="${clean}" target="_blank">${clean}</a>`;
                });
                parent.replaceChild(span, node);
            }
        }
    }

    fixZillowLinks();
    const observer = new MutationObserver(fixZillowLinks);
    observer.observe(document.body, { childList: true, subtree: true });
})();