From 640871c19c42e0967a42926104d36f2a8d837377 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Tue, 27 Jun 2017 10:20:50 +0530 Subject: Refactor adding event listeners to separate function. * youtube-noscript-shim.user.js (attachEventListener): New function. Use attachEventListener, slightly simplifying the script. --- youtube-noscript-shim.user.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'youtube-noscript-shim.user.js') diff --git a/youtube-noscript-shim.user.js b/youtube-noscript-shim.user.js index 6468e78..74fac73 100644 --- a/youtube-noscript-shim.user.js +++ b/youtube-noscript-shim.user.js @@ -47,6 +47,16 @@ function toggleBooleanAttributes (selector, ...attributes) { }); }); } + +/** + * Attach event listener to one or more elements + * @param {string} selector - CSS selector for the elements + * @param {string} eventType - Event type to listen for + * @param {function} listener - Event listener callback function + */ +function attachEventListener (selector, eventType, listener) { + document.querySelectorAll(selector).forEach(function (element) { + element.addEventListener(eventType, listener) }); } @@ -56,14 +66,14 @@ document.querySelectorAll("img[data-thumb]").forEach(function (img) { }); // Search filters -document.querySelector(".filter-button").addEventListener("click", function () { +attachEventListener(".filter-button", "click", function () { toggleClasses(".filter-button", "yt-uix-button-toggled"); toggleClasses(".search-header", "yt-uix-expander-collapsed"); toggleBooleanAttributes(".filter-button", "aria-pressed"); }); // Menu -document.querySelector("#appbar-guide-button").addEvenetListener("click", function () { +attachEventListener("#appbar-guide-button", "click", function () { toggleClasses("html", "show-guide"); toggleBooleanAttributes("#appbar-guide-button", "aria-expanded"); }); -- cgit v1.2.3