|
21 | 21 | // Hides every `[data-desktop-only]` element (rule in css/00-base.css). |
22 | 22 | document.documentElement.classList.add("is-web"); |
23 | 23 |
|
| 24 | + // NO LAN FROM THE WEB. Some printer modules talk to the machines straight from |
| 25 | + // the page (Creality ws://<ip>:9999, Snapmaker http://<ip>:7125 — the other |
| 26 | + // brands go through the desktop's main process, absent here), and their |
| 27 | + // background pings start as soon as the printer list loads. From an https page |
| 28 | + // that is mixed content: Chrome flags the whole site "Not secure" and asks for |
| 29 | + // local-network access, for something that can't work anyway. So an insecure |
| 30 | + // WebSocket or fetch is refused right here — the callers already treat a |
| 31 | + // failure as "printer unreachable". (Printers on the web will come through a |
| 32 | + // secure relay, never the LAN — docs/STUDIO-WEB-PLAN.md.) |
| 33 | + if (location.protocol === "https:") { |
| 34 | + const insecure = (u) => { |
| 35 | + try { return /^(ws|http):$/.test(new URL(String(u), location.href).protocol); } catch { return false; } |
| 36 | + }; |
| 37 | + const NativeWS = window.WebSocket; |
| 38 | + window.WebSocket = function (url, protocols) { |
| 39 | + if (insecure(url)) throw new DOMException("LAN connections are desktop-only", "SecurityError"); |
| 40 | + return protocols === undefined ? new NativeWS(url) : new NativeWS(url, protocols); |
| 41 | + }; |
| 42 | + window.WebSocket.prototype = NativeWS.prototype; |
| 43 | + Object.assign(window.WebSocket, { CONNECTING: 0, OPEN: 1, CLOSING: 2, CLOSED: 3 }); |
| 44 | + const nativeFetch = window.fetch.bind(window); |
| 45 | + window.fetch = (input, init) => { |
| 46 | + const url = input instanceof Request ? input.url : input; |
| 47 | + if (insecure(url)) return Promise.reject(new TypeError("LAN connections are desktop-only")); |
| 48 | + return nativeFetch(input, init); |
| 49 | + }; |
| 50 | + } |
| 51 | + |
24 | 52 | // Install as an app (PWA): Chrome / Edge / Android announce it once, possibly |
25 | 53 | // before the renderer module has loaded — keep the event for the sidebar's |
26 | 54 | // "Install the app" button (inventory.js) and tell it the moment it lands. |
|
0 commit comments