Fix Broken Zillow Links on Reddit

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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 });
})();