Created
October 20, 2016 13:31
-
-
Save anonymous/912b51649ea63007f7c1403d5b2c5659 to your computer and use it in GitHub Desktop.
Ebanoe.it new comments highlighter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Ebanoe.it highlighter | |
// @namespace http://ebanoe.it/ | |
// @version 0.2 | |
// @description ebanoe.it | |
// @author ebanoe.it | |
// @include http://ebanoe.it/* | |
// @include https://ebanoe.it/* | |
// @grant none | |
// @run-at document-end | |
// @noframes | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var backgroundColor = '#fff4cc'; | |
var threadRegexp = /\d{4}\/\d{2}\/\d{2}\/([^$]*)/i; | |
var archiveTime = 30 * 24 * 3600 * 1000; | |
var ebanoe = { | |
pageId: '', | |
comments: [], | |
init: function(){ | |
var parsedUrl = location.pathname.match(threadRegexp); | |
if (parsedUrl){ | |
this.pageId = 'eb_it_id' + parsedUrl[1]; | |
this.findNewComments(); | |
} | |
}, | |
findNewComments: function(){ | |
var visited = localStorage[this.pageId]; | |
var now = new Date().getTime(); | |
if (!visited || visited >= now){ | |
localStorage[this.pageId] = now; | |
return; | |
} | |
[].forEach.call(document.querySelectorAll('div[id^=div-comment-]'), function(item){ | |
var comment = item.querySelector('div.commentmetadata'); | |
var matches = (comment.innerText || comment.textContent).match(/\d+/ig); | |
if (matches){ | |
var commentTime = new Date(matches[2], matches[1] - 1 , matches[0], matches[3], matches[4]).getTime(); | |
if (commentTime > visited){ | |
ebanoe.comments.push({ | |
ts: commentTime, | |
node: item | |
}); | |
} | |
} | |
}); | |
//ebanoe.comments = ebanoe.comments.sort(function(a,b){ | |
// return a.ts - b.ts; | |
//}); | |
ebanoe.highlightNewComments(); | |
//ebanoe.injectNavigation(); | |
ebanoe.clearOldThreads(); | |
localStorage[this.pageId] = now; | |
}, | |
highlightNewComments: function(){ | |
ebanoe.comments.forEach(function(comment){ | |
comment.node.style.backgroundColor = backgroundColor; | |
}); | |
}, | |
injectNavigation: function(){ | |
if (ebanoe.comments.length){ | |
var reversedComments = ebanoe.comments.reverse(); | |
var navNode = document.createElement('div'); | |
navNode.style.position = 'fixed'; | |
navNode.style.backgroundColor = 'green'; | |
navNode.style.right = '0px'; | |
navNode.style.top = '50%'; | |
navNode.style.borderTopLeftRadius = '10px'; | |
navNode.style.borderBottomLeftRadius = '10px'; | |
navNode.style.cursor = 'pointer'; | |
navNode.style.color = 'rgb(255, 255, 255)'; | |
navNode.style.fontWeight = '700'; | |
navNode.style.padding = '10px 15px'; | |
navNode.style.boxSizing = 'border-box'; | |
navNode.style.zIndex = '99999'; | |
navNode.innerHTML = reversedComments.length; | |
navNode.addEventListener("click", function(){ | |
var comment = reversedComments.pop(); | |
comment.node.parentNode.scrollIntoView(); | |
navNode.innerHTML = reversedComments.length; | |
if (reversedComments.length < 1){ | |
navNode.style.display = 'none'; | |
} | |
}, false); | |
document.body.appendChild(navNode); | |
} | |
}, | |
clearOldThreads(){ | |
var now = new Date().getTime(); | |
for (var i=0; i<localStorage.length; i++) { | |
var key = localStorage.key(i); | |
if (/^eb_it_id/i.test(key) && parseInt(localStorage.getItem(key)) + ebanoe.archiveTime < now) { | |
localStorage.removeItem(key); | |
} | |
} | |
} | |
}; | |
ebanoe.init(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment