Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name E-Hentai Highlighter
- // @namespace http://userscripts.org/users/106844
- // @description Highlighter for E-Hentai (e-hentai.org/exhentai.org). Supports regular expressions.
- // @include http://g.e-hentai.org/*
- // @include http://gu.e-hentai.org/*
- // @exclude http://g.e-hentai.org/g/*
- // @include http://exhentai.org/*
- // @include http://u.exhentai.org/*
- // @exclude http://exhentai.org/g/*
- // @version 0.2.3
- // ==/UserScript==
- // -------------------- OPTIONS -------------------
- var DEFAULT_COLOR = '#a9938f'; // background color for highlighted items (E-Hentai)
- var EX_COLOR = '#3f4148'; // background color for highlighted items (Exhentai)
- var HIGHLIGHTER_ROWS = 9; // height (in # of rows) of the highlighter textarea
- var FILTER_ROWS = 6; // height (in # of rows) of the filter textarea
- var TEXTAREA_COLS = 40; // width (in # of columns) of both textareas
- var USE_NEWLINES = true; // use newline as the default delimiter (instead of ;)
- // --------------- COMMON FUNCTIONS ---------------
- function C(x,html) {
- var tokens = x.match(/^[^#.]+|[#.][^#.]+/g),
- res = document.createElement(tokens[0]);
- tokens.shift();
- tokens.forEach(function(k) {
- if (k.charAt(0) == '.') res.className += k.substr(1) + ' ';
- else if (k.charAt(0) == '#') res.id = k.substr(1);
- });
- if (html != null) res.innerHTML = html;
- return res;
- }
- function I(id) {
- return document.getElementById(id);
- }
- function Q(css) {
- return document.querySelector(css);
- }
- function q(css) {
- return document.querySelectorAll(css);
- }
- function S(sub,str) {
- return (str||document.URL).indexOf(sub)!=-1;
- }
- function E(exp,root) {
- return document.evaluate(exp,root||document.body,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue;
- }
- function e(exp,root) {
- return document.evaluate(exp,root||document.body,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
- }
- function load(key,def) {
- var res;
- if (typeof(GM_deleteValue) != 'undefined') res = GM_getValue(key,def);
- else res = (x=localStorage.getItem(key))!=null?x:def;
- return JSON.parse(res);
- }
- function save(key,value) {
- if (typeof(GM_deleteValue) != 'undefined') GM_setValue(key,JSON.stringify(value));
- else localStorage.setItem(key,JSON.stringify(value));
- }
- // --------------------- MAIN ---------------------
- var EHH = {
- init: function() {
- // User data
- EHH.keywords = load('keywords','[]');
- EHH.filters = load('filters','[]');
- EHH.highlighterDisabled = load('highlighterDisabled',false);
- EHH.filterDisabled = load('filterDisabled',false);
- // Permanent style
- document.head.appendChild(C('style',
- '#e-HentaiPopup {' +
- 'position: fixed; top: 0; right: 0; padding: 3px;' +
- 'border: 1px black solid; background: #686868; z-index: 10;' +
- '} #e-HentaiPopup:not(:hover) *:not(:first-child) { display: none; }' +
- '#e-HentaiPopup * {' +
- 'font-family: Verdana, Tahoma, Georgia, Dejavu, "Times New Roman", Serif;' +
- 'font-size: 10px;' +
- '} #e-HentaiPopup *:not(textarea) { color: white; }' +
- '#e-HentaiPopup div { text-align: center; }' +
- '#e-HentaiPopup tr:last-child { text-align: left; }' +
- '#e-HentaiPopup td:nth-child(2) { text-align: right; }' +
- '#e-HentaiPopup td:nth-child(2) a, #e-HentaiPopup tr:last-child a {' +
- 'cursor: pointer; font-weight: bold; border-bottom: 1px dotted;' +
- '} #e-HentaiPopup .e-Disable { color: #94de80; }' +
- '#e-HentaiPopup .e-Disable:hover { color: #b9ffa6; }' +
- '#e-HentaiPopup .e-Enable { color: #d98080; }' +
- '#e-HentaiPopup .e-Enable:hover { color: #f1bebe; }' +
- '#e-HentaiPopup tr:last-child a:hover { color: black; }' +
- '#e-HentaiPopup td > span { margin-right: 5px; float: right; }' +
- '.itg > .c { display: none; }' +
- '.itd1, .ido > h1.ih ~ div {' +
- 'float: none !important; display: inline-block;' +
- 'margin: 0px !important; vertical-align: top;' +
- '} .e-Highlighted b { font-weight: inherit; }'));
- // Swappable styles
- EHH.highlighterCSS = C('style#e-Highlighter',
- '.e-Highlighted { background: -moz-linear-gradient(top, rgba(255,255,255,0), ' + (S('e-hentai')?DEFAULT_COLOR:EX_COLOR) + ' 100%) !important; border: 2px solid ' + (S('e-hentai')?DEFAULT_COLOR:EX_COLOR) + ' !important; border-radius: 5px; margin: 3px; padding: 2px !important}' +
- '.e-Highlighted b { font-weight: bold !important; text-decoration: none; }');
- EHH.filterCSS = C('style#e-Filter',
- 'tr.color0 { background: ' + (S('e-hentai')?'#F2F0E4':'#363940') + '; }' +
- 'tr.color1 { background: ' + (S('e-hentai')?'#EDEBDF':'#4F535B') + '; }' +
- '#toppane ~ .c, .e-Filtered { display: none !important; }');
- // Popup
- document.body.appendChild(C('div#e-HentaiPopup',
- '<div><b>E-H Highlighter</b></div><hr /><table align="right">' +
- '<tr><td>Keywords:</td><td><a id="e-HighlighterSwitch">Highlighter: enabled</a></tr>' +
- '<tr><td colspan="2"><textarea rows="' + HIGHLIGHTER_ROWS +
- '" cols="' + TEXTAREA_COLS + '"></textarea></td></tr>' +
- '<tr><td>Filters:</td><td><a id="e-FilterSwitch">Filter: enabled</a></td></tr>' +
- '<tr><td colspan="2"><textarea rows="' + FILTER_ROWS +
- '" cols="' + TEXTAREA_COLS + '"></textarea></td></tr>' +
- '<tr><td colspan="2"><a id="e-PopupSave">Save changes</a>' +
- '<span><b>Filtered items:</b> <span id="e-FilteredItems"></span></span></tr></table>'));
- // Popup elements
- EHH.highlighterSwitch = I('e-HighlighterSwitch');
- EHH.filterSwitch = I('e-FilterSwitch');
- EHH.updateSwitch(EHH.highlighterSwitch,EHH.highlighterDisabled);
- EHH.updateSwitch(EHH.filterSwitch,EHH.filterDisabled);
- var textareas = q('#e-HentaiPopup textarea');
- EHH.highlighterArea = textareas[0];
- EHH.filterArea = textareas[1];
- EHH.highlighterArea.textContent = EHH.keywords.join(USE_NEWLINES?'\n':';');
- EHH.filterArea.textContent = EHH.filters.join(USE_NEWLINES?'\n':';');
- // Events
- EHH.highlighterSwitch.addEventListener('click',function() {
- EHH.highlighterDisabled = /enabled/.test(this.textContent);
- EHH.updateSwitch(this,EHH.highlighterDisabled);
- save('highlighterDisabled',EHH.highlighterDisabled);
- },false);
- EHH.filterSwitch.addEventListener('click',function() {
- EHH.filterDisabled = /enabled/.test(this.textContent);
- EHH.updateSwitch(this,EHH.filterDisabled);
- save('filterDisabled',EHH.filterDisabled);
- },false);
- I('e-PopupSave').addEventListener('click',function() {
- EHH.keywords = EHH.highlighterArea.value.split(/[;\n]/).filter(function(x) { return x.length>0; });
- EHH.filters = EHH.filterArea.value.split(/[;\n]/).filter(function(x) { return x.length>0; });
- save('keywords',EHH.keywords);
- save('filters',EHH.filters);
- EHH.walk();
- },false);
- document.addEventListener('DOMNodeInserted',function(e) {
- if (e.target.nodeName == 'TBODY')
- EHH.walk(e.target);
- },false);
- // Start
- EHH.swap();
- EHH.walk();
- },
- swap: function() {
- if (!EHH.highlighterDisabled && !I('e-Highlighter')) document.head.appendChild(EHH.highlighterCSS);
- else if (EHH.highlighterDisabled && I('e-Highlighter')) document.head.removeChild(EHH.highlighterCSS);
- if (!EHH.filterDisabled && !I('e-Filter')) document.head.appendChild(EHH.filterCSS);
- else if (EHH.filterDisabled && I('e-Filter')) document.head.removeChild(EHH.filterCSS);
- I('e-FilteredItems').textContent = EHH.filterDisabled?0:q('.e-Filtered').length;
- },
- updateSwitch: function(target,disable) {
- target.textContent = target.textContent.replace(/[^\s]+$$/,disable?'disabled':'enabled');
- target.className = disable?'e-Enable':'e-Disable';
- EHH.swap();
- },
- walk: function(root) {
- var highlightReg = !EHH.keywords.length?null:new RegExp('(' + EHH.keywords.join('|') + ')','gi'),
- filterReg = !EHH.filters.length?null:new RegExp(EHH.filters.join('|'),'i'),
- flip = false,
- targets = e('.//*[starts-with(@class,"gtr") or starts-with(@class,"itd1")]|.//table[@class="t2"]//tr');
- for (var i=0;i!=targets.snapshotLength;i++) {
- var target = targets.snapshotItem(i),
- title = E('.//div[@class="it3" or @class="itd2" or @class="t2"]/' +
- 'a[not(@rel="nofollow")]',target)||E('./td//a',target);
- if (filterReg) {
- if (filterReg.test(title.textContent)) {
- if (!S('e-Filtered',target.className)) target.className += ' e-Filtered';
- flip = !flip;
- } else if (S('e-Filtered',target.className)) {
- target.className = target.className.replace(/ ?e-Filtered/g,'');
- }
- }
- if (highlightReg) {
- if (highlightReg.test(title.textContent)) {
- if (!S('e-Highlighted',target.className)) target.className += ' e-Highlighted';
- title.innerHTML = title.innerHTML.replace(highlightReg,'<b>$1</b>').replace(/(<\/?b>)+/g,'$1');
- } else if (S('e-Highlighted',target.className)) {
- target.className = target.className.replace(/ ?e-Highlighted/g,'');
- title.innerHTML = title.innerHTML.replace(/<b>(.+?)<\/b>/g,'$1');
- }
- } else if (S('e-Highlighted',target.className)) {
- target.className = target.className.replace(/\s?e-Highlighted/,'');
- title.innerHTML = title.innerHTML.replace(/<b>(.+?)<\/b>/g,'$1');
- }
- if (!/^gtr/.test(target.className)) continue;
- if (!S('color',target.className)) target.className += ' color' + Number(flip=!flip);
- else target.className = target.className.replace(/color\d/,'color' + Number(flip=!flip));
- }
- I('e-FilteredItems').textContent = EHH.filterDisabled?0:q('.e-Filtered').length;
- }
- }
- EHH.init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement