Added exclusion patterns feature; incremented version to 0.2

This commit is contained in:
Said Achmiz 2019-01-29 18:20:36 -05:00
parent 08e07344c0
commit a8133b6c26
4 changed files with 128 additions and 25 deletions

View File

@ -1,21 +1,32 @@
window.onload = () => { window.onload = () => {
chrome.storage.sync.get("patterns", (result) => { chrome.storage.sync.get([ "matchingPatterns", "exclusionPatterns" ], (result) => {
killStickyIfMatch(result.patterns); killStickyIfMatch(result);
}); });
}; };
function killStickyIfMatch(patterns) { function killStickyIfMatch(result) {
if (typeof patterns == "undefined") return; if (typeof result.matchingPatterns == "undefined") return;
patterns = patterns.split("\n");
var killSticky = false; 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))) { if (location.href.match(new RegExp(pattern))) {
killSticky = true; killSticky = true;
break; 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; if (!killSticky) return;
console.log("Killing all stickies!"); console.log("Killing all stickies!");
document.querySelectorAll('body *').forEach(element => { document.querySelectorAll('body *').forEach(element => {
if (getComputedStyle(element).position === 'fixed' || if (getComputedStyle(element).position === 'fixed' ||

View File

@ -1,6 +1,6 @@
{ {
"name": "AlwaysKillSticky", "name": "AlwaysKillSticky",
"version": "0.1", "version": "0.2",
"description": "Get rid of sticky elements on websites - permanently!", "description": "Get rid of sticky elements on websites - permanently!",
"permissions": [ "activeTab", "storage" ], "permissions": [ "activeTab", "storage" ],
"content_scripts": [ "content_scripts": [

View File

@ -20,25 +20,61 @@
padding: 10px; padding: 10px;
font-size: 1rem; 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 { textarea {
min-height: 400px; border: 1px solid #aaa;
border: 1px solid #bbb;
font-family: Inconsolata, Courier, monospace; font-family: Inconsolata, Courier, monospace;
font-size: 1.125rem; font-size: 1.125rem;
width: 100%;
}
#matchingPatterns textarea {
min-height: 300px;
}
#exclusionPatterns textarea {
min-height: 150px;
} }
form { form {
text-align: right; text-align: right;
margin: 0 0 1em 0;
display: flex;
justify-content: flex-end;
align-items: center;
} }
button { button {
-webkit-appearance: none; -webkit-appearance: none;
-moz-appearance: none; -moz-appearance: none;
border: 1px solid #99; border: 1px solid #bbb;
font-size: 1.5rem;
color: #fff; color: #fff;
margin: 1em auto;
padding: 10px 16px; padding: 10px 16px;
background-color: #16e;
cursor: default; 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 { button:active {
transform: scale(0.95); transform: scale(0.95);
@ -47,16 +83,27 @@
outline: none; outline: none;
} }
button:disabled { button:disabled {
filter: saturate(0); background-color: #777;
opacity: 0.5; opacity: 0.5;
} }
</style> </style>
</head> </head>
<body> <body>
<h1>Always Kill Sticky</h1> <h1>Always Kill Sticky</h1>
<p>Enter a list of <a href='https://regexr.com/' rel='nofollow'>regular expression</a> patterns to match URLs, one per line. Stickies will be killed on pages matching those patterns.</p> <form>
<button disabled type='button' class='reset-button'>Reset</button>
<button disabled type='button' class='save-button'>Save</button>
</form>
<div id='matchingPatterns'>
<h2>Matching patterns</h2>
<p>Kill stickies on web page URLs matching the following <a href='https://regexr.com/' rel='nofollow'>regular expressions</a> (one per line):</p>
<textarea spellcheck="false"></textarea> <textarea spellcheck="false"></textarea>
<form><button disabled type='button'>Save</button></form> </div>
<div id='exclusionPatterns'>
<h2>Exclusion patterns</h2>
<p>Do <strong>not</strong> kill stickies on web page URLs matching the following <a href='https://regexr.com/' rel='nofollow'>regular expressions</a> (one per line):</p>
<textarea spellcheck="false"></textarea>
</div>
</body> </body>
<script src="options.js"></script> <script src="options.js"></script>
</html> </html>

View File

@ -22,15 +22,60 @@ Element.prototype.removeActivateEvent = function() {
this.removeEventListener("keyup", ael); this.removeEventListener("keyup", ael);
} }
document.querySelector("button").addActivateEvent((event) => { /*******************/
/* EVENT LISTENERS */
/*******************/
document.querySelectorAll("button").forEach(button => {
button.addActivateEvent((event) => {
event.target.blur(); event.target.blur();
let text = document.querySelector("textarea").value; if (button.classList.contains("save-button"))
chrome.storage.sync.set({ "patterns": text }, () => { saveChanges();
event.target.disabled = true; else
resetChanges();
}); });
}); });
document.querySelector("textarea").addEventListener("input", (event) => { document.querySelectorAll("textarea").forEach(textarea => {
document.querySelector("button").disabled = false; 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();