|
72 | 72 | return value; |
73 | 73 | }, |
74 | 74 | }); |
75 | | - // Defensive: if a result link is absolute and points off-site, rewrite it to this host |
76 | | - $("#results-container").on("click", "a", function (e) { |
77 | | - var href = $(this).attr("href"); |
78 | | - if (!href) return; |
79 | | - // Only act on absolute URLs |
80 | | - if (/^https?:\/\//i.test(href)) { |
| 75 | + |
| 76 | + // Helper function to check if a link should be skipped during rewriting |
| 77 | + function shouldSkipLink($link, originalHref) { |
| 78 | + // Skip if already processed, missing href, or special protocol links |
| 79 | + if ($link.data("original-href") || !originalHref) { |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + // Skip special protocol links (anchors, mailto, javascript, data, vbscript) |
| 84 | + var specialProtocols = ["#", "mailto:", "javascript:", "data:", "vbscript:"]; |
| 85 | + return specialProtocols.some(function(protocol) { |
| 86 | + return originalHref.startsWith(protocol); |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + // Function to rewrite search result links for iframe display |
| 91 | + function rewriteSearchResultLinks() { |
| 92 | + if (window.self !== window.top) { |
81 | 93 | try { |
82 | | - var u = new URL(href); |
83 | | - if (u.origin !== window.location.origin) { |
84 | | - e.preventDefault(); |
85 | | - var path = u.pathname; |
86 | | - if (!path.startsWith(basePath)) { |
87 | | - path = basePath.replace(/\/$/, "") + "/" + path.replace(/^\//, ""); |
88 | | - } |
89 | | - window.location.href = path + (u.search || "") + (u.hash || ""); |
| 94 | + var parentOrigin = window.location.ancestorOrigins && window.location.ancestorOrigins.length > 0 |
| 95 | + ? window.location.ancestorOrigins[0] |
| 96 | + : document.referrer ? new URL(document.referrer).origin : null; |
| 97 | + |
| 98 | + if (parentOrigin) { |
| 99 | + $("#results-container a").each(function() { |
| 100 | + var $link = $(this); |
| 101 | + var originalHref = $link.attr("href"); |
| 102 | + |
| 103 | + // Skip if already processed or special links |
| 104 | + if (shouldSkipLink($link, originalHref)) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + // Get the path from the href |
| 109 | + var path = originalHref; |
| 110 | + if (!path.startsWith("/")) { |
| 111 | + path = "/" + path; |
| 112 | + } |
| 113 | + |
| 114 | + // Store original and set to parent origin for hover display |
| 115 | + $link.data("original-href", originalHref); |
| 116 | + $link.attr("href", parentOrigin + path); |
| 117 | + }); |
90 | 118 | } |
91 | 119 | } catch (err) { |
92 | | - // ignore malformed URLs |
| 120 | + console.warn("Could not rewrite search result links:", err); |
93 | 121 | } |
94 | 122 | } |
95 | | - }); |
| 123 | + } |
| 124 | + |
| 125 | + // Function to rewrite navigation links for iframe display |
| 126 | + function rewriteNavigationLinks() { |
| 127 | + if (window.self !== window.top) { |
| 128 | + try { |
| 129 | + var parentOrigin = window.location.ancestorOrigins && window.location.ancestorOrigins.length > 0 |
| 130 | + ? window.location.ancestorOrigins[0] |
| 131 | + : document.referrer ? new URL(document.referrer).origin : null; |
| 132 | + |
| 133 | + if (parentOrigin) { |
| 134 | + // Find all navigation links (nav a elements) |
| 135 | + $("nav a").each(function() { |
| 136 | + var $link = $(this); |
| 137 | + var originalHref = $link.attr("href"); |
| 138 | + |
| 139 | + // Skip if already processed or special links |
| 140 | + if (shouldSkipLink($link, originalHref)) { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + // Extract path from href (handle both absolute and relative URLs) |
| 145 | + var path = originalHref; |
| 146 | + try { |
| 147 | + // If it's an absolute URL, extract just the pathname |
| 148 | + if (path.startsWith("http://") || path.startsWith("https://")) { |
| 149 | + var url = new URL(path); |
| 150 | + path = url.pathname + (url.search || "") + (url.hash || ""); |
| 151 | + } |
| 152 | + } catch (e) { |
| 153 | + // If URL parsing fails, treat as relative path |
| 154 | + console.warn("Could not parse URL:", path, e); |
| 155 | + } |
| 156 | + |
| 157 | + // Ensure path starts with / |
| 158 | + if (!path.startsWith("/")) { |
| 159 | + path = "/" + path; |
| 160 | + } |
| 161 | + |
| 162 | + // Store original and set to parent origin for hover display |
| 163 | + $link.data("original-href", originalHref); |
| 164 | + $link.attr("href", parentOrigin + path); |
| 165 | + }); |
| 166 | + } |
| 167 | + } catch (err) { |
| 168 | + console.warn("Could not rewrite navigation links:", err); |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // Use MutationObserver to rewrite search result links when they're added |
| 174 | + if (window.self !== window.top) { |
| 175 | + var resultsContainer = document.getElementById("results-container"); |
| 176 | + if (resultsContainer) { |
| 177 | + var observer = new MutationObserver(function(mutations) { |
| 178 | + rewriteSearchResultLinks(); |
| 179 | + }); |
| 180 | + observer.observe(resultsContainer, { |
| 181 | + childList: true, |
| 182 | + subtree: true |
| 183 | + }); |
| 184 | + } |
| 185 | + |
| 186 | + // Rewrite navigation links when page loads (nav is static HTML, so no need for MutationObserver) |
| 187 | + $(document).ready(function() { |
| 188 | + rewriteNavigationLinks(); |
| 189 | + }); |
| 190 | + } |
96 | 191 | })(); |
0 commit comments