无法执行邮政事故' ON Domwindow':提供的目标原点与收件人窗口不匹配

发布于 2025-02-09 09:28:39 字数 2227 浏览 1 评论 0原文

我正在一个NUXT JS 2项目中工作,并具有称为createIcileframebridgepurgeiciclesiclehashbetweendomains的全局注入函数。这些功能是插件的一部分,并在页面的安装挂钩中被调用。

当我调用清除功能时,它抱怨说:

在“ domwindow”上无法执行'postmessage':提供的目标原点('https://icicle.ext')与收件人窗口的原点不匹配('http://127.0.0.0.1:8001')< /p>

如果我包装postmessage在几秒钟的超时,然后按预期工作,错误会发生。我已经写了另一个全局功能,该功能将在尝试向iframe发布消息之前是否存在该元素是否存在,该函数在延迟和连续检查X秒方面起作用,但是当与我的吹扫检查结合时,JavaScript仍然似乎认为iframe不存在。

我不想依靠具有单个定义值的settimeout,因为这将导致可靠性问题。

我的插件文件:

/*
** Post Icicle bridge data
*/

function postIcicleBridgeData (app, data, url = 'https://icicle.ext') {
  app.$waitForGlobalElement('[data-icicle-hash-bridge]', () => {
    const bridge = document.querySelector('[data-icicle-hash-bridge]').contentWindow
    if (bridge) bridge.postMessage(data, url)
  })
}

export default function ({ app, route, redirect, context }, inject) {

  /*
  ** Create a bridge to Icicle for cross-domain
  */
  inject('createIcicleIframeBridge', (url = 'https://icicle.ext') => {
    setTimeout(() => {
      const iframeBridge = document.createElement('iframe')
      iframeBridge.src = `${url}?v=${Math.round(new Date().getTime() / 1000)}`
      iframeBridge.dataset.icicleHashBridge = true
      iframeBridge.style.display = 'none'
      document.getElementsByTagName('BODY')[0].appendChild(iframeBridge)
    }, 3 * 1000)
  })

  /*
  ** Clear global hash
  */
  inject('purgeIcicleHashBetweenDomains', () => {
    postIcicleBridgeData(app, 'purgeHash')
  })

  /*
  ** Wait for global element to become available
  */
  inject('waitForGlobalElement', (key, callback, delay = 10) => {
    if (document.querySelector(key)) {
      callback()
    } else {
      setTimeout(() => {
        app.$waitForGlobalElement(key, callback)
      }, delay)
    }
  })

}

然后从页面中打来电话:

mounted () {
  this.$createIcicleIframeBridge()

  // clear Icicle hash in case customer doesn't apply
  this.$purgeIcicleHashBetweenDomains()
}

我在这里缺少什么?我需要更改什么,以便我可以确保每当我的清除功能运行iframe时,始终在那里。

I'm working inside a Nuxt JS 2 project and have a global injected function called createIcicleIframeBridge and purgeIcicleHashBetweenDomains. These functions are part of a plugin, and are called from within the mounted hook of a page.

When I call the purge function, it complains and says:

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://icicle.ext') does not match the recipient window's origin ('http://127.0.0.1:8001')

If I wrap the postMessage in a timeout of a few seconds, then it works as intended and the error goes. I've wrote another global function that'll keep checking if the element exists before attempting to post a message to the iframe, which works in terms of delaying and continuously checking for X seconds, but when combined with my purge check, the javascript still seems to think the iFrame doesn't exist.

I don't want to rely on a setTimeout with a single defined value as this will cause reliability issues.

My plugin file:

/*
** Post Icicle bridge data
*/

function postIcicleBridgeData (app, data, url = 'https://icicle.ext') {
  app.$waitForGlobalElement('[data-icicle-hash-bridge]', () => {
    const bridge = document.querySelector('[data-icicle-hash-bridge]').contentWindow
    if (bridge) bridge.postMessage(data, url)
  })
}

export default function ({ app, route, redirect, context }, inject) {

  /*
  ** Create a bridge to Icicle for cross-domain
  */
  inject('createIcicleIframeBridge', (url = 'https://icicle.ext') => {
    setTimeout(() => {
      const iframeBridge = document.createElement('iframe')
      iframeBridge.src = `${url}?v=${Math.round(new Date().getTime() / 1000)}`
      iframeBridge.dataset.icicleHashBridge = true
      iframeBridge.style.display = 'none'
      document.getElementsByTagName('BODY')[0].appendChild(iframeBridge)
    }, 3 * 1000)
  })

  /*
  ** Clear global hash
  */
  inject('purgeIcicleHashBetweenDomains', () => {
    postIcicleBridgeData(app, 'purgeHash')
  })

  /*
  ** Wait for global element to become available
  */
  inject('waitForGlobalElement', (key, callback, delay = 10) => {
    if (document.querySelector(key)) {
      callback()
    } else {
      setTimeout(() => {
        app.$waitForGlobalElement(key, callback)
      }, delay)
    }
  })

}

Then called from within a page:

mounted () {
  this.$createIcicleIframeBridge()

  // clear Icicle hash in case customer doesn't apply
  this.$purgeIcicleHashBetweenDomains()
}

What am I missing here? What do I need to change so that I can ensure whenever my purge function runs the iFrame is always there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文