/* subject-actions.v1.js Popup for .subject-pill anchors with: - Search in Insight Book (only if subject exists in Insight index) - Search in WOL (uses current href if present) - Search in Database (only for 1–2 word subjects) - Search on Google - Search on Wikipedia Insight index: - The script merges a small built-in seed with (optional) /static/data/insight-index.v1.json - JSON format: { "abraham": "1200000060", "babel": "1200000613", ... } */ (function(){ // ---- Config var WOL_LANG = 'en'; var WOL_LP = 'e'; // Where to fetch your full Insight index (optional, but recommended) var INSIGHT_INDEX_URL = '/static/data/insight-index.v1.json'; // ---- Helpers function norm(s){ return String(s||'').toLowerCase().replace(/[^\p{L}\p{N}\s'-]/gu,'').replace(/\s+/g,' ').trim(); } function isOneOrTwoWords(s){ return norm(s).split(' ').filter(Boolean).length <= 2; } function getSearchBase(){ var el = document.getElementById('subject-list'); return el ? el.getAttribute('data-search-url') || '/search' : '/search'; } function wolSearchURL(q){ return "https://wol.jw.org/"+WOL_LANG+"/wol/s/r1/lp-"+WOL_LP+"?q="+encodeURIComponent(q); } function insightURL(id){ // canonical article path return "https://wol.jw.org/"+WOL_LANG+"/wol/d/r1/lp-"+WOL_LP+"/"+id; } function wikipediaURL(q){ return "https://en.wikipedia.org/wiki/Special:Search?search="+encodeURIComponent(q); } function googleURL(q){ return "https://www.google.com/search?q="+encodeURIComponent(q); } function databaseURL(q){ // Searches your DB limiting to subject field (adjust query key as needed) return getSearchBase()+"?q="+encodeURIComponent(q)+"&fields=subject"; } // ---- Built-in tiny seed (~20 common, verified entries). External JSON will merge/override. var INSIGHT_INDEX = { "abraham": "1200000060", "adam": "1200000089", "baptism": "1200000555", "david": "1200001130", "faith": "1200001484", "forgiveness": "1200001554", "jerusalem": "1200002436", "jesus christ": "1200002451", "kingdom of god": "1200002615", "love": "1200002781", "moses": "1200003118", "noah": "1200003266", "paul": "1200003406", "peter": "1200003451", "prayer": "1200003568", "resurrection": "1200003709", "satan": "1200003845", "marriage": "1200002912", "mercy": "1200002994", "wisdom": "1200004618" }; // Try to extend from external JSON (if present). Safe to fail silently. (function loadExternalIndex(){ try{ var xhr = new XMLHttpRequest(); xhr.open('GET', INSIGHT_INDEX_URL, true); xhr.onreadystatechange = function(){ if (xhr.readyState !== 4) return; if (xhr.status >= 200 && xhr.status < 300) { try { var data = JSON.parse(xhr.responseText || '{}'); if (data && typeof data === 'object'){ for (var k in data){ if (Object.prototype.hasOwnProperty.call(data,k)) INSIGHT_INDEX[norm(k)] = String(data[k]); } } } catch(_){} } }; xhr.send(); } catch(_){} })(); // ---- Popup (injects minimal CSS + HTML with icons) var popupEl; function ensurePopup(){ if (popupEl) return popupEl; // Minimal CSS (safe if injected twice) var style = document.createElement('style'); style.textContent = ".subject-actions-pop{position:absolute;z-index:9999;background:#fff;border:1px solid #e5e7eb;border-radius:.5rem;box-shadow:0 10px 20px rgba(0,0,0,.1);padding:.5rem;min-width:240px;display:none}" + ".subject-actions-pop .hdr{display:flex;justify-content:space-between;align-items:center;font-weight:600;font-size:.9rem;color:#1f2937;margin-bottom:.25rem}" + ".subject-actions-pop .close-x{cursor:pointer;padding:.1rem .4rem;border-radius:.375rem}" + ".subject-actions-pop .close-x:hover{background:#f3f4f6}" + ".subject-actions-pop .btn-row{display:grid;gap:.4rem;margin-top:.25rem}" + ".subject-actions-pop a.action{display:flex;align-items:center;gap:6px;text-decoration:none;padding:.45rem .55rem;border:1px solid #e5e7eb;border-radius:.45rem;color:#1f2937}" + ".subject-actions-pop a.action:hover{background:#f9fafb}" + ".subject-actions-pop a.action[hidden]{display:none}" + ".subject-actions-pop a.action svg{flex:0 0 auto}"; document.head.appendChild(style); popupEl = document.createElement('div'); popupEl.className = 'subject-actions-pop'; // Order: Insight, WOL, Database, Google, Wikipedia — with inline SVG icons popupEl.innerHTML = '
Research Subject
' + '
' + // Insight Book (green book) '' + '' + 'Search in Insight Book' + '' + // WOL (watchtower-ish emblem) '' + '' + 'Search in WOL' + '' + // Database (stacked cylinders) '' + '' + 'Search in Database' + '' + // Google (colored G) '' + '' + 'Search on Google' + '' + // Wikipedia (W) '' + '' + 'Search on Wikipedia' + '' + '
'; document.body.appendChild(popupEl); popupEl.querySelector('.close-x').addEventListener('click', hidePopup); document.addEventListener('click', function(e){ if (!popupEl.contains(e.target) && !e.target.closest('.subject-pill')) hidePopup(); }, true); document.addEventListener('keydown', function(e){ if (e.key === 'Escape') hidePopup(); }); return popupEl; } function showPopup(x,y,links,options){ var el = ensurePopup(); el.style.left = x + 'px'; el.style.top = y + 'px'; // Assign hrefs var aInsight = el.querySelector('.act-insight'); var aWol = el.querySelector('.act-wol'); var aDb = el.querySelector('.act-db'); var aGoogle = el.querySelector('.act-google'); var aWiki = el.querySelector('.act-wiki'); if (aWol) aWol.href = links.wol; if (aWiki) aWiki.href = links.wiki; if (aGoogle) aGoogle.href = links.google; if (aDb) aDb.href = links.db; // Insight visibility if (aInsight) { if (links.insight) { aInsight.removeAttribute('hidden'); aInsight.href = links.insight; } else { aInsight.setAttribute('hidden',''); } } // DB visibility for >2 words if (aDb) { if (options && options.enableDB) aDb.removeAttribute('hidden'); else aDb.setAttribute('hidden',''); } el.style.display = 'block'; } function hidePopup(){ if (popupEl) popupEl.style.display = 'none'; } // ---- Click handler document.addEventListener('click', function(e){ var pill = e.target.closest('.subject-pill'); if (!pill) return; e.preventDefault(); var text = (pill.getAttribute('data-ref') || pill.textContent || '').trim(); var n = norm(text); // Build links var wol = pill.getAttribute('href') || wolSearchURL(text); var wiki = wikipediaURL(text); var goog = googleURL(text); // DB link only for 1–2 words var dbEnabled = isOneOrTwoWords(text); var db = dbEnabled ? databaseURL(text) : '#'; // Insight visible only if we have an exact match var insightId = INSIGHT_INDEX[n]; var insight = insightId ? insightURL(insightId) : null; var rect = pill.getBoundingClientRect(); var x = rect.left + window.scrollX + rect.width/2 - 120; // center-ish var y = rect.top + window.scrollY + rect.height + 6; showPopup(x, y, { wol:wol, wiki:wiki, google:goog, db:db, insight:insight }, { enableDB: dbEnabled }); }); })();