From 019c1de94c0b15b1f1c5af3b401ee27bcf3a6c86 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Tue, 27 Jun 2017 10:15:19 +0530 Subject: Refactor toggleClasses and toggleBooleanAttributes. * youtube-noscript-shim.user.js (toggleClasses): Make classes argument variadic. Support multiple elements. Document it. (toggleBooleanAttributes): Make attributes argument variadic. Support multiple elements. Document it. Fix toggleClasses and toggleBooleanAttributes function calls. --- youtube-noscript-shim.user.js | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) (limited to 'youtube-noscript-shim.user.js') diff --git a/youtube-noscript-shim.user.js b/youtube-noscript-shim.user.js index beee814..92b189f 100644 --- a/youtube-noscript-shim.user.js +++ b/youtube-noscript-shim.user.js @@ -22,16 +22,31 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -function toggleClasses (selector, classes) { - classes.forEach(function (cls) { - document.querySelector(selector).classList.toggle(cls); +/** + * Add/remove classes on one or more elements + * @param {string} selector - CSS selector for the elements + * @param {...string} classes - Classes to add/remove + */ +function toggleClasses (selector, ...classes) { + document.querySelectorAll(selector).forEach(function (element) { + classes.forEach(function (cls) { + element.classList.toggle(cls); + }); }); } -function toggleBooleanAttributes (selector, attributes) { - element = document.querySelector(selector); - attributes.forEach(function (attribute) { - element.setAttribute(attribute, element.getAttribute(attribute) == "true" ? "false" : "true"); +/** + * Toggle boolean attributes on one or more elements + * @param {string} selector - CSS selector for the elements + * @param {...string} classes - Boolean attributes to toggle + */ +function toggleBooleanAttributes (selector, ...attributes) { + document.querySelectorAll(selector).forEach(function (element) { + attributes.forEach(function (attribute) { + element.setAttribute(attribute, element.getAttribute(attribute) == "true" ? "false" : "true"); + }); + }); +} }); } @@ -42,13 +57,13 @@ Array.from(document.querySelectorAll("img[data-thumb]")).forEach(function (img) // Search filters document.querySelector(".filter-button").addEventListener("click", function () { - toggleClasses(".filter-button", ["yt-uix-button-toggled"]); - toggleClasses(".search-header", ["yt-uix-expander-collapsed"]); - toggleBooleanAttributes(".filter-button", ["aria-pressed"]); + toggleClasses(".filter-button", "yt-uix-button-toggled"); + toggleClasses(".search-header", "yt-uix-expander-collapsed"); + toggleBooleanAttributes(".filter-button", "aria-pressed"); }); -// Guide -document.querySelector("#appbar-guide-button").addEventListener("click", function () { - toggleClasses("html", ["show-guide"]); - toggleBooleanAttributes("#appbar-guide-button", ["aria-expanded"]); +// Menu +document.querySelector("#appbar-guide-button").addEvenetListener("click", function () { + toggleClasses("html", "show-guide"); + toggleBooleanAttributes("#appbar-guide-button", "aria-expanded"); }); -- cgit v1.2.3