mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-08-22 05:07:05 +00:00
testing wait alternatives
This commit is contained in:
@@ -23,10 +23,12 @@ exports.stopApplication = function () {
|
||||
};
|
||||
|
||||
exports.getDocument = function (callback, ms) {
|
||||
if (!ms || ms < 1000) ms = 1000;
|
||||
const url = "http://" + (config.address || "localhost") + ":" + (config.port || "8080");
|
||||
jsdom.JSDOM.fromURL(url, { resources: "usable", runScripts: "dangerously" }).then((dom) => {
|
||||
dom.window.name = "jsdom";
|
||||
dom.window.onload = function () {
|
||||
global.MutationObserver = dom.window.MutationObserver;
|
||||
global.document = dom.window.document;
|
||||
setTimeout(() => {
|
||||
callback();
|
||||
@@ -34,3 +36,23 @@ exports.getDocument = function (callback, ms) {
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
exports.waitForElement = function(selector) {
|
||||
return new Promise(resolve => {
|
||||
if (document.querySelector(selector)) {
|
||||
return resolve(document.querySelector(selector));
|
||||
}
|
||||
|
||||
const observer = new MutationObserver(() => {
|
||||
if (document.querySelector(selector)) {
|
||||
resolve(document.querySelector(selector));
|
||||
observer.disconnect();
|
||||
}
|
||||
});
|
||||
|
||||
observer.observe(document.body, {
|
||||
childList: true,
|
||||
subtree: true
|
||||
});
|
||||
});
|
||||
};
|
||||
|
Reference in New Issue
Block a user