|
| 1 | +(function () { |
| 2 | + if (window.__studioNavigationGuardInstalled) { |
| 3 | + return; |
| 4 | + } |
| 5 | + window.__studioNavigationGuardInstalled = true; |
| 6 | + |
| 7 | + var nativeOpen = window.open; |
| 8 | + var allowedSchemes = ["http:", "https:", "mailto:", "tel:"]; |
| 9 | + var studioFlag = "studio"; |
| 10 | + var isStudioPageEnabled = false; |
| 11 | + |
| 12 | + try { |
| 13 | + isStudioPageEnabled = new URL(window.location.href).searchParams.get(studioFlag) === "true"; |
| 14 | + } catch (e) { |
| 15 | + isStudioPageEnabled = false; |
| 16 | + } |
| 17 | + |
| 18 | + function dispatchStudioNavigationBlocked(payload) { |
| 19 | + var eventName = "studio-blocked-navigation"; |
| 20 | + |
| 21 | + try { |
| 22 | + var customEvent = new CustomEvent(eventName, { detail: payload }); |
| 23 | + window.dispatchEvent(customEvent); |
| 24 | + } catch (e) { |
| 25 | + // ignore custom event failures |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + if (window.top && window.top !== window) { |
| 30 | + var topEvent = new CustomEvent(eventName, { detail: payload }); |
| 31 | + window.top.dispatchEvent(topEvent); |
| 32 | + } |
| 33 | + } catch (e) { |
| 34 | + // ignore top window dispatch failures |
| 35 | + } |
| 36 | + |
| 37 | + if (window.parent && window.parent !== window) { |
| 38 | + try { |
| 39 | + window.parent.postMessage( |
| 40 | + { |
| 41 | + type: eventName, |
| 42 | + payload: payload |
| 43 | + }, |
| 44 | + "*" |
| 45 | + ); |
| 46 | + } catch (e) { |
| 47 | + // ignore cross-window failures |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + function bindStudioNavigationBlockedListener() { |
| 53 | + try { |
| 54 | + window.addEventListener("message", function (event) { |
| 55 | + console.log("[studio] raw message:", event.origin, event.data); |
| 56 | + }); |
| 57 | + } catch (e) { |
| 58 | + // ignore listener setup failures |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + window.addEventListener("studio-blocked-navigation", function (event) { |
| 63 | + var payload = event && event.detail ? event.detail : null; |
| 64 | + if (!payload || typeof payload.url !== "string") { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + console.log("[studio] blocked navigation:", payload.source, payload.url); |
| 69 | + }); |
| 70 | + } catch (e) { |
| 71 | + // ignore listener setup failures |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + bindStudioNavigationBlockedListener(); |
| 76 | + |
| 77 | + if (!isStudioPageEnabled) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + function isAllowedUrl(url) { |
| 82 | + if (!url) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + var value = String(url).trim(); |
| 87 | + if (!value) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + if (value.charAt(0) === "#") { |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + if (value.indexOf("javascript:") === 0 || value.indexOf("data:") === 0) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + try { |
| 100 | + var parsed = new URL(value, window.location.href); |
| 101 | + return allowedSchemes.indexOf(parsed.protocol) >= 0 || parsed.origin === window.location.origin; |
| 102 | + } catch (e) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + function shouldBlockAnchor(anchor) { |
| 108 | + if (!anchor || !anchor.getAttribute) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + var href = anchor.getAttribute("href"); |
| 113 | + if (!href) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + if (isAllowedUrl(href) && (anchor.getAttribute("target") || "").toLowerCase() === "_blank") { |
| 118 | + return true; |
| 119 | + } |
| 120 | + |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + function updateAnchorInterception(anchor) { |
| 125 | + if (!anchor || !anchor.getAttribute) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + var href = anchor.getAttribute("href"); |
| 130 | + if (!href) { |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + if (isAllowedUrl(href) && (anchor.getAttribute("target") || "").toLowerCase() === "_blank") { |
| 135 | + anchor.setAttribute("data-studio-block-navigation", "true"); |
| 136 | + } else { |
| 137 | + anchor.removeAttribute("data-studio-block-navigation"); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + function scanAnchors(root) { |
| 142 | + if (!root || !root.querySelectorAll) { |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + var anchors = root.querySelectorAll("a[href]"); |
| 147 | + for (var i = 0; i < anchors.length; i += 1) { |
| 148 | + updateAnchorInterception(anchors[i]); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + function bindAnchorObserver() { |
| 153 | + try { |
| 154 | + scanAnchors(document); |
| 155 | + |
| 156 | + if (!window.MutationObserver) { |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + var observer = new MutationObserver(function (mutations) { |
| 161 | + for (var i = 0; i < mutations.length; i += 1) { |
| 162 | + var mutation = mutations[i]; |
| 163 | + for (var j = 0; j < mutation.addedNodes.length; j += 1) { |
| 164 | + var node = mutation.addedNodes[j]; |
| 165 | + if (!node || node.nodeType !== 1) { |
| 166 | + continue; |
| 167 | + } |
| 168 | + |
| 169 | + if (node.tagName === "A") { |
| 170 | + updateAnchorInterception(node); |
| 171 | + } |
| 172 | + |
| 173 | + scanAnchors(node); |
| 174 | + } |
| 175 | + } |
| 176 | + }); |
| 177 | + |
| 178 | + observer.observe(document.documentElement || document.body, { |
| 179 | + childList: true, |
| 180 | + subtree: true |
| 181 | + }); |
| 182 | + } catch (e) { |
| 183 | + // ignore observer setup failures |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + function handleBlockedNavigation(url, source) { |
| 188 | + var payload = { |
| 189 | + source: source, |
| 190 | + url: url, |
| 191 | + timestamp: Date.now() |
| 192 | + }; |
| 193 | + |
| 194 | + dispatchStudioNavigationBlocked(payload); |
| 195 | + console.log("[studio] blocked navigation:", source, url); |
| 196 | + |
| 197 | + if (typeof window.onStudioNavigationBlocked === "function") { |
| 198 | + window.onStudioNavigationBlocked(url, source); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + window.open = function (url, target, features) { |
| 203 | + if (!isAllowedUrl(url)) { |
| 204 | + return nativeOpen.apply(window, arguments); |
| 205 | + } |
| 206 | + |
| 207 | + if (typeof target === "string" && target.toLowerCase() === "_blank") { |
| 208 | + handleBlockedNavigation(url, "window.open"); |
| 209 | + return null; |
| 210 | + } |
| 211 | + |
| 212 | + return nativeOpen.apply(window, arguments); |
| 213 | + }; |
| 214 | + |
| 215 | + document.addEventListener( |
| 216 | + "click", |
| 217 | + function (event) { |
| 218 | + var node = event.target; |
| 219 | + while (node && node !== document) { |
| 220 | + if (node.tagName === "A") { |
| 221 | + if (shouldBlockAnchor(node)) { |
| 222 | + event.preventDefault(); |
| 223 | + event.stopPropagation(); |
| 224 | + handleBlockedNavigation(node.href || node.getAttribute("href"), "anchor-click"); |
| 225 | + } |
| 226 | + return; |
| 227 | + } |
| 228 | + node = node.parentNode; |
| 229 | + } |
| 230 | + }, |
| 231 | + true |
| 232 | + ); |
| 233 | + |
| 234 | + document.addEventListener( |
| 235 | + "submit", |
| 236 | + function (event) { |
| 237 | + var form = event.target; |
| 238 | + if (!form || form.tagName !== "FORM") { |
| 239 | + return; |
| 240 | + } |
| 241 | + |
| 242 | + var action = form.getAttribute("action") || window.location.href; |
| 243 | + if (!isAllowedUrl(action)) { |
| 244 | + return; |
| 245 | + } |
| 246 | + |
| 247 | + if ((action || "").indexOf("#") === 0) { |
| 248 | + return; |
| 249 | + } |
| 250 | + |
| 251 | + if (new URL(action, window.location.href).origin !== window.location.origin) { |
| 252 | + return; |
| 253 | + } |
| 254 | + |
| 255 | + if ((window.location.search || "").indexOf(studioFlag + "=true") >= 0) { |
| 256 | + event.preventDefault(); |
| 257 | + event.stopPropagation(); |
| 258 | + handleBlockedNavigation(action, "form-submit"); |
| 259 | + } |
| 260 | + }, |
| 261 | + true |
| 262 | + ); |
| 263 | + |
| 264 | + bindStudioNavigationBlockedListener(); |
| 265 | + bindAnchorObserver(); |
| 266 | +})(); |
0 commit comments