js
(async () => {
observeEditorLoaded((editor)=>{
console.log(editor, 'editor loaded');
});
function observeEditorLoaded(callback) {
const observer = new MutationObserver((mutationsList, observer) => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
if (mutation.target.classList?.contains("protyle-wysiwyg")) {
if(!mutation.target?.dataset?.loaded) {
mutation.target.dataset.loaded = true;
callback(mutation.target);
}
}
}
}
});
const config = {
childList: true,
subtree: true,
attributes: false,
};
const targetNode = document.body;
observer.observe(targetNode, config);
return () => observer.disconnect();
}
})();
js
(()=>{
whenElementExist('.layout__center').then(async element => {
await sleep(40);
observeDomChange(element, async (mutation) => {
if (mutation.target.classList?.contains("protyle-wysiwyg")) {
const editor = mutation.target;
if(editor && editor.closest){
const protyle = editor.closest(".protyle");
if(protyle.dataset.loading !== 'finished'){
await whenElementExist(()=>editor?.closest(".protyle") === 'finished');
}
console.log('editor loaded');
}
}
});
});
function observeDomChange(selector, callback) {
const onChange = function(mutationsList, observer) {
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
callback(mutation);
}
}
};
const observer = new MutationObserver(onChange);
const config = { attributes: false, childList: true, subtree: true };
const targetNode = typeof selector === 'string' ? document.querySelector(selector) : selector;
if (targetNode) {
observer.observe(targetNode, config);
}
return () => {
observer.disconnect();
};
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function whenElementExist(selector) {
return new Promise(resolve => {
const checkForElement = () => {
let element = null;
if (typeof selector === 'function') {
element = selector();
} else {
element = document.querySelector(selector);
}
if (element) {
resolve(element);
} else {
requestAnimationFrame(checkForElement);
}
};
checkForElement();
});
}
})();