/* 날씨각 위젯 스크립트 — 다른 사이트가 날씨각 링크와 함께 붙이는 작은 로더 (붙이는 코드는 src/lib/embedWidget.ts 의 embedScript). 링크 자리에 카드 iframe(/embed/card)을 끼워 넣는다. iframe 의 주소·크기·style 은 embedCode 가 만드는 iframe 과 같다 (tests/embedLoader.test.mjs 가 둘이 같은지 본다). 값이 틀린 링크는 그대로 두고, 이 파일이 막히거나 안 오면 링크가 남는다. - 주소에 버전이 없어 붙인 사이트가 늘 최신 파일을 받는다(nginx /embed/ 1시간 캐시). 고칠 때 붙인 코드가 바뀌지 않게 한다. - 날씨각 주소(origin)는 이 스크립트의 주소에서 얻는다 (로컬 도커에서도 그 서버의 카드를 붙인다). - 남의 페이지에서 도니까 iframe 끼워 넣기 외에는 하지 않는다: 전역 이름·쿠키·저장소·추가 요청·콘솔 출력 없음. - 옛 브라우저에서도 돌게 ES5 로만 쓴다. 화면에 가까워질 때 불러오기는 iframe 의 loading="lazy" 에 맡긴다. */ (function () { "use strict"; var doc = document; // 모양별 [기본 높이, 최대 높이, 최소 폭, 최대 폭] (px), 폭:높이 비율 — embedWidget.ts 의 EMBED_HEIGHT·EMBED_MAX_HEIGHT·EMBED_MIN_WIDTH·EMBED_MAX_WIDTH, EMBED_RATIO var SIZE = { side: [245, 320, 220, 440, "11/8"], home: [400, 520, 280, 720, "18/13"] }; var BGS = { sky: 1, light: 1, dark: 1 }; var own = Object.prototype.hasOwnProperty; // currentScript 는 처음 실행될 때만 있다. 없는 브라우저면 이 파일을 부른 script 태그를 찾는다 var me = doc.currentScript; if (!me) { var found = doc.querySelectorAll('script[src*="/embed/widget.js"]'); me = found[found.length - 1]; } var origin = me && /^https?:\/\/[^/?#]+/i.exec(me.src || ""); if (!origin) return; origin = origin[0]; function put(a) { var region = a.getAttribute("data-region"); var kind = a.getAttribute("data-type") || "side"; var bg = a.getAttribute("data-bg") || "sky"; if (!/^[0-9]{10}$/.test(region || "") || !own.call(SIZE, kind) || !own.call(BGS, bg) || !a.parentNode) return; var s = SIZE[kind]; var title = (a.textContent || "").replace(/\s+/g, " ").replace(/^ | $/g, "") || "날씨각"; var f = doc.createElement("iframe"); f.setAttribute("src", origin + "/embed/card?region=" + encodeURIComponent(region) + "&type=" + kind + "&bg=" + bg); f.setAttribute("title", title); f.setAttribute("width", "100%"); f.setAttribute("height", String(s[0])); f.setAttribute("style", "border:0;width:100%;height:auto;aspect-ratio:" + s[4] + ";min-width:" + s[2] + "px;max-width:" + s[3] + "px;min-height:" + s[0] + "px;max-height:" + s[1] + "px;border-radius:28px"); f.setAttribute("loading", "lazy"); f.setAttribute("referrerpolicy", "origin"); a.parentNode.replaceChild(f, a); } // 끼워 넣은 링크는 문서에서 빠지므로 여러 번 돌아도(스크립트를 두 번 붙임) 카드는 하나다 function place() { var links = doc.querySelectorAll("a.nalssigak-weather[data-region]"); for (var i = 0; i < links.length; i++) put(links[i]); } place(); // 문서가 다 읽히기 전에 돌았으면 스크립트보다 아래에 있는 링크를 위해 다 읽힌 뒤 한 번 더 if (doc.readyState === "loading") doc.addEventListener("DOMContentLoaded", place); })();