1.18更新
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
(function(){
|
||||
function defineCallGenerate(){
|
||||
(function(){
|
||||
function defineCallGenerate(){
|
||||
var parentOrigin;
|
||||
try{parentOrigin=new URL(document.referrer).origin}catch(_){parentOrigin='*'}
|
||||
function sanitizeOptions(options){
|
||||
try{
|
||||
return JSON.parse(JSON.stringify(options,function(k,v){return(typeof v==='function')?undefined:v}))
|
||||
@@ -29,12 +31,13 @@
|
||||
function CallGenerateImpl(options){
|
||||
return new Promise(function(resolve,reject){
|
||||
try{
|
||||
function post(m){try{parent.postMessage(m,'*')}catch(e){}}
|
||||
function post(m){try{parent.postMessage(m,parentOrigin)}catch(e){}}
|
||||
if(!options||typeof options!=='object'){reject(new Error('Invalid options'));return}
|
||||
var id=Date.now().toString(36)+Math.random().toString(36).slice(2);
|
||||
function onMessage(e){
|
||||
var d=e&&e.data||{};
|
||||
if(d.source!=='xiaobaix-host'||d.id!==id)return;
|
||||
function onMessage(e){
|
||||
if(parentOrigin!=='*'&&e&&e.origin!==parentOrigin)return;
|
||||
var d=e&&e.data||{};
|
||||
if(d.source!=='xiaobaix-host'||d.id!==id)return;
|
||||
if(d.type==='generateStreamStart'&&options.streaming&&options.streaming.onStart){try{options.streaming.onStart(d.sessionId)}catch(_){}}
|
||||
else if(d.type==='generateStreamChunk'&&options.streaming&&options.streaming.onChunk){try{options.streaming.onChunk(d.chunk,d.accumulated)}catch(_){}}
|
||||
else if(d.type==='generateStreamComplete'){try{window.removeEventListener('message',onMessage)}catch(_){}
|
||||
@@ -46,10 +49,14 @@
|
||||
else if(d.type==='generateError'){try{window.removeEventListener('message',onMessage)}catch(_){}
|
||||
reject(new Error(d.error||'Generation failed'))}
|
||||
}
|
||||
try{window.addEventListener('message',onMessage)}catch(_){}
|
||||
// eslint-disable-next-line no-restricted-syntax -- origin checked via parentOrigin.
|
||||
try{window.addEventListener('message',onMessage)}catch(_){}
|
||||
var sanitized=sanitizeOptions(options);
|
||||
post({type:'generateRequest',id:id,options:sanitized});
|
||||
setTimeout(function(){try{window.removeEventListener('message',onMessage)}catch(e){};reject(new Error('Generation timeout'))},300000);
|
||||
setTimeout(function(){
|
||||
try{window.removeEventListener('message',onMessage)}catch(e){}
|
||||
reject(new Error('Generation timeout'));
|
||||
},300000);
|
||||
}catch(e){reject(e)}
|
||||
})
|
||||
}
|
||||
@@ -57,10 +64,12 @@
|
||||
try{window.callGenerate=CallGenerateImpl}catch(e){}
|
||||
try{window.__xb_callGenerate_loaded=true}catch(e){}
|
||||
}
|
||||
try{defineCallGenerate()}catch(e){}
|
||||
})();
|
||||
|
||||
(function(){
|
||||
try{defineCallGenerate()}catch(e){}
|
||||
})();
|
||||
|
||||
(function(){
|
||||
var parentOrigin;
|
||||
try{parentOrigin=new URL(document.referrer).origin}catch(_){parentOrigin='*'}
|
||||
function applyAvatarCss(urls){
|
||||
try{
|
||||
const root=document.documentElement;
|
||||
@@ -83,18 +92,20 @@
|
||||
}
|
||||
}catch(_){}
|
||||
}
|
||||
function requestAvatars(){
|
||||
try{parent.postMessage({type:'getAvatars'},'*')}catch(_){}
|
||||
}
|
||||
function onMessage(e){
|
||||
const d=e&&e.data||{};
|
||||
if(d&&d.source==='xiaobaix-host'&&d.type==='avatars'){
|
||||
function requestAvatars(){
|
||||
try{parent.postMessage({type:'getAvatars'},parentOrigin)}catch(_){}
|
||||
}
|
||||
function onMessage(e){
|
||||
if(parentOrigin!=='*'&&e&&e.origin!==parentOrigin)return;
|
||||
const d=e&&e.data||{};
|
||||
if(d&&d.source==='xiaobaix-host'&&d.type==='avatars'){
|
||||
applyAvatarCss(d.urls);
|
||||
try{window.removeEventListener('message',onMessage)}catch(_){}
|
||||
}
|
||||
}
|
||||
try{
|
||||
window.addEventListener('message',onMessage);
|
||||
try{
|
||||
// eslint-disable-next-line no-restricted-syntax -- origin checked via parentOrigin.
|
||||
window.addEventListener('message',onMessage);
|
||||
if(document.readyState==='loading'){
|
||||
document.addEventListener('DOMContentLoaded',requestAvatars,{once:true});
|
||||
}else{
|
||||
@@ -102,4 +113,4 @@
|
||||
}
|
||||
window.addEventListener('load',requestAvatars,{once:true});
|
||||
}catch(_){}
|
||||
})();
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user