diff options
author | Arun Isaac | 2017-06-27 10:20:50 +0530 |
---|---|---|
committer | Arun Isaac | 2017-06-27 10:20:50 +0530 |
commit | 640871c19c42e0967a42926104d36f2a8d837377 (patch) | |
tree | 41624501830e0b213f451b7b2e63413f2494a7a7 /youtube-noscript-shim.user.js | |
parent | e685ece795ce2ea5db7bd18852e987c412e6d3a0 (diff) | |
download | youtube-noscript-shim-640871c19c42e0967a42926104d36f2a8d837377.tar.gz youtube-noscript-shim-640871c19c42e0967a42926104d36f2a8d837377.tar.lz youtube-noscript-shim-640871c19c42e0967a42926104d36f2a8d837377.zip |
Refactor adding event listeners to separate function.
* youtube-noscript-shim.user.js (attachEventListener): New function.
Use attachEventListener, slightly simplifying the script.
Diffstat (limited to 'youtube-noscript-shim.user.js')
-rw-r--r-- | youtube-noscript-shim.user.js | 14 |
1 files changed, 12 insertions, 2 deletions
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"); }); |