Przeglądaj źródła

Added exclusion patterns feature; incremented version to 0.2

master
Said Achmiz 6 lat temu
rodzic
commit
a8133b6c26
4 zmienionych plików z 128 dodań i 25 usunięć
  1. 17
    6
      contentScript.js
  2. 1
    1
      manifest.json
  3. 57
    10
      options.html
  4. 53
    8
      options.js

+ 17
- 6
contentScript.js Wyświetl plik

@@ -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' ||

+ 1
- 1
manifest.json Wyświetl plik

@@ -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": [

+ 57
- 10
options.html Wyświetl plik

@@ -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;
}
</style>
</head>
<body>
<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>
<textarea spellcheck="false"></textarea>
<form><button disabled type='button'>Save</button></form>
<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>
</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>
<script src="options.js"></script>
</html>

+ 53
- 8
options.js Wyświetl plik

@@ -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.querySelectorAll("textarea").forEach(textarea => {
textarea.addEventListener("input", (event) => {
document.querySelectorAll("button").forEach(button => {
button.disabled = false;
});
});
});

document.querySelector("textarea").addEventListener("input", (event) => {
document.querySelector("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();

Ładowanie…
Anuluj
Zapisz