我正在尝试在 Ruby on Rails turbolinks 站点中使用 Freshdesk 小部件。
自从我开始使用此小部件以来,在初始页面加载后导航到其他页面时,我的控制台中总是出现以下错误:
> VM2797 <widget_id>.js:1 Uncaught TypeError: Cannot read property
> 'postMessage' of null
> at Object.postMessage (VM2797 <widget_id>.js:1)
> at Object.widgetRenderComplete (VM2797 <widget_id>.js:1)
> at Object.handleMessage (VM2797 <widget_id>.js:1)
这是我的代码在添加带有 id 的 div 之前(我在视图中将其称为部分,并将 ID 作为变量)
<script>
window.fwSettings={
'widget_id': <%= widget_id %>
};
!function(){if("function"!=typeof window.FreshworksWidget){var n=function(){n.q.push(arguments)};n.q=[],window.FreshworksWidget=n}}()
</script>
<script type='text/javascript' src='https://widget.freshworks.com/widgets/<%= widget_id %>.js' async defer></script>
查看示例
<%= render "shared/freshdesk_widget", widget_id: 60000003593 %>
我考虑过使用eventListener turbolinks:before-cache来查找 div 并在缓存前删除它。
这就是现在的样子
<script type='text/javascript'>
document.addEventListener("turbolinks:before-cache", function() {
const deleteable = document.getElementById("div_to_delete");
deleteable.parentNode.removeChild(deleteable);
})
</script>
<div id="div_to_delete">
<script>
window.fwSettings={
'widget_id': <%= widget_id %>
};
!function(){if("function"!=typeof window.FreshworksWidget){var n=function(){n.q.push(arguments)};n.q=[],window.FreshworksWidget=n}}()
</script>
<script type='text/javascript' src='https://widget.freshworks.com/widgets/<%= widget_id %>.js' async defer></script>
</div>
我面临两个错误
> VM49956:4 Uncaught TypeError: Cannot read property 'parentNode' of
> null
> at HTMLDocument.<anonymous> (<anonymous>:4:15)
> at
Object../node_modules/turbolinks/dist/turbolinks.js.e.dispatch
> (turbolinks.js:5)
> at r.notifyApplicationBeforeCachingSnapshot
(turbolinks.js:6)
> at r.cacheSnapshot (turbolinks.js:6)
> at r.cacheSnapshot (turbolinks.js:5)
> at r.<anonymous> (turbolinks.js:5)
> at turbolinks.js:5
和以前一样
> VM2797 <widget_id>.js:1 Uncaught TypeError: Cannot read property
> 'postMessage' of null
> at Object.postMessage (VM2797 <widget_id>.js:1)
> at Object.widgetRenderComplete (VM2797 <widget_id>.js:1)
> at Object.handleMessage (VM2797 <widget_id>.js:1)
提前致谢!干杯