From 59daccdbf2890e4ecef9f297f62b4bf798585e5c Mon Sep 17 00:00:00 2001 From: Send_Nukez Date: Sat, 13 Nov 2021 21:49:11 +0100 Subject: [PATCH] add ready state to info so it won't throw an error when registering items too early --- src/js/Info.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/js/Info.js b/src/js/Info.js index bc79083..80ba54a 100644 --- a/src/js/Info.js +++ b/src/js/Info.js @@ -24,11 +24,16 @@ export default class Info { /** @type {MarkdownIt} */ #md; + /** @type {Boolean} */ + #ready = false; + constructor() { waitForElement([".main-topBar-container", ".main-userWidget-box"], ([topBarContainer, userWidget]) => { this.#container = document.createElement("div"); this.#container.id = "dribbblish-info-container"; topBarContainer.insertBefore(this.#container, userWidget); + + this.#ready = true; }); } @@ -37,6 +42,11 @@ export default class Info { * @param {DribbblishInfo} info */ set(key, info) { + if (!this.#ready) { + setTimeout(() => this.set(key, info), 200); + return; + } + this.remove(key); if (info.text == null && info.icon == null) throw new Error("invalid info"); @@ -58,6 +68,9 @@ export default class Info { this.#container.appendChild(elem); } + /** + * @param {String} key + */ remove(key) { $(this.#container).find(`.dribbblish-info-item[key="${key}"]`).remove(); }