From 887c8a89e9882cc76d485d699785ca1f8c9f1b4e Mon Sep 17 00:00:00 2001 From: achmizs Date: Wed, 30 Jan 2019 17:35:05 -0500 Subject: [PATCH] Broke out shared code into functions.js --- contentScript.js | 11 ++--------- functions.js | 41 +++++++++++++++++++++++++++++++++++++++++ options.html | 1 + options.js | 24 ------------------------ 4 files changed, 44 insertions(+), 33 deletions(-) create mode 100644 functions.js diff --git a/contentScript.js b/contentScript.js index e858b44..6544722 100644 --- a/contentScript.js +++ b/contentScript.js @@ -29,12 +29,5 @@ function killStickyIfMatch(result) { } } if (!killSticky) return; - - console.log("Killing all stickies!"); - document.querySelectorAll('body *').forEach(element => { - if (getComputedStyle(element).position === 'fixed' || - getComputedStyle(element).position === 'sticky') { - element.remove(); - } - }); -} \ No newline at end of file + killSticky(); +} diff --git a/functions.js b/functions.js new file mode 100644 index 0000000..7987a0d --- /dev/null +++ b/functions.js @@ -0,0 +1,41 @@ +/*******************************/ +/* EVENT LISTENER MANIPULATION */ +/*******************************/ + +/* Adds an event listener to a button (or other clickable element), attaching + it to both "click" and "keyup" events (for use with keyboard navigation). + Optionally also attaches the listener to the 'mousedown' event, making the + element activate on mouse down instead of mouse up. */ +Element.prototype.addActivateEvent = function(func, includeMouseDown) { + let ael = this.activateEventListener = (event) => { if (event.button === 0 || event.key === ' ') func(event) }; + if (includeMouseDown) this.addEventListener("mousedown", ael); + this.addEventListener("click", ael); + this.addEventListener("keyup", ael); +} + +/* Removes event listener from a clickable element, automatically detaching it + from all relevant event types. */ +Element.prototype.removeActivateEvent = function() { + let ael = this.activateEventListener; + this.removeEventListener("mousedown", ael); + this.removeEventListener("click", ael); + this.removeEventListener("keyup", ael); +} + +/***********/ +/* HELPERS */ +/***********/ + +function killSticky() { + console.log("Killing all stickies!"); + document.querySelectorAll('body *').forEach(element => { + if (getComputedStyle(element).position === 'fixed' || + getComputedStyle(element).position === 'sticky') { + element.remove(); + } + }); +} + +String.prototype.hasPrefix = function (prefix) { + return (this.lastIndexOf(prefix, 0) === 0); +} diff --git a/options.html b/options.html index ef0ef73..f22dfac 100644 --- a/options.html +++ b/options.html @@ -208,5 +208,6 @@

NOTE: Exclusion patterns override matching patterns; if a page is both matched and excluded, stickies will not be killed on that page.

+ \ No newline at end of file diff --git a/options.js b/options.js index b03b8dd..b7b5296 100644 --- a/options.js +++ b/options.js @@ -1,27 +1,3 @@ -/*******************************/ -/* EVENT LISTENER MANIPULATION */ -/*******************************/ - -/* Adds an event listener to a button (or other clickable element), attaching - it to both "click" and "keyup" events (for use with keyboard navigation). - Optionally also attaches the listener to the 'mousedown' event, making the - element activate on mouse down instead of mouse up. */ -Element.prototype.addActivateEvent = function(func, includeMouseDown) { - let ael = this.activateEventListener = (event) => { if (event.button === 0 || event.key === ' ') func(event) }; - if (includeMouseDown) this.addEventListener("mousedown", ael); - this.addEventListener("click", ael); - this.addEventListener("keyup", ael); -} - -/* Removes event listener from a clickable element, automatically detaching it - from all relevant event types. */ -Element.prototype.removeActivateEvent = function() { - let ael = this.activateEventListener; - this.removeEventListener("mousedown", ael); - this.removeEventListener("click", ael); - this.removeEventListener("keyup", ael); -} - /*******************/ /* EVENT LISTENERS */ /*******************/