js: Use an IIFE.

This commit is contained in:
Florian Bruhin 2015-05-12 07:10:08 +02:00
parent 7a67af24f0
commit 3f21ac6b6a
2 changed files with 24 additions and 23 deletions

View File

@ -16,7 +16,7 @@ rules:
no-throw-literal: 2
no-void: 2
radix: 2
wrap-iife: 2
wrap-iife: [2, "inside"]
brace-style: [2, "1tbs", {"allowSingleLine": true}]
comma-style: [2, "last"]
consistent-this: [2, "self"]

View File

@ -81,27 +81,28 @@ function isElementInViewport(node) {
return boundingRect.top >= -20;
}
var walker = document.createTreeWalker(document.body, 4, null);
var node;
var textNodes = [];
while (node = walker.nextNode()) {
if (node.nodeType === 3 && node.data.trim() !== '') {
textNodes.push(node);
(function() {
var walker = document.createTreeWalker(document.body, 4, null);
var node;
var textNodes = [];
while (node = walker.nextNode()) {
if (node.nodeType === 3 && node.data.trim() !== '') {
textNodes.push(node);
}
}
}
for (var i = 0; i < textNodes.length; i++) {
var element = textNodes[i].parentElement;
if (isElementInViewport(element.parentElement)) {
el = element;
break;
for (var i = 0; i < textNodes.length; i++) {
var element = textNodes[i].parentElement;
if (isElementInViewport(element.parentElement)) {
el = element;
break;
}
}
}
if (el !== undefined) {
var range = document.createRange();
range.setStart(el, 0);
range.setEnd(el, 0);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
if (el !== undefined) {
var range = document.createRange();
range.setStart(el, 0);
range.setEnd(el, 0);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
})();