diff --git a/client/components/ExternalLibs/ExternalLibs.vue b/client/components/ExternalLibs/ExternalLibs.vue index cc2bacac..79207a8a 100644 --- a/client/components/ExternalLibs/ExternalLibs.vue +++ b/client/components/ExternalLibs/ExternalLibs.vue @@ -84,12 +84,47 @@ class ExternalLibs extends Vue { //this.commit('reader/setLibs', rstore.libsDefaults); } - mounted() { + mounted() { this.$refs.window.init(); if (!this.frameSrc) - this.frameSrc = this.libs.startLink; - this.frameVisible = false; - this.frameVisible = true; + this.goToStartLink(); + + this.opener = null; + const host = window.location.host; + const openerHost = (host.indexOf('b.') == 0 ? host.substring(2) : host); + const openerOrigin1 = `http://${openerHost}`; + const openerOrigin2 = `https://${openerHost}`; + + window.addEventListener('message', (event) => { + if (event.origin != openerOrigin1 && event.origin != openerOrigin2) + return; + if (!_.isObject(event.data) || event.data.from != 'LibsPage') + return; + if (event.origin == openerOrigin1) + this.opener = window.opener; + else + this.opener = event.source; + this.openerOrigin = event.origin; + +console.log(event); + + this.recvMessage(event.data); + }, false); + } + + recvMessage(d) { + if (d.type == 'mes') { + switch(d.data) { + case 'hello': this.sendMessage({type: 'mes', data: 'ready'}); break; + } + } else if (d.type == 'obj') { + // + } + } + + sendMessage(d) { + if (this.opener && this.openerOrigin) + this.opener.postMessage(Object.assign({}, {from: 'ExternalLibs'}, d), this.openerOrigin); } get libs() { diff --git a/client/components/Reader/LibsPage/LibsPage.vue b/client/components/Reader/LibsPage/LibsPage.vue index c89a8780..f0069d4e 100644 --- a/client/components/Reader/LibsPage/LibsPage.vue +++ b/client/components/Reader/LibsPage/LibsPage.vue @@ -29,8 +29,13 @@ class LibsPage extends Vue { } init() { - this.popupWindow = window.open(`http://${window.location.host}/?p=external-libs#/external-libs`); + const subdomain = (window.location.protocol != 'http:' ? 'b.' : ''); + const origin = `http://${subdomain}${window.location.host}`; + + this.popupWindow = window.open(`${origin}/?p=external-libs#/external-libs`); + if (this.popupWindow) { + //Проверка закрытия окна (async() => { while(this.popupWindow) { @@ -39,8 +44,22 @@ class LibsPage extends Vue { await utils.sleep(1000); } })(); + + window.addEventListener('message', (event) => { + if (event.origin !== origin) + return; + console.log(event.data); + }, false); + + (async() => { + while(this.popupWindow) { + this.popupWindow.postMessage({from: 'LibsPage', type: 'mes', data: 'hello'}, origin); + await utils.sleep(1000); + } + })(); + + this.loadLibs(); } - this.loadLibs(); } done() {