From 0de69e306d7e43db85f388254f1ed96cd5295123 Mon Sep 17 00:00:00 2001 From: tophf Date: Sat, 2 Dec 2017 19:54:54 +0300 Subject: [PATCH] code refactor: add $.remove() and $$.remove() --- edit/edit.js | 12 ++++-------- install-usercss/install-usercss.js | 5 ++--- js/dom.js | 18 ++++++++++++++---- popup/popup.js | 6 +++--- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/edit/edit.js b/edit/edit.js index 15cb753c..a7d5c526 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -289,9 +289,7 @@ function initCodeMirror() { } } if (index >= total) { - if (progress) { - progress.remove(); - } + $.remove(progress); return; } if (!progress && @@ -1015,9 +1013,7 @@ function setupGlobalSearch() { const dlg = $('.CodeMirror-dialog', cm.getWrapperElement()); if (!dlg || cmp === 0 || wrapAround && CodeMirror.cmpPos(cm.getCursor(), origPos) >= 0) { - if (dlg) { - dlg.remove(); - } + $.remove(dlg); doReplace(); } }); @@ -2061,7 +2057,7 @@ function setGlobalProgress(done, total) { setTimeout(() => { progressElement.title = progress + '%'; }); - } else if (progressElement) { - progressElement.remove(); + } else { + $.remove(progressElement); } } diff --git a/install-usercss/install-usercss.js b/install-usercss/install-usercss.js index 0034b44f..baecd3e6 100644 --- a/install-usercss/install-usercss.js +++ b/install-usercss/install-usercss.js @@ -112,7 +112,7 @@ $('.header').classList.add('meta-init'); $('.header').classList.remove('meta-init-error'); - setTimeout(() => $('.lds-spinner') && $('.lds-spinner').remove(), 1000); + setTimeout(() => $.remove('.lds-spinner'), 1000); showError(''); requestAnimationFrame(adjustCodeHeight); @@ -195,8 +195,7 @@ function install(style) { installed = style; - $$('.warning') - .forEach(el => el.remove()); + $$.remove('.warning'); $('button.install').disabled = true; $('button.install').classList.add('installed'); $('h2.installed').classList.add('active'); diff --git a/js/dom.js b/js/dom.js index e5502ba4..3dfaa164 100644 --- a/js/dom.js +++ b/js/dom.js @@ -11,6 +11,19 @@ for (const type of [NodeList, NamedNodeMap, HTMLCollection, HTMLAllCollection]) } } +$.remove = (selector, base = document) => { + const el = selector instanceof Node ? selector : $(selector, base); + if (el) { + el.remove(); + } +}; + +$$.remove = (selector, base = document) => { + for (const el of base.querySelectorAll(selector)) { + el.remove(); + } +}; + { // display a full text tooltip on buttons with ellipsis overflow and no inherent title const addTooltipsToEllipsized = () => { @@ -41,10 +54,7 @@ for (const type of [NodeList, NamedNodeMap, HTMLCollection, HTMLAllCollection]) } onDOMready().then(() => { - const el = $('#firefox-transitions-bug-suppressor'); - if (el) { - el.remove(); - } + $.remove('#firefox-transitions-bug-suppressor'); }); if (!chrome.app && chrome.windows) { diff --git a/popup/popup.js b/popup/popup.js index a883aebb..f2fd4bc7 100644 --- a/popup/popup.js +++ b/popup/popup.js @@ -348,7 +348,7 @@ Object.assign(handleEvent, { indicator(event) { const entry = handleEvent.getClickedStyleElement(event); const info = template.regexpProblemExplanation.cloneNode(true); - $$('#' + info.id).forEach(el => el.remove()); + $.remove('#' + info.id); $$('a', info).forEach(el => (el.onclick = handleEvent.openURLandHide)); $$('button', info).forEach(el => (el.onclick = handleEvent.closeExplanation)); entry.appendChild(info); @@ -420,14 +420,14 @@ function handleUpdate(style) { // Add an entry when a new style for the current url is installed if (tabURL && BG.getApplicableSections({style, matchUrl: tabURL, stopOnFirst: true}).length) { document.body.classList.remove('blocked'); - $$('.blocked-info, #no-styles').forEach(el => el.remove()); + $$.remove('.blocked-info, #no-styles'); createStyleElement({style}); } } function handleDelete(id) { - $$(ENTRY_ID_PREFIX + id).forEach(el => el.remove()); + $.remove(ENTRY_ID_PREFIX + id); }