Skip to content

Instantly share code, notes, and snippets.

@FokkeZB
Last active March 6, 2019 20:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save FokkeZB/033d4a9089fff392f7990a452e1d323d to your computer and use it in GitHub Desktop.
Save FokkeZB/033d4a9089fff392f7990a452e1d323d to your computer and use it in GitHub Desktop.
Let a script loaded before Intercom wait until intercom has booted
function afterIntercom() {
if (window.intercomSettings.is_intercom_bubble_enabled && window.intercomSettings.is_intercom_twoway_enabled) {
// do something
}
}
if (window.intercomSettings) {
afterIntercom();
} else {
Object.defineProperty(window, 'intercomSettings', {
configurable: true,
enumerable: true,
get: function() {
return this._intercomSettings;
},
set: function(val) {
this._intercomSettings = val;
afterIntercom();
}
});
}
@sn3p
Copy link

sn3p commented Oct 31, 2016

window.intercomSettings in undefined for me, but I'm watching window.Intercom.booted.
See http://blog.learningspaces.io/customizing-the-intercom-launcher/

// Wait for Intercom to boot (max 30 seconds)
const timeout = setTimeout(() => clearInterval(interval), 30000);
const interval = setInterval(() => {
  if (window.Intercom.booted) {
    // Intercom is booted!

    clearInterval(interval);
    clearTimeout(timeout);
  }
}, 100);

@WatsonFungHK
Copy link

This approach is safer. At the very beginning, intercom might be undefined and ultimately break the app
if (!!window.Intercom && window.Intercom.booted)

@t-richards
Copy link

t-richards commented Mar 5, 2019

In the call to Object.defineProperty, the option

writeable: true,

should be eliminated. When using the correctly spelled option, writable: true, it is considered an error to have accessors and writable at the same time:

TypeError: Invalid property descriptor. Cannot both specify accessors and a value or writable attribute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment