Files
pages/static/js/feed-gecko.js
T
Anya Tokugawa 5321d613b4
Build and publish container images / images (push) Successful in 11m45s
Release container images / release (push) Successful in 21s
feat(feeds): add opt-in Gecko relay and folder shortcuts
Document engine redistribution requirements; exclude upstream engine assets and keep the viewer disabled by default.
2026-11-08 16:14:34 +09:00

55 lines
4.3 KiB
JavaScript

import {memoryProfile} from './feed-gecko-profile.mjs';
const api = '/_/api/v1/extension/organizer.feeds/browser/';
const status = document.getElementById('status'), canvas = document.getElementById('screen');
let engine, started = false, ready = false, stopped = false, navigating = false, poll, resizeTimer;
let history = [], position = 0;
const ja = parent.document.documentElement.lang.startsWith('ja');
const text = (en,jp) => ja ? jp : en;
function report(message) { status.textContent=message; status.hidden=!message; }
function url(value) { try { const u=new URL(value); return ['https:','http:'].includes(u.protocol)&&!u.username&&!u.password&&u.href.length<=8192 ? u.href : ''; } catch {return '';} }
function send(data) { parent.postMessage({type:'pageswiki-gecko',...data},location.origin); }
function navigationState() { send({href:history[position],back:position>0,forward:position<history.length-1}); }
async function navigate(command) {
if(!ready||stopped||navigating)return;
const next=position+(command==='back'?-1:command==='forward'?1:0);
if(next<0||next>=history.length)return;
navigating=true;position=next;
try {
// Minimal upstream GRE omits SessionStoreFunctions: native history.back()
// fails. Keep a bounded viewer-local URL history and re-load the chosen URL.
await engine.load(history[position]);
const current=url(await engine.evalChrome('String(location.href)'));
if(current)history[position]=current;
navigationState();
} catch { report(text('Navigation failed. Use ↗ to open the original.','移動できませんでした。↗から原文を開いてください。')); }
finally {navigating=false;}
}
function stop() { stopped=true;clearTimeout(poll);clearTimeout(resizeTimer);engine?.destroy(); }
async function start(href) {
started=true;
if(!crossOriginIsolated || typeof SharedArrayBuffer==='undefined') { report(text('This browser cannot run the isolated viewer. Use ↗ to open the original.','このブラウザでは分離ビューアを実行できません。↗から原文を開いてください。'));return; }
report(text('Starting Gecko…','Geckoを起動中…'));
const timer=setTimeout(()=>{if(!ready&&!stopped)report(text('Startup is taking longer than expected. Close the viewer to stop, or use ↗.','起動に時間がかかっています。閉じると停止します。↗から原文も開けます。'));},45000);
try {
const {Gecko}=await import(api+'assets/v0.0.1/gecko.js'); if(stopped)return;
const socket=new URL(api+'wisp/',location.href);socket.protocol=location.protocol==='https:'?'wss:':'ws:';
engine=new Gecko({canvas,wasm:{url:api+'assets/v0.0.1/gecko.wasm.zst',compressed:true},wispUrl:socket.href,profile:memoryProfile(),width:Math.min(innerWidth,1920),height:Math.min(innerHeight,1080),env:{GECKO_NOWASMJIT:'1'},print:()=>{},printErr:()=>{}});
await engine.init();if(stopped)return;
await engine.load(href);if(stopped)return;
history=[url(await engine.evalChrome('String(location.href)'))||href];position=0;
ready=true;report('');canvas.focus();send({ready:true});
navigationState();
const update=async()=>{if(stopped)return;try{if(!navigating){const href=url(await engine.evalChrome('String(location.href)'));if(href&&!navigating&&href!==history[position]){history=history.slice(0,position+1);history.push(href);if(history.length>64)history.shift();position=history.length-1;navigationState();}}}catch{/* Keep the last validated URL. */}if(!stopped)poll=setTimeout(update,2000);};update();
} catch { report(text('Viewer failed. Close it and retry, or use ↗ to open the original.','ビューアを開始できませんでした。閉じて再試行するか、↗から原文を開いてください。')); }
finally {clearTimeout(timer);}
}
addEventListener('message',event=>{
if(event.source!==parent||event.origin!==location.origin||event.data?.type!=='pageswiki-gecko')return;
if(!started){const href=url(event.data.href);if(href)start(href);return;}
if(['back','forward','reload'].includes(event.data.command))navigate(event.data.command);
});
addEventListener('resize',()=>{clearTimeout(resizeTimer);resizeTimer=setTimeout(()=>{if(ready&&!stopped)engine.resize(Math.min(innerWidth,1920),Math.min(innerHeight,1080)).catch(()=>{});},150);});
addEventListener('keydown',e=>{if(e.key==='Escape'){e.preventDefault();send({close:true});}},true);
addEventListener('pagehide',stop);
send({listening:true});