Initial commit

This commit is contained in:
Jaculabilis 2018-03-03 17:30:07 -06:00
commit bfeb9d2ff6
4 changed files with 133 additions and 0 deletions

23
manifest.json Normal file
View File

@ -0,0 +1,23 @@
{
"manifest_version": 2,
"name": "DOM Optimizer",
"description": "Keeps your web pages thin by optimizing the DOM.",
"version": "1.0",
"permissions": [
"storage"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["optimize.js"]
}
],
"options_ui": {
"page": "options.html",
"chrome_style": true
}
}

46
optimize.js Normal file
View File

@ -0,0 +1,46 @@
function getLowHangingLeaves() {
var parList = [document];
var chiList = document.childNodes;
while (chiList.length > 0) {
parList = chiList;
chiList = [];
for (var i = 0; i < parList.length; i++) {
Array.prototype.push.apply(chiList, parList[i].childNodes);
}
}
return parList;
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function trimDomIteratively(initialDelay, levelDelay, iterDelay, maxPerIter) {
console.log("DOM optimization called with initialDelay="+initialDelay+" levelDelay="+levelDelay+" iterDelay="+iterDelay+" maxPerIter="+maxPerIter);
if (maxPerIter < 1)
maxPerIter = Number.POSITIVE_INFINITY;
var topEls = [null]
if (initialDelay > 0)
await sleep(initialDelay);
while (topEls.length > 0) {
if (levelDelay > 0)
await sleep(levelDelay);
topEls = getLowHangingLeaves()
for (var i = 0; i < topEls.length && i < maxPerIter; i++) {
if (iterDelay > 0)
await sleep(iterDelay);
topEls[i].parentNode.removeChild(topEls[i]);
}
console.log("Optimized " + i + " elements");
}
}
chrome.storage.sync.get({
initialDelay: 10000,
levelDelay: 1000,
iterDelay: 0,
maxPerIter: 0
}, function(items) {
trimDomIteratively(items.initialDelay, items.levelDelay, items.iterDelay, items.maxPerIter);
}
);

29
options.html Normal file
View File

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<title>DOM Trimmer options</title>
<style>
</style>
<body>
<p>
<input type="number" min=0 id="initial">
Initial delay (ms)
</p>
<p>
<input type="number" min=0 id="level">
Level delay (ms)
</p>
<p>
<input type="number" min=0 id="iter">
Iteration delay (ms)
</p>
<p>
<input type="number" min=0 id="maxPer">
Trims per iteration (dom objects)
</p>
<button id="save">Save</button>
<div id="status"></div>
<script src="options.js"></script>
</body>
</html>

35
options.js Normal file
View File

@ -0,0 +1,35 @@
function save_options() {
var initial = document.getElementById('initial').value;
var level = document.getElementById('level').value;
var iter = document.getElementById('iter').value;
var maxPer = document.getElementById('maxPer').value;
chrome.storage.sync.set({
initialDelay: initial,
levelDelay: level,
iterDelay: iter,
maxPerIter: maxPer
}, function() {
var status = document.getElementById('save');
status.textContent = 'Saved!';
setTimeout(function() {
status.textContent = 'Save';
}, 750);
});
}
function restore_options() {
chrome.storage.sync.get({
initialDelay: 10000,
levelDelay: 1000,
iterDelay: 0,
maxPerIter: 0
}, function(items) {
document.getElementById('initial').value = items.initialDelay;
document.getElementById('level').value = items.levelDelay;
document.getElementById('iter').value = items.iterDelay;
document.getElementById('maxPer').value = items.maxPerIter;
});
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click',
save_options);