diff options
author | Arun Isaac | 2017-06-27 10:15:19 +0530 |
---|---|---|
committer | Arun Isaac | 2017-06-27 10:15:19 +0530 |
commit | 019c1de94c0b15b1f1c5af3b401ee27bcf3a6c86 (patch) | |
tree | f99976bee3996c9d4de44369e2ab1fdc85808347 | |
parent | 601073e2099c6c801ec25eba31a44dd7e268ca7a (diff) | |
download | youtube-noscript-shim-019c1de94c0b15b1f1c5af3b401ee27bcf3a6c86.tar.gz youtube-noscript-shim-019c1de94c0b15b1f1c5af3b401ee27bcf3a6c86.tar.lz youtube-noscript-shim-019c1de94c0b15b1f1c5af3b401ee27bcf3a6c86.zip |
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.
-rw-r--r-- | youtube-noscript-shim.user.js | 43 |
1 files changed, 29 insertions, 14 deletions
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 <http://www.gnu.org/licenses/>. -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"); }); |