Skip to content

Commit

Permalink
I can't take it anymore; lines should end in semicolons
Browse files Browse the repository at this point in the history
  • Loading branch information
a13o committed Aug 11, 2018
1 parent 8e0dc6b commit a6240ec
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 109 deletions.
24 changes: 24 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"env": {
"browser": true,
"es6": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"indent": [
"error",
2
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}
74 changes: 37 additions & 37 deletions src/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,34 @@
* @returns {MutationObserver} Returns the observer used in case you want to disconnect it.
*/
function jscss (selector, props) { // eslint-disable-line no-unused-vars
const body = document.getRootNode().body
const body = document.getRootNode().body;

let matchCache = body.querySelectorAll(selector)
let matchCache = body.querySelectorAll(selector);
matchCache.forEach((elem) => {
applyStyle(elem, props)
})
applyStyle(elem, props);
});

const observer = new MutationObserver(() => {
const matches = body.querySelectorAll(selector)
const matches = body.querySelectorAll(selector);

// Exit early if there are no new items. Also works for the empty case.
if (matchCache.length === matches.length) {
let different = false
let different = false;
for (let k = 0; k < matches.length; k++) {
different = matches[k] !== matchCache[k]
if (different) { break }
different = matches[k] !== matchCache[k];
if (different) { break; }
}
if (!different) { return }
if (!different) { return; }
}

// MutationObservers can trigger themselves recursively so we want to
// update matchCache now, before we possibly modify elements further.
const oldMatches = matchCache
matchCache = matches
const oldMatches = matchCache;
matchCache = matches;

let i
let j
let seen
let i;
let j;
let seen;

/**
* Apply styles to newly ADDED elements.
Expand All @@ -65,35 +65,35 @@ function jscss (selector, props) { // eslint-disable-line no-unused-vars
* possible cuz there's no duplicate items.
*/
for (i = 0; i < matches.length; i++) {
seen = false
seen = false;
for (j = 0; j < oldMatches.length; j++) {
seen = matches[i] === oldMatches[j]
if (seen) { break }
seen = matches[i] === oldMatches[j];
if (seen) { break; }
}
if (seen) { continue }
if (seen) { continue; }
// From this point forward matches[i] is known to be newly ADDED.
applyStyle(matches[i], props)
applyStyle(matches[i], props);

// As an optimization, jscss can be requested to apply only once.
if (props.firstResultOnly) {
observer.disconnect()
observer.disconnect();
}
}

/**
* Apply styles to newly REMOVED elements.
*/
for (j = 0; j < oldMatches.length; j++) {
seen = false
seen = false;
for (i = 0; i < matches.length; i++) {
seen = oldMatches[j] === matches[i]
if (seen) { break }
seen = oldMatches[j] === matches[i];
if (seen) { break; }
}
if (seen) { continue }
if (seen) { continue; }
// From this point forward oldMatches[i] is known to be newly REMOVED.
removeStyle(oldMatches[j], props)
removeStyle(oldMatches[j], props);
}
})
});

observer.observe(body, {
subtree: true,
Expand All @@ -102,15 +102,15 @@ function jscss (selector, props) { // eslint-disable-line no-unused-vars
// Technically any attribute can cause a change worth observing since css
// has attribute selectors. No need atm though, so let's be performant.
attributeFilter: ['id', 'class']
})
});

return observer
return observer;
}

function applyStyle (elem, props) {
// Search/replace text support
if ('search' in props && 'replace' in props) {
addTextObserver(elem, props)
addTextObserver(elem, props);
}
}

Expand All @@ -122,22 +122,22 @@ function removeStyle (elem, props) {
}

function addTextObserver (elem, props) {
searchAndReplaceChildren(elem, props)
searchAndReplaceChildren(elem, props);

const observer = new MutationObserver(() => searchAndReplaceChildren(elem, props))
const observer = new MutationObserver(() => searchAndReplaceChildren(elem, props));
observer.observe(elem, {
childList: true,
characterData: true
})
});
}

function searchAndReplaceChildren (elem, props) {
elem.childNodes.forEach((child) => {
if (child.nodeType !== Node.TEXT_NODE) { return }
const newStr = child.textContent.replace(props.search, props.replace)
if (child.nodeType !== Node.TEXT_NODE) { return; }
const newStr = child.textContent.replace(props.search, props.replace);
// Only assign if the value would actually change, since blindly
// assigning could cause another mutation to be observed.
if (child.textContent === newStr) { return }
child.textContent = newStr
})
if (child.textContent === newStr) { return; }
child.textContent = newStr;
});
}
46 changes: 23 additions & 23 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const GLOBAL_SCRIPTS = [
'src/_utils.js'
]
];

const CONTENT_SCRIPTS = [
{
Expand Down Expand Up @@ -77,42 +77,42 @@ const CONTENT_SCRIPTS = [
'remove-view-count.js'
]
}
]
];

let isEnabled = true
let isEnabled = true;
let registeredContentScripts = []

;(function main () {
updateIcon()
updateContentScripts()
})()
updateIcon();
updateContentScripts();
})();

browser.browserAction.onClicked.addListener(() => {
isEnabled = !isEnabled
updateIcon()
updateContentScripts()
browser.tabs.reload()
})
isEnabled = !isEnabled;
updateIcon();
updateContentScripts();
browser.tabs.reload();
});

