diff --git a/plugin.js b/plugin.js index 71d175a..4aa3437 100644 --- a/plugin.js +++ b/plugin.js @@ -418,6 +418,8 @@ $('.menu .menu__list').eq(0).append(makeButton()); if (Lampa.Selector) Lampa.Selector.update(); } + var menuSubscribed = false; + var menuInterval = null; function start() { if (menuSubscribed) return; menuSubscribed = true; @@ -425,6 +427,15 @@ // Register localized strings for this plugin. try { Lampa.Lang.add(STRINGS); } catch (e) { /* older Lampa: ignore */ } + function tryAdd() { + if ($('.menu .menu__list').length && !$('[data-action="' + PLUGIN.menu_action + '"]').length) { + addMenuButton(); + if (menuInterval) { clearInterval(menuInterval); menuInterval = null; } + return true; + } + return false; + } + Lampa.Listener.follow('menu', function (e) { if (e.type === 'start' && e.body) { var list = e.body.find('.menu__list'); @@ -432,13 +443,17 @@ // guard against duplicate copies in this menu body if (list.find('[data-action="' + PLUGIN.menu_action + '"]').length) return; addMenuButton(); + if (menuInterval) { clearInterval(menuInterval); menuInterval = null; } } }); - // If the main menu is already built, add the item immediately so it does - // NOT appear late and shove other items around. - if ($('.menu .menu__list').length && !$('[data-action="' + PLUGIN.menu_action + '"]').length) { - addMenuButton(); + // The main menu may already be built (or built later) by the time this plugin + // loads. Poll briefly (every 500ms) until it appears, then add the item once. + // This keeps it from showing "late and shoving items" while still being reliable. + if (!tryAdd()) { + menuInterval = setInterval(tryAdd, 500); + // safety: stop polling after 30s regardless + setTimeout(function () { if (menuInterval) { clearInterval(menuInterval); menuInterval = null; } }, 30000); } }