|
5 | 5 | var HUB = "https://causalinferencelab.github.io/"; |
6 | 6 | var BOOK_PATH = "/Causal-Inference-with-Python/"; |
7 | 7 | var NB = "https://github.com/CausalInferenceLab/Causal-Inference-with-Python/blob/main/causal-inference-for-the-brave-and-true/"; |
8 | | - var KEY_READ = "cil-read", KEY_LAST = "cil-last", KEY_VOTE = "cil-vote"; |
| 8 | + // 같은 도메인의 허브와 저장소를 함께 쓰므로 웹북 기록은 cil-book-* 이름을 쓴다. 허브는 이 기록으로 진도와 여권 스탬프를 보여 준다. |
| 9 | + var KEY_READ = "cil-book-read", KEY_LAST = "cil-book-last", KEY_VOTE = "cil-book-vote"; |
| 10 | + var HUB_PAGES = /^\/(?:$|index\.html$|article\.html$|game\.html$|lab\.html$|404\.html$|articles\/|builders\/|badges\/)/; |
9 | 11 |
|
10 | 12 | function get(k, d) { try { var v = localStorage.getItem(k); return v ? JSON.parse(v) : d; } catch (e) { return d; } } |
11 | 13 | function put(k, v) { try { localStorage.setItem(k, JSON.stringify(v)); } catch (e) {} } |
|
27 | 29 | var u; |
28 | 30 | try { u = new URL(a.href, location.href); } catch (err) { return; } |
29 | 31 | if (u.origin !== new URL(HUB).origin || u.pathname.indexOf(BOOK_PATH) === 0) return; |
30 | | - var site = u.pathname.split("/")[1]; |
31 | | - site = site && !/\.html$/.test(site) ? site : "hub"; |
| 32 | + var site = HUB_PAGES.test(u.pathname) ? "hub" : u.pathname.split("/")[1]; |
32 | 33 | var where = placement(a); |
33 | 34 | if (!u.searchParams.has("utm_source")) { |
34 | 35 | u.searchParams.set("utm_source", "webbook"); |
|
112 | 113 |
|
113 | 114 | if (!main || idx < 0 || document.querySelector(".hero")) return; |
114 | 115 | var ch = chapters[idx]; |
115 | | - put(KEY_LAST, { slug: ch.slug, t: Date.now() }); |
| 116 | + put(KEY_LAST, { slug: ch.slug, label: label(ch), total: chapters.length, t: Date.now() }); |
116 | 117 |
|
117 | 118 | // ---------- 장 첫머리: 분량, 위치, 노트북 ---------- |
118 | 119 | var header = document.getElementById("title-block-header"); |
|
172 | 173 | end.className = "cil-end"; |
173 | 174 | end.setAttribute("aria-label", "이 장을 마치며"); |
174 | 175 | end.innerHTML = |
| 176 | + '<p class="cil-end-stamp" hidden>처음으로 한 장을 끝까지 읽었습니다. 허브 활동 여권에 <b>웹북 독자</b> 스탬프가 찍혔어요. <a href="' + HUB + '#passport">여권 보기</a></p>' + |
175 | 177 | '<div class="cil-end-row">' + |
176 | 178 | '<div class="cil-end-vote" role="group" aria-label="이 장 평가">' + |
177 | 179 | '<span class="q">이 장, 도움이 됐나요?</span>' + |
|
221 | 223 | if (!b) return; |
222 | 224 | var v = b.getAttribute("data-vote"); |
223 | 225 | if (votes[ch.slug] === v) return; |
| 226 | + votes = get(KEY_VOTE, {}); |
224 | 227 | votes[ch.slug] = v; |
225 | 228 | put(KEY_VOTE, votes); |
226 | 229 | paintVote(v); |
|
230 | 233 | // 끝까지 내려왔고 30초 이상 머물렀으면 읽은 장으로 기록 |
231 | 234 | var start = Date.now(), timer = 0; |
232 | 235 | function complete() { |
| 236 | + read = get(KEY_READ, {}); // 다른 탭에서 읽은 장을 덮어쓰지 않도록 다시 읽는다 |
233 | 237 | if (read[ch.slug]) return; |
234 | 238 | read[ch.slug] = Date.now(); |
235 | 239 | put(KEY_READ, read); |
236 | 240 | paintSidebar(); |
237 | 241 | paintProgress(); |
238 | | - track("chapter_complete", { chapter: ch.slug, chapter_index: idx + 1, read_count: readCount() }); |
| 242 | + var n = readCount(); |
| 243 | + if (n === 1) end.querySelector(".cil-end-stamp").hidden = false; |
| 244 | + track("chapter_complete", { chapter: ch.slug, chapter_index: idx + 1, read_count: n }); |
239 | 245 | } |
240 | 246 | if ("IntersectionObserver" in window && !read[ch.slug]) { |
241 | 247 | var io = new IntersectionObserver(function (entries) { |
|
0 commit comments