From a8133b6c26eb4b387e08463814032e062014a1ef Mon Sep 17 00:00:00 2001 From: achmizs Date: Tue, 29 Jan 2019 18:20:36 -0500 Subject: [PATCH] Added exclusion patterns feature; incremented version to 0.2 --- contentScript.js | 23 ++++++++++++----- manifest.json | 2 +- options.html | 67 ++++++++++++++++++++++++++++++++++++++++-------- options.js | 61 +++++++++++++++++++++++++++++++++++++------ 4 files changed, 128 insertions(+), 25 deletions(-) diff --git a/contentScript.js b/contentScript.js index 489dcdf..1ba196b 100644 --- a/contentScript.js +++ b/contentScript.js @@ -1,21 +1,32 @@ window.onload = () => { - chrome.storage.sync.get("patterns", (result) => { - killStickyIfMatch(result.patterns); + chrome.storage.sync.get([ "matchingPatterns", "exclusionPatterns" ], (result) => { + killStickyIfMatch(result); }); }; -function killStickyIfMatch(patterns) { - if (typeof patterns == "undefined") return; +function killStickyIfMatch(result) { + if (typeof result.matchingPatterns == "undefined") return; - patterns = patterns.split("\n"); var killSticky = false; - for (let pattern of patterns) { + let matchingPatterns = result.matchingPatterns.split("\n"); + for (let pattern of matchingPatterns) { if (location.href.match(new RegExp(pattern))) { killSticky = true; break; } } + let exclusionPatterns = (typeof result.exclusionPatterns != "undefined" && + result.exclusionPatterns != "") ? + result.exclusionPatterns.split("\n") : + [ ]; + for (let pattern of exclusionPatterns) { + if (location.href.match(new RegExp(pattern))) { + killSticky = false; + break; + } + } if (!killSticky) return; + console.log("Killing all stickies!"); document.querySelectorAll('body *').forEach(element => { if (getComputedStyle(element).position === 'fixed' || diff --git a/manifest.json b/manifest.json index 8fb4436..7813cfe 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "name": "AlwaysKillSticky", - "version": "0.1", + "version": "0.2", "description": "Get rid of sticky elements on websites - permanently!", "permissions": [ "activeTab", "storage" ], "content_scripts": [ diff --git a/options.html b/options.html index b4a2dbe..9838549 100644 --- a/options.html +++ b/options.html @@ -20,25 +20,61 @@ padding: 10px; font-size: 1rem; } + body > div { + border: 1px solid #ddd; + padding: 30px 15px 15px 15px; + position: relative; + } + body > div + div { + margin: 2.5em 0 0 0; + } + h1 { + border-bottom: 1px solid #ddd; + } + h2 { + margin: 0; + background-color: #fff; + padding: 10px 15px; + position: absolute; + top: calc(-1 * (0.625em + 10px)); + left: 10px; + border: inherit; + } textarea { - min-height: 400px; - border: 1px solid #bbb; + border: 1px solid #aaa; font-family: Inconsolata, Courier, monospace; font-size: 1.125rem; + width: 100%; + } + #matchingPatterns textarea { + min-height: 300px; + } + #exclusionPatterns textarea { + min-height: 150px; } form { text-align: right; + margin: 0 0 1em 0; + display: flex; + justify-content: flex-end; + align-items: center; } button { -webkit-appearance: none; -moz-appearance: none; - border: 1px solid #99; - font-size: 1.5rem; + border: 1px solid #bbb; color: #fff; - margin: 1em auto; padding: 10px 16px; - background-color: #16e; cursor: default; + margin: 0 0 0 0.5em; + } + button.save-button { + background-color: #16e; + font-size: 1.5rem; + } + button.reset-button { + background-color: #d3453d; + font-size: 1rem; } button:active { transform: scale(0.95); @@ -47,16 +83,27 @@ outline: none; } button:disabled { - filter: saturate(0); + background-color: #777; opacity: 0.5; }

Always Kill Sticky

-

Enter a list of regular expression patterns to match URLs, one per line. Stickies will be killed on pages matching those patterns.

- -
+
+ + +
+
+

Matching patterns

+

Kill stickies on web page URLs matching the following regular expressions (one per line):

+ +
+
+

Exclusion patterns

+

Do not kill stickies on web page URLs matching the following regular expressions (one per line):

+ +
\ No newline at end of file diff --git a/options.js b/options.js index 20c290b..095c0a0 100644 --- a/options.js +++ b/options.js @@ -22,15 +22,60 @@ Element.prototype.removeActivateEvent = function() { this.removeEventListener("keyup", ael); } -document.querySelector("button").addActivateEvent((event) => { - event.target.blur(); +/*******************/ +/* EVENT LISTENERS */ +/*******************/ - let text = document.querySelector("textarea").value; - chrome.storage.sync.set({ "patterns": text }, () => { - event.target.disabled = true; +document.querySelectorAll("button").forEach(button => { + button.addActivateEvent((event) => { + event.target.blur(); + + if (button.classList.contains("save-button")) + saveChanges(); + else + resetChanges(); }); }); -document.querySelector("textarea").addEventListener("input", (event) => { - document.querySelector("button").disabled = false; -}); \ No newline at end of file +document.querySelectorAll("textarea").forEach(textarea => { + textarea.addEventListener("input", (event) => { + document.querySelectorAll("button").forEach(button => { + button.disabled = false; + }); + }); +}); + +/***********/ +/* HELPERS */ +/***********/ + +function saveChanges() { + let matchingPatterns = document.querySelector("#matchingPatterns textarea").value; + let exclusionPatterns = document.querySelector("#exclusionPatterns textarea").value; + chrome.storage.sync.set({ + "matchingPatterns": matchingPatterns, + "exclusionPatterns": exclusionPatterns + }, () => { + document.querySelectorAll("button").forEach(button => { + button.disabled = true; + }); + }); +} + +function resetChanges() { + chrome.storage.sync.get([ "matchingPatterns", "exclusionPatterns" ], (result) => { + document.querySelector("#matchingPatterns textarea").value = result.matchingPatterns; + document.querySelector("#exclusionPatterns textarea").value = result.exclusionPatterns; + document.querySelectorAll("button").forEach(button => { + button.disabled = true; + }); + }); +} + +/******************/ +/* INITIALIZATION */ +/******************/ + +resetChanges(); + +document.querySelector("#matchingPatterns textarea").focus(); \ No newline at end of file