Skip to content

improvement: use mutation observer to replace mutation events when available #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 66 additions & 14 deletions src/MessengerCustomerChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,69 @@ const removeElementByIds = ids => {
});
};

/**
* Use MutationObserver or DOMNodeInserted event to check if the fb_dialog DOM is loaded.
* If it is loaded, call parameter function `callback`.
* @param {function} callback
*/
const addDOMNodeListenerOfFacebookPlugin = callback => {
// check compatibility of MutationObserver
const MutationObserver =
window.MutationObserver ||
window.WebKitMutationObserver ||
window.MozMutationObserver;
if (MutationObserver) {
// create an observer instance
const observer = new MutationObserver((mutations, observerSelf) => {
mutations.forEach(mutation => {
if (
mutation.target.id === 'fb-root' &&
mutation.addedNodes.length > 0
) {
const { addedNodes } = mutation;
addedNodes.forEach(element => {
if (
element &&
element.className &&
typeof element.className === 'string' &&
element.className.includes('fb_dialog')
) {
callback();
observerSelf.disconnect();
}
});
}
});
});
// select the target node
const target = document.querySelector('body');
// configuration of the observer:
const config = { childList: true, subtree: true };

// pass in the target node, as well as the observer options
observer.observe(target, config);
} else if (document.body.addEventListener) {
document.addEventListener(
'DOMNodeInserted',
event => {
const element = event.target;
if (
element.className &&
typeof element.className === 'string' &&
element.className.includes('fb_dialog')
) {
callback();
}
},
false
);
} else {
console.warn(
'MutationObserver and addEventListener are not supported! Callback function of addDOMNodeListenerOfFacebookPlugin would not work.'
);
}
};

export default class MessengerCustomerChat extends Component {
static propTypes = {
pageId: PropTypes.string.isRequired,
Expand Down Expand Up @@ -204,20 +267,9 @@ export default class MessengerCustomerChat extends Component {
const { fbLoaded, shouldShowDialog } = this.state;

if (fbLoaded && shouldShowDialog !== this.props.shouldShowDialog) {
document.addEventListener(
'DOMNodeInserted',
event => {
const element = event.target;
if (
element.className &&
typeof element.className === 'string' &&
element.className.includes('fb_dialog')
) {
this.controlPlugin();
}
},
false
);
addDOMNodeListenerOfFacebookPlugin(() => {
this.controlPlugin();
});
this.subscribeEvents();
}
// Add a random key to rerender. Reference:
Expand Down