-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweblabor-app.js
1 lines (1 loc) · 1.24 KB
/
weblabor-app.js
1
const WeblaborApp={allowed:['localhost','127.0.0.1'],events:{back(){return window.history.back()},forward(){return window.history.forward()},getHistoryLength(){return window.history.length},getUrl(){return window.location.href}},init(){if(!window.parent){return}window.addEventListener("message",(event)=>{if(!this.allowed.map((v)=>event.origin.startsWith(`http://${ v }`)||event.origin.startsWith(`https://${ v }`)).includes(true)){return}const data=event.data;if(typeof data!==typeof[]){return}if(!data.length){return}const result=this.parseCall(data);return this.returnValue(window.parent,result[0],result[1])});this.returnOnInit('historyLength',this.events.getHistoryLength());this.returnOnInit('url',this.events.getUrl())},parseCall(params){const name=params[0];if(typeof name!==typeof ''){return}if(!name.startsWith('weblabormx:')){return}const eventName=name.split('weblabormx:')[1];const event=this.events[eventName];if(!event){throw new Error('Event not found')}return[eventName,event(window,...params)]},returnOnInit(name,value){this.returnValue(window.parent,`weblabormx:${ name }.init`,value)},returnValue(target,name,value){if(!name.startsWith('weblabormx:')){name=`weblabormx:${ name }.return`}target.postMessage([name,value],'*')}};WeblaborApp.init();