function updateIcon () {
browser.browserAction.setTitle({
title: `Disengaged (${isEnabled ? 'on' : 'off'})`
})
});

// Mobile Firefox doesn't support setIcon
if (!browser.browserAction.setIcon) { return }
if (!browser.browserAction.setIcon) { return; }
browser.browserAction.setIcon({
path: isEnabled ? 'icons/icon_48_on.png' : 'icons/icon_48_off.png'
})
});
}

function updateContentScripts () {
isEnabled ? registerContentScripts() : unregisterContentScripts()
isEnabled ? registerContentScripts() : unregisterContentScripts();
}

function unregisterContentScripts () {
registeredContentScripts.forEach(rcs => rcs.unregister())
registeredContentScripts = []
registeredContentScripts.forEach(rcs => rcs.unregister());
registeredContentScripts = [];
}

async function registerContentScripts () {
Expand All @@ -123,16 +123,16 @@ async function registerContentScripts () {
allFrames: !!contentScript.allFrames,
css: contentScript.files
.filter(file => file.match(/.*\.css$/))
.map((file) => { return { file: `${contentScript.folder}/${file}` } }),
.map((file) => { return { file: `${contentScript.folder}/${file}` }; }),
js: [].concat(
GLOBAL_SCRIPTS
.map((file) => { return { file } }),
.map((file) => { return { file }; }),
contentScript.files
.filter(file => file.match(/.*\.js$/))
.map((file) => { return { file: `${contentScript.folder}/${file}` } })
.map((file) => { return { file: `${contentScript.folder}/${file}` }; })
)
}).then((rcs) => {
registeredContentScripts.push(rcs)
})
})
registeredContentScripts.push(rcs);
});
});
}
28 changes: 14 additions & 14 deletions src/hacker-news/collapse-deep-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,34 @@ const COMMENT_COLLAPSE_DEPTH = 1;
* @type {Number} depth
*/
(function collapseDeepComments (depth) {
let indentWidth
let indentWidth;

document.querySelectorAll('tr.athing.comtr').forEach((comment) => {
// already collapsed? nothing to do
if (comment.classList.contains('coll')) return
if (comment.classList.contains('coll')) return;

// measure the indentation element to see how nested we are
const indent = comment.querySelector('td.ind img')
const indent = comment.querySelector('td.ind img');
if (!indentWidth && indent.width) {
// varies by platform; 40px on desktop and 12px on mobile
indentWidth = indent.width
indentWidth = indent.width;
}
// convert depth to indentation
const targetWidth = indentWidth * depth
const targetWidth = indentWidth * depth;

// rule out anything not indented deeply enough
if (!indent.width || indent.width < targetWidth) return
if (!indent.width || indent.width < targetWidth) return;

// set all of hn's classes for the collapsed comment
comment.classList.add('coll')
const toggle = comment.querySelector('.togg')
toggle.textContent = `[+${toggle.getAttribute('n')}]`
comment.querySelector('.comment').classList.add('noshow')
comment.querySelector('.votelinks').classList.add('nosee')
comment.classList.add('coll');
const toggle = comment.querySelector('.togg');
toggle.textContent = `[+${toggle.getAttribute('n')}]`;
comment.querySelector('.comment').classList.add('noshow');
comment.querySelector('.votelinks').classList.add('nosee');

// sub comments are additionally hidden with noshow
if (indent.width > targetWidth) {
comment.classList.add('noshow')
comment.classList.add('noshow');
}
})
})(COMMENT_COLLAPSE_DEPTH)
});
})(COMMENT_COLLAPSE_DEPTH);
22 changes: 11 additions & 11 deletions src/hacker-news/collapse-low-quality-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
(function collapseLowQualityComments () {
document.querySelectorAll('tr.athing.comtr').forEach((comment) => {
// already collapsed? nothing to do
if (comment.classList.contains('coll')) return
if (comment.classList.contains('coll')) return;

const fadedSpan = comment.querySelector('div.comment > span:not(.c00)')
const fadedSpan = comment.querySelector('div.comment > span:not(.c00)');

const div = comment.querySelector('div.comment')
const flagged = div.textContent.trim() === '[flagged]'
const div = comment.querySelector('div.comment');
const flagged = div.textContent.trim() === '[flagged]';

if (fadedSpan || flagged) {
// set all of hn's classes for the collapsed comment
comment.classList.add('coll')
const toggle = comment.querySelector('.togg')
toggle.textContent = `[+${toggle.getAttribute('n')}]`
comment.querySelector('.comment').classList.add('noshow')
comment.querySelector('.votelinks').classList.add('nosee')
comment.classList.add('coll');
const toggle = comment.querySelector('.togg');
toggle.textContent = `[+${toggle.getAttribute('n')}]`;
comment.querySelector('.comment').classList.add('noshow');
comment.querySelector('.votelinks').classList.add('nosee');
}
})
})()
});
})();
4 changes: 2 additions & 2 deletions src/hacker-news/remove-self-karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
search: /\(\d+\)\s/,
replace: '',
firstResultOnly: true
})
})()
});
})();
4 changes: 2 additions & 2 deletions src/youtube-music/remove-view-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
search: /^\s\s|[\d.]+[KMB]*\sviews$/,
replace: '',
firstResultOnly: true
})
})()
});
})();
Loading

0 comments on commit a6240ec

Please sign in to comment.